Class: Rodbot::Message
- Inherits:
-
Object
- Object
- Rodbot::Message
- Defined in:
- lib/rodbot/message.rb
Overview
Generic serializable chat message container
The primary purpose of message objects is their ability to contain both the actual message text as well as meta data such as the room in which the message was or will be posted.
Furthermore, they can be serialized (dump
) and then recreated (new
) from that. You can also create a serialized message string outside of RodBot performing the following steps:
-
Create a JSON hash which contains the keys
class
(with static valueRodbot::Message
),text
and optionallyroom
. -
Encode it as Base64 without newlines.
-
Prefix the result with Serializer::PRELUDE.
Example for Shell:
string='{"class":"Rodbot::Message",text":"hello, world","room":"general"}'
string=$(echo $string | base64)
string="data:application/json;base64,$string"
echo $string
Instance Attribute Summary collapse
-
#room ⇒ String?
Room (aka: channel, group etc depending on the chat service) in which the message was or will be posted.
-
#text ⇒ String?
readonly
Raw message text.
Class Method Summary collapse
-
.new(string, room: nil) ⇒ Object
Initialize message from either message object previously serialized with
dump
or from raw message text.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Whether two messages are equal.
-
#dump ⇒ String
Serialize the message.
-
#initialize(text, room: nil) ⇒ Message
constructor
Initialize message from raw message text.
-
#to_h ⇒ Hash
Convert message to Hash.
Constructor Details
#initialize(text, room: nil) ⇒ Message
Initialize message from raw message text
42 43 44 |
# File 'lib/rodbot/message.rb', line 42 def initialize(text, room: nil) @text, @room = text, room end |
Instance Attribute Details
#room ⇒ String?
Room (aka: channel, group etc depending on the chat service) in which the message was or will be posted
36 37 38 |
# File 'lib/rodbot/message.rb', line 36 def room @room end |
#text ⇒ String? (readonly)
Raw message text
30 31 32 |
# File 'lib/rodbot/message.rb', line 30 def text @text end |
Class Method Details
.new(string, room: nil) ⇒ Object
Initialize message from either message object previously serialized with dump
or from raw message text
53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/rodbot/message.rb', line 53 def self.new(string, room: nil) allocate.instance_eval do serializer = Rodbot::Serializer.new(string) if serializer.deserializable? hash = serializer.hash fail(ArgumentError, "not a dumped message") unless hash['class'] == self.class.to_s initialize(hash['text'], room: room || hash['room']) else initialize(string.force_encoding('UTF-8'), room: room) end self end end |
Instance Method Details
#==(other) ⇒ Boolean
Whether two messages are equal
84 85 86 |
# File 'lib/rodbot/message.rb', line 84 def ==(other) to_h == other.to_h end |
#dump ⇒ String
Serialize the message
70 71 72 |
# File 'lib/rodbot/message.rb', line 70 def dump Rodbot::Serializer.new(to_h).string end |
#to_h ⇒ Hash
Convert message to Hash
77 78 79 |
# File 'lib/rodbot/message.rb', line 77 def to_h { class: self.class.to_s, text: text, room: room } end |