Class: MPD::Client

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

Overview

The MPD::Client is used for interactions with a MPD server.

Example:

“‘ruby require ’mpd_client’ require ‘logger’

client = MPD::Client.new client.log = Logger.new($stderr) client.connect(‘/var/run/mpd/socket’) “‘

Constant Summary collapse

VERSION =
'0.2.0'

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



177
178
179
180
# File 'lib/mpd_client.rb', line 177

def initialize
  @mutex = Mutex.new
  reset
end

Class Attribute Details

.logObject

Default logger for all MPD::Client` instances

“‘ruby MPD::Client.log = Logger.new($stderr) “`



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

def log
  @log
end

Instance Attribute Details

#logObject

The current logger. If no logger has been set MPD::Client.log is used



239
240
241
# File 'lib/mpd_client.rb', line 239

def log
  @log || MPD::Client.log
end

#mpd_versionObject (readonly)

Returns the value of attribute mpd_version.



143
144
145
# File 'lib/mpd_client.rb', line 143

def mpd_version
  @mpd_version
end

Class Method Details

.add_command(name, retval) ⇒ Object



160
161
162
163
164
165
166
167
168
# File 'lib/mpd_client.rb', line 160

def add_command(name, retval)
  escaped_name = name.tr(' ', '_')

  define_method escaped_name.to_sym do |*args|
    ensure_connected

    execute(name, *args, retval)
  end
end

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



153
154
155
156
157
158
# File 'lib/mpd_client.rb', line 153

def connect(host = 'localhost', port = 6600)
  client = MPD::Client.new
  client.connect(host, port)

  client
end

.remove_command(name) ⇒ Object



170
171
172
173
174
# File 'lib/mpd_client.rb', line 170

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

#albumart(uri) ⇒ Object



246
247
248
# File 'lib/mpd_client.rb', line 246

def albumart(uri)
  fetch_binary(StringIO.new, 0, 'albumart', uri)
end

#command_list_endObject



230
231
232
233
234
235
236
# File 'lib/mpd_client.rb', line 230

def command_list_end
  raise 'Not in command list' if @command_list.nil?

  write_command('command_list_end')

  fetch_command_list
end

#command_list_ok_beginObject



222
223
224
225
226
227
228
# File 'lib/mpd_client.rb', line 222

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



182
183
184
185
186
187
# File 'lib/mpd_client.rb', line 182

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

  reconnect
end

#connected?Boolean

Returns:

  • (Boolean)


217
218
219
# File 'lib/mpd_client.rb', line 217

def connected?
  @connected
end

#disconnectObject



203
204
205
206
207
# File 'lib/mpd_client.rb', line 203

def disconnect
  log&.info('MPD disconnect')
  @socket.close
  reset
end

#readpicture(uri) ⇒ Object



250
251
252
# File 'lib/mpd_client.rb', line 250

def readpicture(uri)
  fetch_binary(StringIO.new, 0, 'readpicture', uri)
end

#reconnectObject



189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/mpd_client.rb', line 189

def reconnect
  log&.info("MPD (re)connect #{@host}, #{@port}")

  @socket =
    if @host.start_with?('/')
      UNIXSocket.new(@host)
    else
      TCPSocket.new(@host, @port)
    end

  hello
  @connected = true
end

#resetObject



209
210
211
212
213
214
215
# File 'lib/mpd_client.rb', line 209

def reset
  @mpd_version = nil
  @command_list = nil
  @socket = nil
  @log = nil
  @connected = false
end