diff options
author | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2014-07-06 16:09:47 -0700 |
---|---|---|
committer | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2014-07-06 16:09:47 -0700 |
commit | 602cbf21d35ba898841cac0f3c458bd90b6b6841 (patch) | |
tree | 0d8b282562d866ff70d833666a383a50d6714f4e /libmpd.rb | |
parent | 4ebf4d0b5e8de5a79efa3811eb35ac8b85f26b07 (diff) | |
download | ruby-libmpd-602cbf21d35ba898841cac0f3c458bd90b6b6841.tar.gz ruby-libmpd-602cbf21d35ba898841cac0f3c458bd90b6b6841.tar.xz |
Style changes and simpler code.
Diffstat (limited to 'libmpd.rb')
-rw-r--r-- | libmpd.rb | 48 |
1 files changed, 22 insertions, 26 deletions
@@ -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 |