Class: Rodbot::Message

Inherits:
Object
  • Object
show all
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:

  1. Create a JSON hash which contains the keys class (with static value Rodbot::Message), text and optionally room.

  2. Encode it as Base64 without newlines.

  3. 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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, room: nil) ⇒ Message

Initialize message from raw message text

Parameters:

  • text (String)

    raw message text

  • room (String, nil) (defaults to: nil)

    room in which the message was or will be posted



42
43
44
# File 'lib/rodbot/message.rb', line 42

def initialize(text, room: nil)
  @text, @room = text, room
end

Instance Attribute Details

#roomString?

Room (aka: channel, group etc depending on the chat service) in which the message was or will be posted

Returns:

  • (String, nil)


36
37
38
# File 'lib/rodbot/message.rb', line 36

def room
  @room
end

#textString? (readonly)

Raw message text

Returns:

  • (String, nil)


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

Parameters:

  • string (String)

    string returned by dump or raw message text

  • room (String, nil) (defaults to: nil)

    room in which the message was or will be posted

Raises:

  • (ArgumentError)

    if the string is not valid Base64, JSON or does not contain the key “class”:“Rodbot::Message”



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

Returns:

  • (Boolean)


84
85
86
# File 'lib/rodbot/message.rb', line 84

def ==(other)
  to_h == other.to_h
end

#dumpString

Serialize the message

Returns:

  • (String)

    serialized and encoded self



70
71
72
# File 'lib/rodbot/message.rb', line 70

def dump
  Rodbot::Serializer.new(to_h).string
end

#to_hHash

Convert message to Hash

Returns:

  • (Hash)


77
78
79
# File 'lib/rodbot/message.rb', line 77

def to_h
  { class: self.class.to_s, text: text, room: room }
end