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 or Hash)
    • the name of the subscribed channel, or

    a hash that includes the :channel key and any other params to send.



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/action_cable_client/message_factory.rb', line 9

def initialize(channel)
  # the ending result should look like
  # "{"channel":"RoomChannel"}" but that's up to
  # the Mesage to format it
  @channel = channel
  @identifier =
    case channel
    when String then { channel: channel }
    when Hash then channel
    else
      raise ActionCableClient::Errors::ChannelNotSpecified, 'channel is invalid'
    end
end

Instance Attribute Details

#channelObject (readonly)

Returns the value of attribute channel.



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

def channel
  @channel
end

#identifierObject (readonly)

Returns the value of attribute identifier.



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

def identifier
  @identifier
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



34
35
36
# File 'lib/action_cable_client/message_factory.rb', line 34

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



26
27
28
29
# File 'lib/action_cable_client/message_factory.rb', line 26

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