Class: ChatX::Message
- Inherits:
-
Object
- Object
- ChatX::Message
- Defined in:
- lib/chatx/models/message.rb
Instance Attribute Summary collapse
-
#content ⇒ Object
(also: #body)
readonly
Returns the value of attribute content.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#parent_id ⇒ Object
readonly
Returns the value of attribute parent_id.
-
#room ⇒ Object
readonly
Returns the value of attribute room.
-
#server ⇒ Object
readonly
Returns the value of attribute server.
-
#timestamp ⇒ Object
readonly
Returns the value of attribute timestamp.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(server, **opts) ⇒ Message
constructor
A new instance of Message.
- #pinged?(username) ⇒ Boolean
- #pings ⇒ Object
- #reply(bot, content) ⇒ Object
- #reply? ⇒ Boolean
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
#content ⇒ Object (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 |
#id ⇒ Object (readonly)
Returns the value of attribute id.
8 9 10 |
# File 'lib/chatx/models/message.rb', line 8 def id @id end |
#parent_id ⇒ Object (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 |
#room ⇒ Object (readonly)
Returns the value of attribute room.
8 9 10 |
# File 'lib/chatx/models/message.rb', line 8 def room @room end |
#server ⇒ Object (readonly)
Returns the value of attribute server.
8 9 10 |
# File 'lib/chatx/models/message.rb', line 8 def server @server end |
#timestamp ⇒ Object (readonly)
Returns the value of attribute timestamp.
8 9 10 |
# File 'lib/chatx/models/message.rb', line 8 def @timestamp end |
#user ⇒ Object (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
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 |
#pings ⇒ Object
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
36 37 38 |
# File 'lib/chatx/models/message.rb', line 36 def reply? @content =~ /^:\d+\s/ end |