Class: Gitter::API::Message

Inherits:
Base
  • Object
show all
Includes:
Collectable
Defined in:
lib/gitter/api/message.rb

Overview

Model representation of the /room/:room_id/chatMessages* REST endpoints in the gitter API

Instance Attribute Summary collapse

Attributes inherited from Base

#client

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Collectable

included

Methods inherited from Base

#api_prefix

Constructor Details

#initialize(client, room_id, data) ⇒ Message

INTERNAL METHOD

Initialize a new Gitter::API::Message

Used by Gitter::API::Room when fetching messages, so favor using that instead.

Parameters

Note: messages in the Gitter schema don’t have a ‘room_id`, so that is passed in as an additional arg here.

client (Gitter::API::Client)

Configured client object

room_id (String)

Room ID message came from

data (Hash)

Initialization data

Options

(string keys only)

id (String)

Message id

user (String)

Gitter::API::User that sent the message

text (String)

Original message in plain-text/markdown

html (String)

HTML formatted message

unread (Boolean)

Indicates if current user has read the message

read_by (Integer)

Number of users that have read the message

created_at (Date)

ISO formatted date of the message

updated_at (Date)

ISO formatted date of the message if edited

mentions (Array<User>)

Gitter::API::User(s) mentioned in message

issues (Array<String>)

List of #Issues referenced in the message

urls (Array<String>)

List of URLs present in message



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/gitter/api/message.rb', line 82

def initialize client, room_id, data
  super client, data

  @room_id    = room_id

  @id         = data["id"]
  @user       = User.new client, data["fromUser"]
  @text       = data["text"]
  @html       = data["html"]
  @unread     = data["unread"]

  @read_by    = data["readBy"]
  @created_at = data["sent"]
  @updated_at = data["editedAt"]

  @mentions   = data["mentions"].map { |user| User.new client, user }
  @issues     = data["issues"]
  @urls       = data["urls"]
end

Instance Attribute Details

#created_atObject (readonly)

ISO formatted date of when the message originally sent



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

def created_at
  @created_at
end

#htmlObject (readonly)

HTML formatted message (formatted on the API server)



27
28
29
# File 'lib/gitter/api/message.rb', line 27

def html
  @html
end

#idObject (readonly)

Message id (from gitter)



18
19
20
# File 'lib/gitter/api/message.rb', line 18

def id
  @id
end

#issuesObject (readonly)

List of github issues referened in the message



45
46
47
# File 'lib/gitter/api/message.rb', line 45

def issues
  @issues
end

#mentionsObject (readonly)

List of Gitter::API::User mentioned in the message



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

def mentions
  @mentions
end

#read_byObject (readonly)

Number of users that have read the message



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

def read_by
  @read_by
end

#textObject (readonly)

Original message in plain-text/markdown



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

def text
  @text
end

#unreadObject (readonly)

Indicates if the message has been read by the client user



30
31
32
# File 'lib/gitter/api/message.rb', line 30

def unread
  @unread
end

#updated_atObject (readonly)

ISO formatted data of when the message was edited last (if it has been)



39
40
41
# File 'lib/gitter/api/message.rb', line 39

def updated_at
  @updated_at
end

#urlsObject (readonly)

List of URLs present in the message



48
49
50
# File 'lib/gitter/api/message.rb', line 48

def urls
  @urls
end

#userObject (readonly)

Gitter::API::User that sent the message



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

def user
  @user
end

Class Method Details

.collectable_args(parent, item_data) ⇒ Object

See Gitter::API::Collectable



11
12
13
14
15
# File 'lib/gitter/api/message.rb', line 11

def self.collectable_args parent, item_data # :nodoc:
  room_id = parent.respond_to? :room_id ? parent.room_id : nil

  [parent.client, room_id, item_data]
end

Instance Method Details

#mark_as_readObject

Mark the message as read for the client user

:return: true



126
127
128
129
130
131
132
133
# File 'lib/gitter/api/message.rb', line 126

def mark_as_read
  payload = { "chat" => [id] }.to_json
  path    = "/#{api_prefix}/user/#{client.user.id}/rooms/#{room_id}/unreadItems"

  client.post path, payload

  true
end

#update(text) ⇒ Object

Edit/update a Message’s text

The html will be reformated on the server, and returned as part of the data when returned.

A new instance of Gitter::API::Message is the turn value

:return: Gitter::API::Message

Parameters

text (String)

New text to update the message record to



115
116
117
118
119
120
# File 'lib/gitter/api/message.rb', line 115

def update text
  payload = { "text" => message }.to_json
  data    = client.post "#{api_prefix}/rooms/#{room_id}/chatMessages/#{id}", payload

  new client, room_id, data
end