Class: Bixby::WebSocket::Message
- Inherits:
-
Object
- Object
- Bixby::WebSocket::Message
- Defined in:
- lib/bixby-common/websocket/message.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(id = nil, type = "rpc", headers = nil) ⇒ Message
constructor
A new instance of Message.
-
#to_s ⇒ String
Convert object to String, useful for debugging.
- #to_wire ⇒ Object
Constructor Details
#initialize(id = nil, type = "rpc", headers = nil) ⇒ Message
Returns a new instance of Message.
9 10 11 12 13 |
# File 'lib/bixby-common/websocket/message.rb', line 9 def initialize(id=nil, type="rpc", headers=nil) @id = id || SecureRandom.uuid @type = type @headers = headers || {} end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
7 8 9 |
# File 'lib/bixby-common/websocket/message.rb', line 7 def body @body end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
7 8 9 |
# File 'lib/bixby-common/websocket/message.rb', line 7 def headers @headers end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
7 8 9 |
# File 'lib/bixby-common/websocket/message.rb', line 7 def id @id end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
7 8 9 |
# File 'lib/bixby-common/websocket/message.rb', line 7 def type @type end |
Class Method Details
.from_wire(body) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/bixby-common/websocket/message.rb', line 15 def self.from_wire(body) obj = MultiJson.load(body) clazz = case obj["type"] when "rpc", "connect" Request when "rpc_result" Response end req = clazz.allocate req.instance_eval do @id = obj["id"] @type = obj["type"] @body = obj["data"] if obj.include? "headers" then @headers = obj["headers"] end end return req end |
Instance Method Details
#to_s ⇒ String
Convert object to String, useful for debugging
50 51 52 53 54 55 56 57 58 |
# File 'lib/bixby-common/websocket/message.rb', line 50 def to_s # :nocov: s = [] s << "#{self.class}:#{self.object_id}" s << " id: #{self.id}" s << " type: #{self.type}" s << " headers: " + Debug.pretty_hash(self.headers) s << " body: " + Debug.pretty_hash(MultiJson.load(self.body)) s.join("\n") end |
#to_wire ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/bixby-common/websocket/message.rb', line 38 def to_wire hash = { :type => @type, :id => @id, :headers => @headers, :data => @body } MultiJson.dump(hash) end |