Class: Startback::Websocket::Hub::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/startback/websocket/hub/message.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, socket) ⇒ Message

Returns a new instance of Message.



7
8
9
10
11
12
# File 'lib/startback/websocket/hub/message.rb', line 7

def initialize(data, socket)
  data = JSON.parse(data, symbolize_names: true)
  @headers = data[:headers] || {}
  @body = data[:body] || {}
  @socket = socket
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



13
14
15
# File 'lib/startback/websocket/hub/message.rb', line 13

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



13
14
15
# File 'lib/startback/websocket/hub/message.rb', line 13

def headers
  @headers
end

#socketObject (readonly)

Returns the value of attribute socket.



13
14
15
# File 'lib/startback/websocket/hub/message.rb', line 13

def socket
  @socket
end

Instance Method Details

#reply(message) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/startback/websocket/hub/message.rb', line 15

def reply(message)
  raise "No reply-to header found" unless headers[:'reply-to']
  response = {
    headers: {
      :'in-reply-to' => headers[:'reply-to']
    },
    body: message
  }
  @socket.send(response.to_json)
end