From a68efddce147f3ace1107720a1c3fb1364508e18 Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Sun, 6 Jul 2014 16:45:00 -0700 Subject: Add disconnect method and exception class. --- libmpd.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) 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 -- cgit v1.2.3-70-g09d2