Class: Chatrix::Message

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

Overview

Describes a message sent in a room.

Constant Summary collapse

TYPES =

Supported message types.

:html is a special message type parsed in #parse_body!.

{
  'm.text' => :text,
  'm.emote' => :emote,
  'm.notice' => :notice
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sender, timestamp, content) ⇒ Message

Initializes a new Message instance.



40
41
42
43
44
45
46
47
48
49
# File 'lib/chatrix/message.rb', line 40

def initialize(sender, timestamp, content)
  @raw = content

  @type = TYPES[@raw['msgtype']]
  @body = @raw['body']
  @sender = sender
  @timestamp = timestamp

  parse_body!
end

Instance Attribute Details

#bodyString (readonly)



29
30
31
# File 'lib/chatrix/message.rb', line 29

def body
  @body
end

#rawHash (readonly)



17
18
19
# File 'lib/chatrix/message.rb', line 17

def raw
  @raw
end

#senderUser (readonly)



24
25
26
# File 'lib/chatrix/message.rb', line 24

def sender
  @sender
end

#timestampInteger (readonly)



33
34
35
# File 'lib/chatrix/message.rb', line 33

def timestamp
  @timestamp
end

#typeSymbol? (readonly)



21
22
23
# File 'lib/chatrix/message.rb', line 21

def type
  @type
end

Instance Method Details

#parse_body!Object (private)

Parses the message content to see if there's any special formatting available.



55
56
57
58
59
60
61
62
# File 'lib/chatrix/message.rb', line 55

def parse_body!
  return unless @raw.key? 'format'
  case @raw['format']
  when 'org.matrix.custom.html'
    @type = :html
    @formatted = @raw['formatted_body']
  end
end