summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2014-07-06 16:45:00 -0700
committerDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2014-07-06 16:45:00 -0700
commita68efddce147f3ace1107720a1c3fb1364508e18 (patch)
tree1a8d9087151bee615b4c279ac6073dce4cd59596
parent602cbf21d35ba898841cac0f3c458bd90b6b6841 (diff)
downloadruby-libmpd-a68efddce147f3ace1107720a1c3fb1364508e18.tar.gz
ruby-libmpd-a68efddce147f3ace1107720a1c3fb1364508e18.tar.xz
Add disconnect method and exception class.
-rw-r--r--libmpd.rb20
1 files changed, 20 insertions, 0 deletions
diff --git a/libmpd.rb b/libmpd.rb
index 744a524..be46161 100644
--- a/libmpd.rb
+++ b/libmpd.rb
@@ -37,6 +37,12 @@ class MPD
include MPDPlaylist
include MPDStatus
+ # Exception class used for MPD-related errors. At the moment, it is only
+ # raised when attempting to perform an operation without having connected
+ # first.
+ class Error < StandardError
+ end
+
# Initialise an MPD object with the specified host and port.
#
# The default host is "localhost" and the default port is 6600.
@@ -52,8 +58,18 @@ class MPD
@socket.gets.chomp
end
+ # Disconnects from the server.
+ def disconnect
+ ensure_connected
+
+ @socket.close
+ @socket = nil
+ end
+
# Sends a command to the server and returns the response.
def send_request(command)
+ ensure_connected
+
# Escape backslashes in command.
@socket.puts command.gsub('\\', '\\\\\\')
@@ -62,6 +78,10 @@ class MPD
private
+ def ensure_connected
+ raise MPD::Error, 'Not connected to server.' if @socket.nil?
+ end
+
def get_response
response = String.new