class MPD::ServerResponse

Just a frozen string with some useful methods

Public Class Methods

from_connection(conn) click to toggle source

Reads data from `MPD::Connection` instance to next OK/ACK

# File lib/mpd/server_response.rb, line 7
def self.from_connection(conn)
  line = ''
  buffer = ''
  while line != "OK\n" && !line.start_with?('ACK')
    line = conn.gets
    raise(ConnectionError) if line.nil?
    buffer << line
  end

  new(buffer.force_encoding('UTF-8'))
end
new(response_text) click to toggle source
Calls superclass method
# File lib/mpd/server_response.rb, line 19
def initialize(response_text)
  super
  freeze
end

Public Instance Methods

content() click to toggle source

text without status

# File lib/mpd/server_response.rb, line 49
def content
  lines[0..-2].join
end
error?() click to toggle source
# File lib/mpd/server_response.rb, line 58
def error?
  status.start_with?('ACK')
end
key_value_pairs() click to toggle source

MPD often sends data as:

id: 1
title: some title
id: 2
title: another title

So this method comes useful.

# File lib/mpd/server_response.rb, line 38
def key_value_pairs
  content.split("\n").map do |line|
    index = line.index(':')
    raise "can't create key-value pair" unless index
    key = line[0...index]
    value = line[(index + 1)..-1].strip
    [key, value]
  end
end
status() click to toggle source

last line of response

# File lib/mpd/server_response.rb, line 54
def status
  lines.last.chomp
end
to_h() click to toggle source
# File lib/mpd/server_response.rb, line 24
def to_h
  key_value_pairs.to_h
end