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



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

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



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

def log
  @log
end

Instance Attribute Details

#logObject

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



234
235
236
# File 'lib/mpd_client.rb', line 234

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

#mpd_versionObject (readonly)

Returns the value of attribute mpd_version.



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

def mpd_version
  @mpd_version
end

Class Method Details

.add_command(name, retval) ⇒ Object



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

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



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

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

  client
end

.remove_command(name) ⇒ Object



170
171
172
173
# 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

#command_list_endObject



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

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



220
221
222
223
224
# File 'lib/mpd_client.rb', line 220

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



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

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

  reconnect
end

#connected?Boolean

Returns:

  • (Boolean)


215
216
217
# File 'lib/mpd_client.rb', line 215

def connected?
  @connected
end

#disconnectObject



201
202
203
204
205
# File 'lib/mpd_client.rb', line 201

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

#reconnectObject



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

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



207
208
209
210
211
212
213
# File 'lib/mpd_client.rb', line 207

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