From 602cbf21d35ba898841cac0f3c458bd90b6b6841 Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Sun, 6 Jul 2014 16:09:47 -0700 Subject: Style changes and simpler code. --- libmpd.rb | 48 ++++++++++++++++++++++-------------------------- 1 file changed, 22 insertions(+), 26 deletions(-) (limited to 'libmpd.rb') 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 +# Copyright 2009-2014 David Vazgenovich Shakaryan # 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 -- cgit v1.2.3-70-g09d2