summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--libmpd.rb48
-rw-r--r--libmpd/database.rb8
-rw-r--r--libmpd/playbackcontrol.rb31
-rw-r--r--libmpd/playbackoptions.rb38
-rw-r--r--libmpd/playlist.rb24
-rw-r--r--libmpd/status.rb10
6 files changed, 74 insertions, 85 deletions
diff --git a/libmpd.rb b/libmpd.rb
index 88df927..744a524 100644
--- a/libmpd.rb
+++ b/libmpd.rb
@@ -1,7 +1,7 @@
#!/usr/bin/env ruby
#
#--
-# Copyright 2009 David Vazgenovich Shakaryan <dvshakaryan@gmail.com>
+# Copyright 2009-2014 David Vazgenovich Shakaryan <dvshakaryan@gmail.com>
# Distributed under the terms of the GNU General Public License v3.
# See http://www.gnu.org/licenses/gpl.txt for the full license text.
#++
@@ -9,15 +9,13 @@
# *Author*:: David Vazgenovich Shakaryan
# *License*:: GNU General Public License v3
-
require 'socket'
-MODULE_PATH = File.expand_path(File.dirname(__FILE__)) + '/libmpd/'
-require MODULE_PATH + 'database'
-require MODULE_PATH + 'playbackcontrol'
-require MODULE_PATH + 'playbackoptions'
-require MODULE_PATH + 'playlist'
-require MODULE_PATH + 'status'
+require_relative 'libmpd/database'
+require_relative 'libmpd/playbackcontrol'
+require_relative 'libmpd/playbackoptions'
+require_relative 'libmpd/playlist'
+require_relative 'libmpd/status'
class TrueClass # :nodoc:
def to_i
@@ -42,28 +40,32 @@ class MPD
# Initialise an MPD object with the specified host and port.
#
# The default host is "localhost" and the default port is 6600.
- def initialize host='localhost', port=6600
+ def initialize(host = 'localhost', port = 6600)
@host = host
@port = port
end
# Connects to the server.
def connect
- @socket = TCPSocket.new @host, @port
- return @socket.gets.chomp
+ @socket = TCPSocket.new(@host, @port)
+
+ @socket.gets.chomp
end
# Sends a command to the server and returns the response.
- def send_request command
+ def send_request(command)
# Escape backslashes in command.
@socket.puts command.gsub('\\', '\\\\\\')
- return get_response
+
+ get_response
end
+ private
+
def get_response
response = String.new
- while true
+ loop do
line = @socket.gets
return response if line == "OK\n"
@@ -73,26 +75,20 @@ class MPD
end
end
- def generate_hash str
+ def generate_hash(str)
hash = Hash.new
str.split("\n").each do |line|
field, value = line.split(': ', 2)
- hash[field.downcase.to_sym] = value
+ hash[field.downcase.intern] = value
end
- return hash
+ hash
end
- def split_and_hash str
- songs = str.split(/(?!\n)(?=file:)/).map do |song|
- generate_hash song
+ def split_and_hash(str)
+ str.split(/(?!\n)(?=file:)/).map do |song|
+ generate_hash(song)
end
-
- return songs
end
-
- private :generate_hash
- private :get_response
- private :split_and_hash
end
diff --git a/libmpd/database.rb b/libmpd/database.rb
index e5f86b3..4249f5e 100644
--- a/libmpd/database.rb
+++ b/libmpd/database.rb
@@ -1,7 +1,7 @@
#!/usr/bin/env ruby
#
#--
-# Copyright 2009 David Vazgenovich Shakaryan <dvshakaryan@gmail.com>
+# Copyright 2009-2014 David Vazgenovich Shakaryan <dvshakaryan@gmail.com>
# Distributed under the terms of the GNU General Public License v3.
# See http://www.gnu.org/licenses/gpl.txt for the full license text.
#++
@@ -13,7 +13,7 @@
module MPDDatabase
# Returns all directories.
# If an argument is specified, list all subdirectories of that directory.
- def directories(uri=nil)
+ def directories(uri = nil)
command = 'lsinfo'
command << ' "%s"' % uri if uri
@@ -41,7 +41,7 @@ module MPDDatabase
# Returns all files.
# If an argument is specified, list all files in that directory.
- def files(uri=nil)
+ def files(uri = nil)
command = 'lsinfo'
command << ' "%s"' % uri if uri
@@ -67,7 +67,7 @@ module MPDDatabase
# Updates the database.
# If an argument is given, update that particular file or directory.
- def update(uri=nil)
+ def update(uri = nil)
command = 'update'
command << ' "%s"' % uri if uri
diff --git a/libmpd/playbackcontrol.rb b/libmpd/playbackcontrol.rb
index 1e7a9c4..5707703 100644
--- a/libmpd/playbackcontrol.rb
+++ b/libmpd/playbackcontrol.rb
@@ -1,7 +1,7 @@
#!/usr/bin/env ruby
#
#--
-# Copyright 2009 David Vazgenovich Shakaryan <dvshakaryan@gmail.com>
+# Copyright 2009-2014 David Vazgenovich Shakaryan <dvshakaryan@gmail.com>
# Distributed under the terms of the GNU General Public License v3.
# See http://www.gnu.org/licenses/gpl.txt for the full license text.
#++
@@ -13,69 +13,66 @@
module MPDPlaybackControl
# Plays the next song in the playlist.
def next
- return send_request('next')
+ send_request('next')
end
# Sets pause state.
#
# Accepts an argument of +true+ to enable or +false+ to disable.
# If no argument is given, defaults to +true+.
- def pause(state=true)
- return send_request('pause %s' % state.to_i)
+ def pause(state = true)
+ send_request('pause %s' % state.to_i)
end
# Returns +true+ if paused.
# Otherwise, returns +false+.
def paused?
- return true if status[:state] == 'pause'
- return false
+ status[:state] == 'pause'
end
# Begins playing the playlist. If an argument is given, begins at the
# specified song position.
- def play(songpos=nil)
+ def play(songpos = nil)
command = 'play'
command << ' %s' % songpos if songpos
- return send_request(command)
+ send_request(command)
end
# Begins playing the playlist. If an argument is given, begins at the
# specified song id.
- def playid(songid=nil)
+ def playid(songid = nil)
command = 'playid'
command << ' %s' % songid if songid
- return send_request(command)
+ send_request(command)
end
# Returns +true+ if playing.
# Otherwise, returns +false+.
def playing?
- return true if status[:state] == 'play'
- return false
+ status[:state] == 'play'
end
# Plays the previous song in the playlist.
def previous
- return send_request('previous')
+ send_request('previous')
end
# Seeks to the given position of the current song.
# Accepts an argument of seconds.
def seek(time)
- return send_request('seek %s %s' % [status[:song], time])
+ send_request('seek %s %s' % [status[:song], time])
end
# Stops playing.
def stop
- return send_request('stop')
+ send_request('stop')
end
# Returns +true+ if stopped.
# Otherwise, returns +false+.
def stopped?
- return true if status[:state] == 'stop'
- return false
+ status[:state] == 'stop'
end
end
diff --git a/libmpd/playbackoptions.rb b/libmpd/playbackoptions.rb
index f9d39ec..84afd47 100644
--- a/libmpd/playbackoptions.rb
+++ b/libmpd/playbackoptions.rb
@@ -1,7 +1,7 @@
#!/usr/bin/env ruby
#
#--
-# Copyright 2009 David Vazgenovich Shakaryan <dvshakaryan@gmail.com>
+# Copyright 2009-2014 David Vazgenovich Shakaryan <dvshakaryan@gmail.com>
# Distributed under the terms of the GNU General Public License v3.
# See http://www.gnu.org/licenses/gpl.txt for the full license text.
#++
@@ -16,65 +16,62 @@ module MPDPlaybackOptions
#
# Accepts an argument of +true+ to enable or +false+ to disable.
# If no argument is given, defaults to +true+.
- def consume(state=true)
- return send_request('consume %s' % state.to_i)
+ def consume(state = true)
+ send_request('consume %s' % state.to_i)
end
# Returns +true+ if consume is activated.
# Otherwise, returns +false+.
def consume?
- return true if status[:consume] == '1'
- return false
+ status[:consume] == '1'
end
# Sets crossfading between songs.
def crossfade(seconds)
- return send_request('crossfade %s' % seconds)
+ send_request('crossfade %s' % seconds)
end
# Returns the current crossfade setting as an integer.
def crossfade?
- return status[:xfade].to_i
+ status[:xfade].to_i
end
# Sets random state.
#
# Accepts an argument of +true+ to enable or +false+ to disable.
# If no argument is given, defaults to +true+.
- def random(state=true)
- return send_request('random %s' % state.to_i)
+ def random(state = true)
+ send_request('random %s' % state.to_i)
end
# Returns +true+ if random is activated.
# Otherwise, returns +false+.
def random?
- return true if status[:random] == '1'
- return false
+ status[:random] == '1'
end
# Sets repeat state.
#
# Accepts an argument of +true+ to enable or +false+ to disable.
# If no argument is given, defaults to +true+.
- def repeat(state=true)
- return send_request('repeat %s' % state.to_i)
+ def repeat(state = true)
+ send_request('repeat %s' % state.to_i)
end
# Returns +true+ if repeat is activated.
# Otherwise, returns +false+.
def repeat?
- return true if status[:repeat] == '1'
- return false
+ status[:repeat] == '1'
end
# Sets volume from a range of 0 to 100.
def volume(volume)
- return send_request('setvol %s' % volume)
+ send_request('setvol %s' % volume)
end
# Returns the current volume as an integer.
def volume?
- return status[:volume].to_i
+ status[:volume].to_i
end
# Sets single state.
@@ -83,14 +80,13 @@ module MPDPlaybackOptions
#
# Accepts an argument of +true+ to enable or +false+ to disable.
# If no argument is given, defaults to +true+.
- def single(state=true)
- return send_request('single %s' % state.to_i)
+ def single(state = true)
+ send_request('single %s' % state.to_i)
end
# Returns +true+ if single is activated.
# Otherwise, returns +false+.
def single?
- return true if status[:single] == '1'
- return false
+ status[:single] == '1'
end
end
diff --git a/libmpd/playlist.rb b/libmpd/playlist.rb
index cc5db9f..d4be0b8 100644
--- a/libmpd/playlist.rb
+++ b/libmpd/playlist.rb
@@ -1,7 +1,7 @@
#!/usr/bin/env ruby
#
#--
-# Copyright 2009 David Vazgenovich Shakaryan <dvshakaryan@gmail.com>
+# Copyright 2009-2014 David Vazgenovich Shakaryan <dvshakaryan@gmail.com>
# Distributed under the terms of the GNU General Public License v3.
# See http://www.gnu.org/licenses/gpl.txt for the full license text.
#++
@@ -14,46 +14,46 @@ module MPDPlaylist
# Adds the specified file to the playlist.
# Directories add recursively.
def add(uri)
- return send_request('add "%s"' % uri)
+ send_request('add "%s"' % uri)
end
# Adds the specified file to the playlist and returns the song id.
# An optional playlist position can be specified.
- def addid(uri, position=nil)
+ def addid(uri, position = nil)
command = 'addid "%s"' % uri
command << ' %s' % position if position
- return send_request(command).scan(/\d+/).first.to_i
+ send_request(command).scan(/\d+/).first.to_i
end
# Clears the playlist.
def clear
- return send_request('clear')
+ send_request('clear')
end
# Deletes a song from the playlist.
def delete(songpos)
- return send_request('delete %s' % songpos)
+ send_request('delete %s' % songpos)
end
# Deletes a song from the playlist.
def deleteid(songid)
- return send_request('deleteid %s' % songid)
+ send_request('deleteid %s' % songid)
end
# Returns an Array composed of Hashes containing information about the songs
# in the playlist.
def playlist
- return split_and_hash(send_request('playlistinfo'))
+ split_and_hash(send_request('playlistinfo'))
end
# Swaps the positions of the given songs, specified by playlist positions.
- def swap first, second
- return send_request('swap %s %s' % [first, second])
+ def swap(first, second)
+ send_request('swap %s %s' % [first, second])
end
# Swaps the positions of the given songs, specified by song ids.
- def swapid first, second
- return send_request('swapid %s %s' % [first, second])
+ def swapid(first, second)
+ send_request('swapid %s %s' % [first, second])
end
end
diff --git a/libmpd/status.rb b/libmpd/status.rb
index 52c937c..d11c6ca 100644
--- a/libmpd/status.rb
+++ b/libmpd/status.rb
@@ -1,7 +1,7 @@
#!/usr/bin/env ruby
#
#--
-# Copyright 2009 David Vazgenovich Shakaryan <dvshakaryan@gmail.com>
+# Copyright 2009-2014 David Vazgenovich Shakaryan <dvshakaryan@gmail.com>
# Distributed under the terms of the GNU General Public License v3.
# See http://www.gnu.org/licenses/gpl.txt for the full license text.
#++
@@ -13,21 +13,21 @@
module MPDStatus
# Clears the error message in the status, if one exists.
def clearerror
- return send_request('clearerror')
+ send_request('clearerror')
end
# Returns information about the current song.
def current
- return generate_hash(send_request('currentsong'))
+ generate_hash(send_request('currentsong'))
end
# Returns daemon and database statistics.
def stats
- return generate_hash(send_request('stats'))
+ generate_hash(send_request('stats'))
end
# Returns the status.
def status
- return generate_hash(send_request('status'))
+ generate_hash(send_request('status'))
end
end