Class: MeshChat::Message::Base

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

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message: '', sender_name: '', sender_location: '', sender_uid: '', time_recieved: nil, payload: nil) ⇒ Base

Returns a new instance of Base.

Parameters:

  • message (String) (defaults to: '')
  • name_of_sender (String)
  • location (String)

    the location of the sender

  • payload (Hash) (defaults to: nil)

    optionally overrides the default payload



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/meshchat/message/base.rb', line 19

def initialize(
  message: '',
  sender_name: '',
  sender_location: '',
  sender_uid: '',
  time_recieved: nil,
  payload: nil)

  @payload = payload.presence

  # TODO: find a more elegant way to represent this
  self.message = message.presence || @payload.try(:[], 'message')
  self.sender_name = sender_name.presence || @payload.try(:[], 'sender').try(:[], 'alias') || Settings['alias']
  self.sender_location = sender_location.presence || @payload.try(:[], 'sender').try(:[], 'location') || Settings.location
  self.sender_uid = sender_uid.presence || @payload.try(:[], 'sender').try(:[], 'uid') || Settings['uid']
  self.time_recieved = time_recieved.presence || Time.now

end

Instance Attribute Details

#messageObject

Returns the value of attribute message.



12
13
14
# File 'lib/meshchat/message/base.rb', line 12

def message
  @message
end

#payloadObject

Returns the value of attribute payload.



12
13
14
# File 'lib/meshchat/message/base.rb', line 12

def payload
  @payload
end

#sender_locationObject

Returns the value of attribute sender_location.



12
13
14
# File 'lib/meshchat/message/base.rb', line 12

def sender_location
  @sender_location
end

#sender_nameObject

Returns the value of attribute sender_name.



12
13
14
# File 'lib/meshchat/message/base.rb', line 12

def sender_name
  @sender_name
end

#sender_uidObject

Returns the value of attribute sender_uid.



12
13
14
# File 'lib/meshchat/message/base.rb', line 12

def sender_uid
  @sender_uid
end

#time_recievedObject

Returns the value of attribute time_recieved.



12
13
14
# File 'lib/meshchat/message/base.rb', line 12

def time_recieved
  @time_recieved
end

Instance Method Details

#clientObject



57
58
59
# File 'lib/meshchat/message/base.rb', line 57

def client
  MeshChat.name
end

#client_versionObject



61
62
63
# File 'lib/meshchat/message/base.rb', line 61

def client_version
  MeshChat.version
end

#displayObject

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



67
68
69
# File 'lib/meshchat/message/base.rb', line 67

def display
  message
end

#handleObject

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



75
76
77
# File 'lib/meshchat/message/base.rb', line 75

def handle
  display
end

#renderObject

this message should be called immediately before sending to the whomever



88
89
90
# File 'lib/meshchat/message/base.rb', line 88

def render
  payload.to_json
end

#respondObject

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



82
83
84
# File 'lib/meshchat/message/base.rb', line 82

def respond
  return
end

#typeObject



53
54
55
# File 'lib/meshchat/message/base.rb', line 53

def type
  @type ||= TYPES.invert[self.class]
end