Class: MPDClient

Inherits:
Object
  • Object
show all
Defined in:
lib/mpd_client.rb,
lib/mpd_client/version.rb

Overview

The MPDClient library is used for interactions with a MPD.

Example

require 'mpd_client'
require 'logger'

client = MPDClient.new
client.log = Logger.new($stderr)
client.connect('/var/run/mpd/socket')

Constant Summary collapse

VERSION =
"0.0.6"

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMPDClient

Returns a new instance of MPDClient.



163
164
165
166
# File 'lib/mpd_client.rb', line 163

def initialize
  @mutex = Mutex.new
  reset
end

Class Attribute Details

.logObject

Default logger for all MPDClient instances

MPDClient.log = Logger.new($stderr)


148
149
150
# File 'lib/mpd_client.rb', line 148

def log
  @log
end

Instance Attribute Details

#mpd_versionObject (readonly)

Returns the value of attribute mpd_version.



141
142
143
# File 'lib/mpd_client.rb', line 141

def mpd_version
  @mpd_version
end

Class Method Details

.add_command(name, retval) ⇒ Object



150
151
152
153
154
155
# File 'lib/mpd_client.rb', line 150

def add_command(name, retval)
  escaped_name = name.gsub(' ', '_')
  define_method escaped_name.to_sym do |*args|
    execute(name, *args, retval)
  end
end

.remove_command(name) ⇒ Object



157
158
159
160
# File 'lib/mpd_client.rb', line 157

def remove_command(name)
  raise "Can't remove not existent '#{name}' command" unless method_defined? name.to_sym
  remove_method name.to_sym
end

Instance Method Details

#command_list_endObject



198
199
200
201
202
203
# File 'lib/mpd_client.rb', line 198

def command_list_end
  raise "Not in command list" if @command_list.nil?
  write_command('command_list_end')

  return fetch_command_list
end

#command_list_ok_beginObject



192
193
194
195
196
# File 'lib/mpd_client.rb', line 192

def command_list_ok_begin
  raise "Already in command list" unless @command_list.nil?
  write_command('command_list_ok_begin')
  @command_list = []
end

#connect(host = 'localhost', port = 6600) ⇒ Object



168
169
170
171
172
# File 'lib/mpd_client.rb', line 168

def connect(host = 'localhost', port = 6600)
  @host = host
  @port = port
  reconnect
end

#disconnectObject



185
186
187
188
189
# File 'lib/mpd_client.rb', line 185

def disconnect
  log.info("MPD disconnect") if log
  @socket.close
  reset
end

#logObject

The current logger. If no logger has been set MPDClient.log is used



207
208
209
# File 'lib/mpd_client.rb', line 207

def log
  @log || MPDClient.log
end

#log=(logger) ⇒ Object

Sets the logger used by this instance of MPDClient



213
214
215
# File 'lib/mpd_client.rb', line 213

def log= logger
  @log = logger
end

#reconnectObject



174
175
176
177
178
179
180
181
182
183
# File 'lib/mpd_client.rb', line 174

def reconnect
  log.info("MPD (re)connect #{@host}, #{@port}") if log
  if @host.start_with?('/')
    @socket = UNIXSocket.new(@host)
    hello
  else
    @socket = TCPSocket.new(@host, @port)
    hello
  end
end