Class: ActionCableClient::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/action_cable_client/message.rb

Constant Summary collapse

IDENTIFIER_KEY =
'identifier'.freeze
IDENTIFIER_PING =
'_ping'.freeze
TYPE_KEY =

Type is never sent, but is received TODO: find a better place for this constant

'type'.freeze
TYPE_CONFIRM_SUBSCRIPTION =
'confirm_subscription'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command, identifier, data) ⇒ Message

Returns a new instance of Message.

Parameters:

  • command (String)
    • the type of message that this is

  • identifier (Hash)
    • the channel we are subscribed to

  • data (Hash)
    • the data to be sent in this message



17
18
19
20
21
# File 'lib/action_cable_client/message.rb', line 17

def initialize(command, identifier, data)
  @_command = command
  @_identifier = identifier
  @_data = data
end

Instance Attribute Details

#_commandObject (readonly)

Returns the value of attribute _command.



12
13
14
# File 'lib/action_cable_client/message.rb', line 12

def _command
  @_command
end

#_dataObject (readonly)

Returns the value of attribute _data.



12
13
14
# File 'lib/action_cable_client/message.rb', line 12

def _data
  @_data
end

#_identifierObject (readonly)

Returns the value of attribute _identifier.



12
13
14
# File 'lib/action_cable_client/message.rb', line 12

def _identifier
  @_identifier
end

Instance Method Details

#to_jsonObject



23
24
25
26
27
28
29
30
31
32
# File 'lib/action_cable_client/message.rb', line 23

def to_json
  hash = {
    command: _command,
    identifier: _identifier.to_json
  }

  hash[:data] = _data.to_json if _data.present?

  hash.to_json
end