Class: ActionCableClient::MessageFactory

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(channel) ⇒ MessageFactory

Returns a new instance of MessageFactory.

Parameters:

  • channel (String)
    • the name of the subscribed channel



7
8
9
# File 'lib/action_cable_client/message_factory.rb', line 7

def initialize(channel)
  @_channel = channel
end

Instance Attribute Details

#_channelObject (readonly)

Returns the value of attribute _channel.



4
5
6
# File 'lib/action_cable_client/message_factory.rb', line 4

def _channel
  @_channel
end

Instance Method Details

#build_data(action, message) ⇒ Hash

Returns The data that will be included in the message.

Parameters:

  • action (String)
    • the action that is performed to send this message

  • message (Hash)
    • the data to send

Returns:

  • (Hash)

    The data that will be included in the message



22
23
24
# File 'lib/action_cable_client/message_factory.rb', line 22

def build_data(action, message)
  message.merge(action: action) if message.is_a?(Hash)
end

#create(command, action = '', message = nil) ⇒ Object

Parameters:

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

  • action (String) (defaults to: '')
    • the action that is performed to send this message

  • message (Hash) (defaults to: nil)
    • the data to send



14
15
16
17
# File 'lib/action_cable_client/message_factory.rb', line 14

def create(command, action = '', message = nil)
  data = build_data(action, message)
  Message.new(command, identifier, data)
end

#identifierObject

the ending result should look like “”channel“:”RoomChannel“” but that’s up to the Mesage to format it



29
30
31
# File 'lib/action_cable_client/message_factory.rb', line 29

def identifier
  { channel: _channel }
end