lib/mpd_client.rb in mpd_client-0.0.2 vs lib/mpd_client.rb in mpd_client-0.0.3
- old
+ new
@@ -1,6 +1,6 @@
-# -*- encoding: utf-8 -*-
+# encoding: utf-8
require 'socket'
require "mpd_client/version"
HELLO_PREFIX = "OK MPD "
@@ -80,19 +80,19 @@
"list" => "fetch_list",
"listall" => "fetch_database",
"listallinfo" => "fetch_database",
"lsinfo" => "fetch_database",
"search" => "fetch_songs",
- "searchadd" => "fetch_nothing",
- "searchaddp1" => "fetch_nothing",
+ "searchadd" => "fetch_nothing",
+ "searchaddp1" => "fetch_nothing",
"update" => "fetch_item",
"rescan" => "fetch_item",
# Sticker Commands
- "sticker get" => "fetch_item",
+ "sticker get" => "fetch_sticker",
"sticker set" => "fetch_nothing",
"sticker delete" => "fetch_nothing",
- "sticker list" => "fetch_list",
+ "sticker list" => "fetch_stickers",
"sticker find" => "fetch_songs",
# Connection Commands
"close" => "",
"kill" => "",
"password" => "fetch_nothing",
@@ -114,28 +114,61 @@
"channels" => "fetch_list",
"readmessages" => "fetch_messages",
"sendmessage" => "fetch_nothing"
}
+# The MPDClient library is used for interactions with a MPD.
+#
+# == Example
+#
+# require 'mpd_client'
+# require 'logger'
+#
+# client = MPDClient.new
+# client.log = Logger.new($stderr)
+# client.connect('/var/run/mpd/socket')
+#
class MPDClient
attr_reader :mpd_version
+ class << self
+ # Default logger for all MPDClient instances
+ #
+ # MPDClient.log = Logger.new($stderr)
+ #
+ attr_accessor :log
+
+ def add_command(name, retval)
+ escaped_name = name.gsub(' ', '_')
+ define_method escaped_name.to_sym do |*args|
+ execute(name, *args, retval)
+ end
+ end
+
+ def remove_command(name)
+ raise "Can't remove not existent '#{name}' command" unless method_defined? name.to_sym
+ remove_method name.to_sym
+ end
+ end
+
def initialize
reset
end
def connect(host = 'localhost', port = 6600)
+ log.info("MPD connect #{host}, #{port}") if log
if host.start_with?('/')
@socket = UNIXSocket.new(host)
hello
else
@socket = TCPSocket.new(host, port)
hello
end
end
def disconnect
+ log.info("MPD disconnect")
@socket.close
reset
end
# http://www.musicpd.org/doc/protocol/ch01s04.html
@@ -150,21 +183,20 @@
write_command('command_list_end')
return fetch_command_list
end
- class << self
- def add_command(name, retval)
- define_method name.to_sym do |*args|
- execute(name, *args, retval)
- end
- end
+ # The current logger. If no logger has been set MPDClient.log is used
+ #
+ def log
+ @log || MPDClient.log
+ end
- def remove_command(name)
- raise "Can't remove not existent '#{name}' command" unless method_defined? name.to_sym
- remove_method name.to_sym
- end
+ # Sets the +logger+ used by this instance of MPDClient
+ #
+ def log= logger
+ @log = logger
end
private
def execute(command, *args, retval)
@@ -182,11 +214,19 @@
@socket.flush
end
def write_command(command, *args)
parts = [command]
- args.each{|arg| parts << "\"#{escape(arg)}\""}
+ args.each do |arg|
+ if arg.kind_of?(Array)
+ parts << (arg.size == 1 ? "\"#{arg[0].to_i}:\"" : "\"#{arg[0].to_i}:#{arg[1].to_i}\"")
+ else
+ parts << "\"#{escape(arg)}\""
+ end
+ end
+ #log.debug("Calling MPD: #{command}#{args}") if log
+ log.debug("Calling MPD: #{parts.join(' ')}") if log
write_line(parts.join(' '))
end
def read_line
line = @socket.gets.force_encoding('utf-8')
@@ -299,10 +339,23 @@
end
return result
end
+ def fetch_stickers
+ result = []
+ read_pairs.each do |key, sticker|
+ value = sticker.split('=', 2)
+ raise "Could now parse sticker: #{sticker}" if value.size < 2
+ result << Hash[*value]
+ end
+
+ return result
+ end
+
+ def fetch_sticker; fetch_stickers[0]; end
+
def fetch_command_list
result = []
begin
@command_list.each do |retval|
result << (eval retval)
@@ -325,9 +378,10 @@
def reset
@mpd_version = nil
@command_list = nil
@socket = nil
+ @log = nil
end
def escape(text)
text.to_s.gsub("\\", "\\\\").gsub('"', '\\"')
end