class Meshchat::Network::Message::Base

NOTE:

#display: shows the message
          should be used locally, before *sending* a message
#handle: processing logic for the message
         should be used when receiving a message, and there
         needs to be a response right away
#respond: where the actual logic for the response goes

Attributes

_message[RW]
_message_dispatcher[RW]
_message_factory[RW]
_sender_location[RW]
_sender_name[RW]
_sender_uid[RW]
_time_received[RW]
payload[RW]

Public Class Methods

new( message: '', sender: {}, payload: {}, message_dispatcher: nil, message_factory: nil ) click to toggle source

@param [String] message @param [Hash] sender @param [Hash] payload all paramaters for a received message @param [MeshChat::Network::Dispatcher] message_dispatcher optionally overrides the default payload @param [MeshChat::Message::Factory] message_factory the message factory

# File lib/meshchat/network/message/base.rb, line 23
def initialize(
  message:            '',
  sender:             {},
  payload:            {},
  message_dispatcher: nil,
  message_factory:    nil
)

  if payload.present?
    @payload = payload.deep_stringify_keys
  else
    @_message         = message
    @_sender_name     = sender['alias']
    @_sender_location = sender['location']
    @_sender_uid      = sender['uid']
    @_time_received   = Time.now.iso8601
  end

  @_message_dispatcher = message_dispatcher
  @_message_factory    = message_factory
end

Public Instance Methods

client() click to toggle source
# File lib/meshchat/network/message/base.rb, line 92
def client
  APP_CONFIG[:client_name]
end
client_version() click to toggle source
# File lib/meshchat/network/message/base.rb, line 96
def client_version
  APP_CONFIG[:client_version]
end
display() click to toggle source

shows the message should be used locally, before sending a message

# File lib/meshchat/network/message/base.rb, line 102
def display
  {
    time:    time_received_as_date,
    from:    sender_name,
    message: message
  }
end
encrypt_for(node) click to toggle source
# File lib/meshchat/network/message/base.rb, line 131
def encrypt_for(node)
  result     = jsonized_payload
  public_key = node.public_key
  result     = Encryption.encrypt(result, public_key) if node.public_key
  Base64.strict_encode64(result)
end
handle() click to toggle source

processing logic for the message should be used when receiving a message, and there needs to be a response right away. this may call display, if the response is always to be displayed

# File lib/meshchat/network/message/base.rb, line 114
def handle
  display
end
jsonized_payload()
Alias for: render
message() click to toggle source
# File lib/meshchat/network/message/base.rb, line 64
def message
  _message || payload['message']
end
render() click to toggle source

this message should be called immediately before sending to the whomever

# File lib/meshchat/network/message/base.rb, line 125
def render
  payload.to_json
end
Also aliased as: jsonized_payload
respond() click to toggle source

Most message types aren't going to need to have an immediate response.

# File lib/meshchat/network/message/base.rb, line 120
def respond
end
sender() click to toggle source
# File lib/meshchat/network/message/base.rb, line 68
def sender
  payload['sender']
end
sender_location() click to toggle source
# File lib/meshchat/network/message/base.rb, line 76
def sender_location
  _sender_location || sender['location']
end
sender_name() click to toggle source
# File lib/meshchat/network/message/base.rb, line 72
def sender_name
  _sender_name || sender['alias']
end
sender_uid() click to toggle source
# File lib/meshchat/network/message/base.rb, line 80
def sender_uid
  _sender_uid || sender['uid']
end
time_received() click to toggle source
# File lib/meshchat/network/message/base.rb, line 84
def time_received
  _time_received || payload['time_sent']
end
time_received_as_date() click to toggle source
# File lib/meshchat/network/message/base.rb, line 88
def time_received_as_date
  DateTime.parse(time_received) if time_received
end
type() click to toggle source
# File lib/meshchat/network/message/base.rb, line 60
def type
  @type ||= Factory::TYPES.invert[self.class]
end