Class: MPD::ServerResponse

Inherits:
String
  • Object
show all
Defined in:
lib/mpd/server_response.rb

Overview

Just a frozen string with some useful methods

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response_text) ⇒ ServerResponse



18
19
20
21
# File 'lib/mpd/server_response.rb', line 18

def initialize(response_text)
  super
  freeze
end

Class Method Details

.from_connection(conn) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/mpd/server_response.rb', line 6

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

Instance Method Details

#contentObject

text without status



38
39
40
# File 'lib/mpd/server_response.rb', line 38

def content
  lines[0..-2].join
end

#error?Boolean



46
47
48
# File 'lib/mpd/server_response.rb', line 46

def error?
  status.start_with?('ACK')
end

#key_value_pairsObject



27
28
29
30
31
32
33
34
35
# File 'lib/mpd/server_response.rb', line 27

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

#statusObject



42
43
44
# File 'lib/mpd/server_response.rb', line 42

def status
  lines.last.chomp
end

#to_hObject



23
24
25
# File 'lib/mpd/server_response.rb', line 23

def to_h
  key_value_pairs.to_h
end