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.1'

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.



180
181
182
183
# File 'lib/mpd_client.rb', line 180

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) “`



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

def log
  @log
end

Instance Attribute Details

#logObject

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



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

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

#mpd_versionObject (readonly)

Returns the value of attribute mpd_version.



146
147
148
# File 'lib/mpd_client.rb', line 146

def mpd_version
  @mpd_version
end

Class Method Details

.add_command(name, retval) ⇒ Object



163
164
165
166
167
168
169
170
171
# File 'lib/mpd_client.rb', line 163

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, timeout: nil) ⇒ Object



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

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

  client
end

.remove_command(name) ⇒ Object



173
174
175
176
177
# File 'lib/mpd_client.rb', line 173

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



258
259
260
# File 'lib/mpd_client.rb', line 258

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

#command_list_endObject



242
243
244
245
246
247
248
# File 'lib/mpd_client.rb', line 242

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



234
235
236
237
238
239
240
# File 'lib/mpd_client.rb', line 234

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, timeout: nil) ⇒ Object



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

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

  reconnect
end

#connected?Boolean

Returns:

  • (Boolean)


229
230
231
# File 'lib/mpd_client.rb', line 229

def connected?
  @connected
end

#disconnectObject



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

def disconnect
  log&.info('MPD disconnect')
  begin
    Timeout.timeout(@timeout) do
      @socket.close
    end
  rescue Timeout::Error
    @socket = nil
  end
  reset
end

#readpicture(uri) ⇒ Object



262
263
264
# File 'lib/mpd_client.rb', line 262

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

#reconnectObject



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/mpd_client.rb', line 193

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

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

  hello
  @connected = true
end

#resetObject



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

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