summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobert Gerus <rgerus@dpd.com.pl>2013-09-02 16:33:31 +0200
committerRobert Gerus <rgerus@dpd.com.pl>2013-09-02 16:33:31 +0200
commitbae894c502bd8c1c81b392e11e1194e0cc3c5cb6 (patch)
tree8924ada1b85016962fa63f35fe790cd01dfd479c
parent02aade1618662e9e4db52e645f63928d0da3f88f (diff)
downloadzvolcreator-bae894c502bd8c1c81b392e11e1194e0cc3c5cb6.tar.gz
zvolcreator-bae894c502bd8c1c81b392e11e1194e0cc3c5cb6.tar.bz2
zvolcreator-bae894c502bd8c1c81b392e11e1194e0cc3c5cb6.zip
Some ideas... to be continued
-rw-r--r--lib/zvoltronapi/message.rb27
-rw-r--r--lib/zvoltronapi/talker.rb37
2 files changed, 64 insertions, 0 deletions
diff --git a/lib/zvoltronapi/message.rb b/lib/zvoltronapi/message.rb
new file mode 100644
index 0000000..2182ef1
--- /dev/null
+++ b/lib/zvoltronapi/message.rb
@@ -0,0 +1,27 @@
+require 'json'
+
+module ZVoltronApi
+
+ class Message < Hash
+
+ def verify
+ raise ArgumentError.new("Invalid message: no status") if not self["status"]
+ raise ArgumentError.new("Invalid message: no action") if not self["action"]
+ raise ArgumentError.new("Invalid message: no content") if not self["content"]
+ self
+ end
+
+ def initialize(status, action, content)
+ self["status"] = status
+ self["action"] = action
+ self["content"] = content
+ end
+
+ def self.parse(raw_message)
+ j = JSON.parse(raw_message)
+ self.new(j["status"], j["action"], j["content"]).verify
+ end
+
+ end
+
+end
diff --git a/lib/zvoltronapi/talker.rb b/lib/zvoltronapi/talker.rb
new file mode 100644
index 0000000..daee4af
--- /dev/null
+++ b/lib/zvoltronapi/talker.rb
@@ -0,0 +1,37 @@
+require 'json'
+require 'message'
+
+module ZVoltronApi
+
+ class Talker
+
+ def send_message(h)
+ @socket.puts(h.to_json)
+ end
+
+ def read_message
+ Message.parse(@socket.gets)
+ end
+
+ def expect(message, expectations)
+ raise ArgumentError.new("Not a message") if not message.kind_of? Message
+ raise ArgumentError.new("Expectations not a hash") if not expectations.kind_of? Hash
+ raise ArgumentError.new("Empty expectations") if expectations.empty?
+
+ @socket.send_message(message)
+ response = @socket.read_message
+
+ expectations.keys.each do |key|
+ raise ArgumentError.new("Expectation failed: #{key}") if not expectations[key].call(response[key])
+ end
+
+ response
+ end
+
+ def initialize(socket)
+ @socket = socket
+ end
+
+ end
+
+end