Class: Bixby::WebSocket::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/bixby-common/websocket/message.rb

Direct Known Subclasses

Request, Response

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#bodyObject (readonly)

Returns the value of attribute body.



7
8
9
# File 'lib/bixby-common/websocket/message.rb', line 7

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



7
8
9
# File 'lib/bixby-common/websocket/message.rb', line 7

def headers
  @headers
end

#idObject (readonly)

Returns the value of attribute id.



7
8
9
# File 'lib/bixby-common/websocket/message.rb', line 7

def id
  @id
end

#typeObject (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_sString

Convert object to String, useful for debugging

Returns:

  • (String)


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_wireObject



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