Class: ChatX::Message

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server, **opts) ⇒ Message

Returns a new instance of Message.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/chatx/models/message.rb', line 10

def initialize(server, **opts)
  if opts.values_at(:time_stamp, :content, :room_id, :user_id, :message_id).any?(&:nil?)
    raise ChatX::InitializationDataException, 'Got nil for an expected message property'
  end

  @server = server

  @id = opts[:message_id]
  @timestamp = Time.at(opts[:time_stamp]).utc.to_datetime
  @content = opts[:content]
  @room = ChatX::Helpers.cached opts[:room_id].to_i, :rooms do
    ChatX::Room.new server, room_id: opts[:room_id].to_i
  end
  @user = ChatX::Helpers.cached opts[:user_id].to_i, :users do
    ChatX::User.new server, user_id: opts[:user_id].to_i
  end

  @parent_id = opts[:parent_id]
end

Instance Attribute Details

#contentObject (readonly) Also known as: body

Returns the value of attribute content.



8
9
10
# File 'lib/chatx/models/message.rb', line 8

def content
  @content
end

#idObject (readonly)

Returns the value of attribute id.



8
9
10
# File 'lib/chatx/models/message.rb', line 8

def id
  @id
end

#parent_idObject (readonly)

Returns the value of attribute parent_id.



8
9
10
# File 'lib/chatx/models/message.rb', line 8

def parent_id
  @parent_id
end

#roomObject (readonly)

Returns the value of attribute room.



8
9
10
# File 'lib/chatx/models/message.rb', line 8

def room
  @room
end

#serverObject (readonly)

Returns the value of attribute server.



8
9
10
# File 'lib/chatx/models/message.rb', line 8

def server
  @server
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



8
9
10
# File 'lib/chatx/models/message.rb', line 8

def timestamp
  @timestamp
end

#userObject (readonly)

Returns the value of attribute user.



8
9
10
# File 'lib/chatx/models/message.rb', line 8

def user
  @user
end

Class Method Details

.from_hash(server, hash) ⇒ Object



66
67
68
# File 'lib/chatx/models/message.rb', line 66

def self.from_hash(server, hash)
  new server, **ChatX::Helpers.symbolize_hash_keys(hash)
end

Instance Method Details

#pinged?(username) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/chatx/models/message.rb', line 45

def pinged?(username)
  @pings.map(&:downcase).map { |x| username.downcase.start_with? x }.any?
end

#pingsObject



40
41
42
43
# File 'lib/chatx/models/message.rb', line 40

def pings
  @pings = @content.scan(/@(\w+)/).flatten if @pings.nil?
  @pings
end

#reply(bot, content) ⇒ Object



32
33
34
# File 'lib/chatx/models/message.rb', line 32

def reply(bot, content)
  bot.say ":#{id} #{content}", @room.id, server: @server
end

#reply?Boolean

Returns:

  • (Boolean)


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

def reply?
  @content =~ /^:\d+\s/
end