Commit 00e02f714c8e637356c87cd9346337955896d3f7

Anton Maminov 2013-02-19T17:16:28

add support for ranges

diff --git a/README.md b/README.md
index 6313fe3..6875f22 100644
--- a/README.md
+++ b/README.md
@@ -36,6 +36,12 @@ Once you have an instance of the `MPDClient` class, start by connecting to the s
 client.connect('localhost', 6600)
 ```
 
+or Unix domain socket
+
+```ruby
+client.connect('/var/run/mpd/socket')
+```
+
 The client library can be used as follows:
 
 ```ruby
@@ -54,6 +60,48 @@ client.status                # insert the status command into the list
 client.command_list_end      # result will be a Array with the results
 ```
 
+### Ranges
+
+Some commands(e.g. `move`, `delete`, `load`, `shuffle`, `playlistinfo`) support integer ranges(`[START:END]`) as argument. This is done in `mpd_client` by using two element array:
+
+```ruby
+# move the first three songs after the fifth number in the playlist
+client.move([0, 3], 5)
+```
+
+Second element can be omitted. MPD will assumes the biggest possible number then:
+
+```ruby
+# delete all songs from the current playlist, except for the firts ten
+client.delete([10,])
+```
+
+### Logging
+
+Default logger for all MPDClient instances:
+
+```ruby
+require 'logger'
+require 'mpd_client'
+
+MPDClient.log = Logger.new($stderr)
+
+client = MPDClient.new
+```
+
+Sets the logger used by this instance of MPDClient:
+
+```ruby
+require 'logger'
+require 'mpd_client'
+
+client = MPDClient.new
+client.log = Logger.new($stderr)
+```
+
+
+For more information about logging configuration, see http://www.ruby-doc.org/stdlib-1.9.3/libdoc/logger/rdoc/Logger.html
+
 ## Contributing
 
 1. Fork it
@@ -64,4 +112,4 @@ client.command_list_end      # result will be a Array with the results
 
 ## License
 
-This library is distributed under the MIT license.  Please see the LICENSE file.
\ No newline at end of file
+This library is distributed under the MIT license.  Please see the LICENSE file.
diff --git a/examples/range.rb b/examples/range.rb
new file mode 100644
index 0000000..50deebe
--- /dev/null
+++ b/examples/range.rb
@@ -0,0 +1,19 @@
+require 'bundler'
+Bundler.setup :default
+
+require 'pp'
+require 'logger'
+require 'mpd_client'
+
+MPDClient.log = Logger.new($stderr)
+
+client = MPDClient.new
+client.connect('localhost', 6600)
+
+# delete all songs from the current playlist, except for the firts ten
+client.delete([10,])
+
+# move the first three songs after the fifth number in the playlist
+client.move([0, 3], 69)
+
+pp client.playlistinfo([69, 71])
diff --git a/examples/stickers.rb b/examples/stickers.rb
index b4b0cd5..b56d58f 100644
--- a/examples/stickers.rb
+++ b/examples/stickers.rb
@@ -6,11 +6,12 @@ Bundler.setup :default
 require 'logger'
 require 'mpd_client'
 
-type = ARGV[0]
-what = ARGV[1]
+MPDClient.log = Logger.new($stderr)
+
+# Stickers
+# http://www.musicpd.org/doc/protocol/ch03s07.html
 
 client = MPDClient.new
-client.log = Logger.new($stderr)
 
 # Connecting to the server
 client.connect('/var/run/mpd/socket')
@@ -44,7 +45,7 @@ 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.sticker_delete('song', uri, 'rating')
 
 client.close
 client.disconnect
diff --git a/lib/mpd_client.rb b/lib/mpd_client.rb
index 70872b1..063ffb2 100644
--- a/lib/mpd_client.rb
+++ b/lib/mpd_client.rb
@@ -1,4 +1,4 @@
-# -*- encoding: utf-8 -*-
+# encoding: utf-8
 
 require 'socket'
 require "mpd_client/version"
@@ -155,6 +155,7 @@ class MPDClient
   end
 
   def connect(host = 'localhost', port = 6600)
+    log.info("MPD connect #{host}, #{port}") if log
     if host.start_with?('/')
       @socket = UNIXSocket.new(host)
       hello
@@ -165,6 +166,7 @@ class MPDClient
   end
 
   def disconnect
+    log.info("MPD disconnect")
     @socket.close
     reset
   end
@@ -208,14 +210,21 @@ class MPDClient
   end
 
   def write_line(line)
-    log.debug("MPD command: #{line}") if log
     @socket.puts line
     @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
 
diff --git a/lib/mpd_client/version.rb b/lib/mpd_client/version.rb
index c00860a..7955d49 100644
--- a/lib/mpd_client/version.rb
+++ b/lib/mpd_client/version.rb
@@ -1,5 +1,5 @@
-# -*- encoding: utf-8 -*-
+# encoding: utf-8
 
 class MPDClient
-  VERSION = "0.0.2"
+  VERSION = "0.0.3"
 end