Class: Gitter::API::Room

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

Overview

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

Defined Under Namespace

Modules: ClientMethods

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, data) ⇒ Room

INTERNAL METHOD

Initialize a new Gitter::API::Room

Use Gitter::API::Room::ClientMethods (found of Gitter::API::Client) to initialize and make use of the instance methods.

Parameters

client (Gitter::API::Client)

Configured client object

data (Hash)

Initialization data

Options

(string keys only)

id (String)

Room id

name (String)

Room name

topic (String)

Room topic

one_on_one (Boolean)

Indicates if one on one chat

lurk (Boolean)

Indicates if notifications disabled

public (Boolean)

Indicates if public room

unread_items (Integer)

Number of unread messages

mentions (Integer)

Number of unread mentions

user_count (Integer)

Number of users in the room

tags (Array<String>)

Tags that define the room

uri (String)

Room URI on Gitter

url (String)

Path to the room on gitter



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/gitter/api/room.rb', line 96

def initialize client, data
  super

  @id           = data["id"]
  @name         = data["name"]
  @topic        = data["topic"]
  @one_on_one   = data["oneOnOne"]
  @lurk         = data["lurk"]
  @public       = data["public"]
  @mentions     = data["mentions"]
  @user_count   = data["userCount"]
  @unread_items = data["unreadItems"]
  @tags         = data["tags"]
  @uri          = data["uri"]
  @url          = data["url"]
end

Instance Attribute Details

#idObject (readonly) Also known as: room_id

Room id



10
11
12
# File 'lib/gitter/api/room.rb', line 10

def id
  @id
end

#lurkObject (readonly)

Indicates if the room is configured with notifications for the user



22
23
24
# File 'lib/gitter/api/room.rb', line 22

def lurk
  @lurk
end

#mentionsObject (readonly)

Number of unread mentions for this room



28
29
30
# File 'lib/gitter/api/room.rb', line 28

def mentions
  @mentions
end

#nameObject (readonly)

Room name



13
14
15
# File 'lib/gitter/api/room.rb', line 13

def name
  @name
end

#one_on_oneObject (readonly)

Indicates if the room is a one on one chat



19
20
21
# File 'lib/gitter/api/room.rb', line 19

def one_on_one
  @one_on_one
end

#publicObject (readonly)

Indicates if the room is public



25
26
27
# File 'lib/gitter/api/room.rb', line 25

def public
  @public
end

#tagsObject (readonly)

Array of tags for the room



37
38
39
# File 'lib/gitter/api/room.rb', line 37

def tags
  @tags
end

#topicObject (readonly)

Room topic



16
17
18
# File 'lib/gitter/api/room.rb', line 16

def topic
  @topic
end

#unread_itemsObject (readonly)

Number of unread messages for this room



34
35
36
# File 'lib/gitter/api/room.rb', line 34

def unread_items
  @unread_items
end

#uriObject (readonly)

Room URI on gitter



40
41
42
# File 'lib/gitter/api/room.rb', line 40

def uri
  @uri
end

#urlObject (readonly)

Room url



43
44
45
# File 'lib/gitter/api/room.rb', line 43

def url
  @url
end

#user_countObject (readonly)

Number of users in the room



31
32
33
# File 'lib/gitter/api/room.rb', line 31

def user_count
  @user_count
end

Class Method Details

.find(uri) ⇒ Object

Find a room given a URI

See Gitter::API::Room::ClientMethods#find_room

Parameters

uri (String)

Room URI on Gitter

Example

client.find "gitterhq/sandbox"
#=> <#Gitter::API::Room name="gitterhq/sandbox" ...>

:return: Gitter::API::Room



63
64
65
# File 'lib/gitter/api/room.rb', line 63

def self.find uri
  Client.find_room uri
end

Instance Method Details

#joinObject

Join the current room

:return: Gitter::API::Room



170
171
172
173
174
175
# File 'lib/gitter/api/room.rb', line 170

def join
  payload = { "id" => id }.to_json
  data    = client.post "#{api_prefix}/user/#{client.user.id}/rooms", payload

  self
end

#messages(options = {}) ⇒ Object

List recent messages of a room

Options

:search (String)

Filter based on search query

:before (String)

Limit messages to before a date

:after (String)

Limit messages to after a date

:around (String)

Limit messages to around a date

:limit (Integer)

Limit number of records returned

:skip (Integer)

Return users after skiping N records

Returns a collection of most recent messages, with the oldest of that collection being first in the returned list.

:return: Gitter::API::User::Collection



151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/gitter/api/room.rb', line 151

def messages options = {}
  query = {
    "skip"     => options[:skip],
    "beforeId" => options[:before],
    "afterId"  => options[:after],
    "aroundId" => options[:around],
    "limit"    => options[:limit],
    "q"        => options[:search]
  }

  data = client.get "/v1/rooms/#{id}/chatMessages", query

  Message::Collection.new self, data
end

#send_message(message) ⇒ Object

Send a message to the current room

Parameters

message (String)

Message to send to the room

Messages should be in plain text/mardown format, and will be converted to html on the server side.

:return: Gitter::API::Message



188
189
190
191
192
193
# File 'lib/gitter/api/room.rb', line 188

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

  Message.new client, id, data
end

#unread_messagesObject

Unread messages in the room for the current user

:return: Gitter::API::Message::Collection



199
200
201
202
203
# File 'lib/gitter/api/room.rb', line 199

def unread_messages
  data = client.get "#{api_prefix}/user/#{client.user.id}/rooms/#{id}/unreadItems"

  Message::Collection.new self, data
end

#users(options = {}) ⇒ Object

List users of a room

Options

:search (String)

Filter based on search query

:limit (Integer)

Limit number of records returned

:skip (Integer)

Return users after skiping N records

:return: Gitter::API::User::Collection



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/gitter/api/room.rb', line 123

def users options = {}
  query = {
    "skip"     => options[:skip],
    "limit"    => options[:limit],
    "q"        => options[:search]
  }

  data = client.get "/#{api_prefix}/rooms/#{id}/users", query

  User::Collection.new self, data
end