Class: MPD::Protocol::Response

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/mpd/protocol/response.rb

Direct Known Subclasses

Error, Ok

Defined Under Namespace

Classes: Data

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command, data) ⇒ Response

Returns a new instance of Response.



75
76
77
78
# File 'lib/mpd/protocol/response.rb', line 75

def initialize (command, data)
	@command  = command
	@internal = data.freeze
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



73
74
75
# File 'lib/mpd/protocol/response.rb', line 73

def command
  @command
end

Class Method Details

.parse(text) ⇒ Object



67
68
69
# File 'lib/mpd/protocol/response.rb', line 67

def self.parse (text)
	read(StringIO.new(text))
end

.read(io, command) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/mpd/protocol/response.rb', line 47

def self.read (io, command)
	result = []

	while (line = io.readline) && !line.match(/^(list_)?OK(\s|$)/) && !line.start_with?('ACK ')
		name, value = line.split ': ', 2
		name        = name.to_sym
		value       = value.chomp

		result << Data.new(name, value)
	end

	type, message = line.split ' ', 2

	if type == 'OK' || type == 'list_OK'
		Ok.new(command, result, message)
	else
		Error.new(command, result, *Error.parse(message))
	end
end

Instance Method Details

#eachObject



80
81
82
83
84
85
86
87
88
# File 'lib/mpd/protocol/response.rb', line 80

def each
	return to_enum unless block_given?

	@internal.each {|data|
		yield [data.name, data.value]
	}

	self
end

#empty?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/mpd/protocol/response.rb', line 90

def empty?
	to_a.empty?
end

#to_aObject



94
95
96
# File 'lib/mpd/protocol/response.rb', line 94

def to_a
	@internal
end

#to_hashObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/mpd/protocol/response.rb', line 98

def to_hash
	return @hash if @hash

	result = {}

	each {|name, value|
		if result[name]
			if result[name].is_a? Array
				result[name] << value
			else
				result[name] = [result[name], value]
			end
		else
			result[name] = value
		end
	}

	@hash = result.freeze
end