Commit b7ab0f6dd3e0135da60022cf552664386d921d5e

Anton Maminov 2013-01-09T17:00:41

Better support for fetching stickers from MPD

diff --git a/examples/search_and_replace_playlist.rb b/examples/search_and_replace_playlist.rb
index 1a902b2..210fdc1 100644
--- a/examples/search_and_replace_playlist.rb
+++ b/examples/search_and_replace_playlist.rb
@@ -3,12 +3,18 @@
 require 'bundler'
 Bundler.setup :default
 
+require 'logger'
 require 'mpd_client'
 
+#MPDClient.log = Logger.new($stderr)
+
+client = MPDClient.new
+
 type = ARGV[0]
 what = ARGV[1]
 
 client = MPDClient.new
+client.log = Logger.new($stderr)
 
 # Connecting to the server
 client.connect('localhost', 6600)
diff --git a/examples/stickers.rb b/examples/stickers.rb
new file mode 100644
index 0000000..b4b0cd5
--- /dev/null
+++ b/examples/stickers.rb
@@ -0,0 +1,50 @@
+# encoding: utf-8
+
+require 'bundler'
+Bundler.setup :default
+
+require 'logger'
+require 'mpd_client'
+
+type = ARGV[0]
+what = ARGV[1]
+
+client = MPDClient.new
+client.log = Logger.new($stderr)
+
+# Connecting to the server
+client.connect('/var/run/mpd/socket')
+
+puts "MPD version: #{client.mpd_version}"
+puts "mpd_client version: #{MPDClient::VERSION}"
+
+uri = "world/j/Jane Air/2012.Иллюзия полёта/12. Любить любовь.ogg"
+
+# sticker set {TYPE} {URI} {NAME} {VALUE}
+#   Adds a sticker value to the specified object. If a sticker item with that name already exists, it is replaced.
+#
+client.sticker_set('song', uri, 'rating', '1')
+
+# sticker get {TYPE} {URI} {NAME}
+#   Reads a sticker value for the specified object.
+#
+puts client.sticker_get('song', uri, 'rating')
+
+# sticker list {TYPE} {URI}
+#   Lists the stickers for the specified object.
+#
+puts client.sticker_list('song', uri)
+
+# sticker find {TYPE} {URI} {NAME}
+#   Searches the sticker database for stickers with the specified name, below the specified directory (URI).
+#   For each matching song, it prints the URI and that one sticker's value.
+#
+puts client.sticker_find('song', '/', 'rating')
+
+# sticker delete {TYPE} {URI} [NAME]
+#   Deletes a sticker value from the specified object. If you do not specify a sticker name, all sticker values are deleted.
+#
+puts client.sticker_delete('song', uri, 'rating')
+
+client.close
+client.disconnect
diff --git a/lib/mpd_client.rb b/lib/mpd_client.rb
index 2f5336c..70872b1 100644
--- a/lib/mpd_client.rb
+++ b/lib/mpd_client.rb
@@ -87,10 +87,10 @@ COMMANDS = {
   "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"              => "",
@@ -183,7 +183,7 @@ class MPDClient
     return fetch_command_list
   end
 
-  # Sets the +logger+ used by this instance of MPDClient
+  # The current logger. If no logger has been set MPDClient.log is used
   #
   def log
     @log || MPDClient.log
@@ -195,8 +195,6 @@ class MPDClient
     @log = logger
   end
 
-
-
   private
 
   def execute(command, *args, retval)
@@ -334,6 +332,19 @@ class MPDClient
     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