Class: Meshchat::Network::Message::Factory

Inherits:
Object
  • Object
show all
Defined in:
lib/meshchat/network/message/factory.rb

Constant Summary collapse

TYPES =

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

e.g.: NODE_LIST == ‘nodelist’

{
  CHAT           => Chat,
  EMOTE          => Emote,
  ROLL           => Emote,
  WHISPER        => Whisper,
  DISCONNECT     => Disconnect,
  PING           => Ping,
  PING_REPLY     => PingReply,
  NODE_LIST      => NodeList,
  NODE_LIST_DIFF => NodeListDiff,
  NODE_LIST_HASH => NodeListHash
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dispatcher) ⇒ Factory

Returns a new instance of Factory.



29
30
31
32
33
34
35
# File 'lib/meshchat/network/message/factory.rb', line 29

def initialize(dispatcher)
  @_dispatcher        = dispatcher
  @_common_parameters = {
    message_dispatcher: _dispatcher,
    message_factory:    self
  }
end

Instance Attribute Details

#_common_parametersObject

Returns the value of attribute _common_parameters.



27
28
29
# File 'lib/meshchat/network/message/factory.rb', line 27

def _common_parameters
  @_common_parameters
end

#_dispatcherObject

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



26
27
28
# File 'lib/meshchat/network/message/factory.rb', line 26

def _dispatcher
  @_dispatcher
end

Instance Method Details

#create(type = '', data: {}) ⇒ Object

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



40
41
42
43
44
45
46
47
48
# 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) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/meshchat/network/message/factory.rb', line 58

def is_receiving?(data)
  data[:payload].present?
end

#parameters_for(data) ⇒ Object



50
51
52
53
54
55
56
# 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) ⇒ Object

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



64
65
66
# 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) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# 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