class Meshchat::Network::Message::Factory

Constants

TYPES

The left side of this map is all the lowercase un-underscored variant of the constant name

e.g.: NODE_LIST == 'nodelist'

Attributes

_common_parameters[RW]
_dispatcher[RW]

the message dispatcher that is responsible for dispatching messages across either network

Public Class Methods

new(dispatcher) click to toggle source
# File lib/meshchat/network/message/factory.rb, line 29
def initialize(dispatcher)
  @_dispatcher        = dispatcher
  @_common_parameters = {
    message_dispatcher: _dispatcher,
    message_factory:    self
  }
end

Public Instance Methods

create(type = '', data: {}) click to toggle source

If data contains the payload key, we are receiving the message. If data does not caine the payload key, we are buliding the message to send

# File lib/meshchat/network/message/factory.rb, line 40
def create(type = '', data: {})
  return Debug.message_type_not_found(type + 'not found') if type.blank?
  data = data.deep_symbolize_keys

  parameters = parameters_for(data)
  klass = TYPES[type]
  raise Errors::MessageTypeNotRecognized, type + ' not found' unless klass
  klass.new(parameters)
end
is_receiving?(data) click to toggle source
# File lib/meshchat/network/message/factory.rb, line 58
def is_receiving?(data)
  data[:payload].present?
end
parameters_for(data) click to toggle source
# File lib/meshchat/network/message/factory.rb, line 50
def parameters_for(data)
  if is_receiving?(data)
    receiving_parameters_for(data)
  else
    sending_parameters_for(data)
  end
end
receiving_parameters_for(data) click to toggle source

ensures a payload exists, as well as assigns the message dispatcher and message factory

# File lib/meshchat/network/message/factory.rb, line 64
def receiving_parameters_for(data)
  { payload: data[:payload] }.merge(_common_parameters)
end
sending_parameters_for(data) click to toggle source
# File lib/meshchat/network/message/factory.rb, line 68
def sending_parameters_for(data)
  data.merge(
    message: data[:message],
    sender: {
      'alias'    => APP_CONFIG.user['alias'],
      'location' => APP_CONFIG.user.location,
      'uid'      => APP_CONFIG.user['uid']
    }
  )
end