Browse Source

Add the error response type to the library

master
Nick Thomas 11 years ago
parent
commit
3f8b74005d
  1. 2
      lib/qmp_client/messages.rb
  2. 48
      lib/qmp_client/messages/error.rb
  3. 8
      test/unit/qmp_client/test_messages.rb
  4. 4
      test/unit/test_qmp_client.rb

2
lib/qmp_client/messages.rb

@ -2,6 +2,7 @@ require 'json'
require 'qmp_client/messages/command'
require 'qmp_client/messages/greeting'
require 'qmp_client/messages/error'
require 'qmp_client/messages/event'
require 'qmp_client/messages/query'
require 'qmp_client/messages/reply'
@ -36,6 +37,7 @@ module QMPClient
return Event::build(hsh) if Event::represents?(hsh)
return Command::build(hsh) if Command::represents?(hsh)
return Query::build(hsh) if Query::represents?(hsh)
return Error::build(hsh) if Error::represents?(hsh)
return Greeting::build(hsh) if Greeting::represents?(hsh)

48
lib/qmp_client/messages/error.rb

@ -0,0 +1,48 @@
require 'qmp_client/messages/message'
module QMPClient
module Messages
class Error < Message
attr_reader :request_id
attr_reader :error_class
attr_reader :data
attr_reader :desc
# Does the hash represent a Command?
def self.represents?(hsh)
super(hsh) &&
(hsh.keys - %w|id error|).empty? && hsh['error'].is_a?(Hash) &&
hsh['error']['class'].is_a?(String) &&
hsh['error']['data'].is_a?(Hash) &&
hsh['error']['desc'].is_a?(String)
end
def ==(other)
other.is_a?(Error) && [
:request_id, :error_class, :data, :desc
].all? {|m| self.send(m) == other.send(m) }
end
def self.build(hsh)
err = hsh['error']
new(hsh['id'], err['class'], err['data'], err['desc'])
end
def initialize(request_id, err_kls, data, desc)
@request_id = request_id
@error_class = err_kls
@data = data
@desc = desc
end
def to_hash
{ 'id' => request_id,
'error' => {
'class' => error_class, 'data' => data, 'desc' => desc
}
}
end
end
end
end

8
test/unit/qmp_client/test_messages.rb

@ -24,6 +24,13 @@ module TestQMPClient
}}
GREETING_TXT = GREETING_HSH.to_json
ERROR_MSG = Messages::Error.new('00000001', 'foo_kls', {}, 'foodesc')
ERROR_HSH = { 'id' => '00000001', 'error' => {
'class' => 'foo_kls', 'data' => {}, 'desc' => 'foodesc'
}
}
ERROR_TXT = ERROR_HSH.to_json
ETIME = Time.now
EVENT_MSG = Messages::Event.new('foo', ETIME, {'a' => 'b'})
EVENT_HSH = {
@ -50,6 +57,7 @@ module TestQMPClient
[
[Messages::Command, COMMAND_MSG, COMMAND_HSH, COMMAND_TXT],
[Messages::Greeting, GREETING_MSG, GREETING_HSH, GREETING_TXT],
[Messages::Error, ERROR_MSG, ERROR_HSH, ERROR_TXT],
[Messages::Event, EVENT_MSG, EVENT_HSH, EVENT_TXT],
[Messages::Query, QUERY_MSG, QUERY_HSH, QUERY_TXT],
[Messages::Reply, REPLY_MSG, REPLY_HSH, REPLY_TXT]

4
test/unit/test_qmp_client.rb

@ -16,7 +16,9 @@ module TestQMPClient
assert(defined?(::QMPClient::Messages::Query), "Query message not defined")
assert(defined?(::QMPClient::Messages::Command), "Command message not defined")
assert(defined?(::QMPClient::Messages::Event), "Event message not defined")
assert(defined?(::QMPClient::Messages::Reply), "Greeting message not defined")
assert(defined?(::QMPClient::Messages::Error), "Error message not defined")
assert(defined?(::QMPClient::Messages::Greeting), "Greeting message not defined")
assert(defined?(::QMPClient::Messages::Reply), "Reply message not defined")
end
def expects_socket_run(rio, wio=nil)

Loading…
Cancel
Save