Class: HipChat::Room

Inherits:
OpenStruct
  • Object
show all
Includes:
HTTParty, FileHelper
Defined in:
lib/hipchat/room.rb

Constant Summary

Constants included from FileHelper

FileHelper::BOUNDARY

Instance Method Summary collapse

Constructor Details

#initialize(token, params) ⇒ Room

Returns a new instance of Room.



12
13
14
15
16
17
# File 'lib/hipchat/room.rb', line 12

def initialize(token, params)
  @token = token
  @api = HipChat::ApiVersion::Room.new(params)
  self.class.base_uri(@api.base_uri)
  super(params)
end

Instance Method Details

#add_member(user, room_roles = ['room_member']) ⇒ Object

Add member to this room



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/hipchat/room.rb', line 77

def add_member(user, room_roles=['room_member'])
  response = self.class.put(@api.add_member_config[:url]+"/#{user}",
    :query => { :auth_token => @token },
    :body => {
      :room_roles => room_roles
    }.to_json,
    :headers => @api.headers)

  ErrorHandler.response_code_to_exception_for :room, room_id, response
  true
end

#create_webhook(url, event, options = {}) ⇒ Object

Create a webhook for this room

Usage:

# Default
create_webhook 'http://example.org/path/to/my/webhook', 'room_event'

Options:

pattern

The regular expression pattern to match against messages. Only applicable for message events.

(default "")
name

The label for this webhook

(default "")

Raises:



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/hipchat/room.rb', line 325

def create_webhook(url, event, options = {})
  raise InvalidEvent.new("Invalid event: #{event}") unless %w(room_message room_notification room_exit room_enter room_topic_change room_archived room_deleted room_unarchived).include? event

  begin
    u = URI::parse(url)
    raise InvalidUrl.new("Invalid Scheme: #{url}") unless %w(http https).include? u.scheme
  rescue URI::InvalidURIError
    raise InvalidUrl.new("Invalid URL: #{url}")
  end

  merged_options = {
    :pattern => '',
    :name => ''
  }.merge options

  response = self.class.post(@api.webhook_config[:url],
    :query => {
      :auth_token => @token
    },
    :body => {:url => url, :pattern => merged_options[:pattern], :event => event, :name => merged_options[:name]}.send(@api.send_config[:body_format]),
    :headers => @api.headers
  )

  ErrorHandler.response_code_to_exception_for :room, room_id, response
  response.body
end

#delete_roomObject

Delete a room



55
56
57
58
59
60
61
# File 'lib/hipchat/room.rb', line 55

def delete_room
  response = self.class.send(@api.delete_room_config[:method], @api.delete_room_config[:url],
    :query => {:auth_token => @token }.merge(@api.get_room_config[:query_params]),
    :headers => @api.headers)
  ErrorHandler.response_code_to_exception_for :room, room_id, response
  true
end

#delete_webhook(webhook_id) ⇒ Object

Delete a webhook for this room

Usage:

# Default
delete_webhook 'webhook_id'


358
359
360
361
362
363
364
365
366
367
368
# File 'lib/hipchat/room.rb', line 358

def delete_webhook(webhook_id)
  response = self.class.delete("#{@api.webhook_config[:url]}/#{URI::escape(webhook_id)}",
                             :query => {
                               :auth_token => @token
                             },
                             :headers => @api.headers
  )

  ErrorHandler.response_code_to_exception_for :webhook, webhook_id, response
  true
end

#get_all_webhooks(options = {}) ⇒ Object

Gets all webhooks for this room

Usage:

# Default
get_all_webhooks

Options:

start-index

The regular expression pattern to match against messages. Only applicable for message events.

(default "")
max-results

The label for this webhook

(default "")


383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'lib/hipchat/room.rb', line 383

def get_all_webhooks(options = {})
  merged_options = {:'start-index' => 0, :'max-results' => 100}.merge(options)

  response = self.class.get(@api.webhook_config[:url],
                               :query => {
                                 :auth_token => @token,
                                 :'start-index' => merged_options[:'start-index'],
                                 :'max-results' => merged_options[:'max-results']
                               },
                               :headers => @api.headers
  )

  ErrorHandler.response_code_to_exception_for :room, room_id, response
  response.body
end

#get_roomObject

Retrieve data for this room



20
21
22
23
24
25
26
27
28
# File 'lib/hipchat/room.rb', line 20

def get_room
  response = self.class.get(@api.get_room_config[:url],
    :query => {:auth_token => @token }.merge(@api.get_room_config[:query_params]),
    :headers => @api.headers
  )

  ErrorHandler.response_code_to_exception_for :room, room_id, response
  response.parsed_response
end

#get_webhook(webhook_id) ⇒ Object

Get a webhook for this room

Usage:

# Default
get_webhook 'webhook_id'


405
406
407
408
409
410
411
412
413
414
415
# File 'lib/hipchat/room.rb', line 405

def get_webhook(webhook_id)
  response = self.class.get("#{@api.webhook_config[:url]}/#{URI::escape(webhook_id)}",
                            :query => {
                              :auth_token => @token
                            },
                            :headers => @api.headers
  )

  ErrorHandler.response_code_to_exception_for :webhook, webhook_id, response
  response.body
end

#history(options = {}) ⇒ Object

Pull this room’s history

Usage

# Default

Options

date

Whether to return a specific day (YYYY-MM-DD format) or recent

(default "recent")
timezone

Your timezone. Supported timezones are at: www.hipchat.com/docs/api/timezones

(default "UTC")
format

Format to retrieve the history in. Valid options are JSON and XML

(default "JSON")


265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/hipchat/room.rb', line 265

def history(options = {})

  merged_options = {
    :date => 'recent',
    :timezone => 'UTC',
    :format => 'JSON',
    :'max-results' => 100,
    :'start-index' => 0,
    :'end-date' => nil
  }.merge options

  response = self.class.get(@api.history_config[:url],
    :query => {
      :room_id    => room_id,
      :date       => merged_options[:date],
      :timezone   => merged_options[:timezone],
      :format     => merged_options[:format],
      :'max-results' => merged_options[:'max-results'],
      :'start-index' => merged_options[:'start-index'],
      :'end-date' => merged_options[:'end-date'],
      :auth_token => @token
    }.reject!{|k,v| v.nil?},
    :headers => @api.headers
  )

  ErrorHandler.response_code_to_exception_for :room, room_id, response
  response.body
end

#invite(user, reason = '') ⇒ Object

Invite user to this room



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/hipchat/room.rb', line 64

def invite(user, reason='')
  response = self.class.post(@api.invite_config[:url]+"/#{user}",
    :query => { :auth_token => @token },
    :body => {
      :reason => reason
    }.to_json,
    :headers => @api.headers)

  ErrorHandler.response_code_to_exception_for :room, room_id, response
  true
end

#reply(parent_message_id, message) ⇒ Object



164
165
166
167
168
169
170
171
172
# File 'lib/hipchat/room.rb', line 164

def reply(parent_message_id, message)
  query_params  = { auth_token: @token }.merge(@api.reply_config[:query_params])
  body          = { message: message, parent_message_id: parent_message_id }.send(@api.reply_config[:body_format])

  response = self.class.post(@api.reply_config[:url], query: query_params, body: body, headers: @api.headers)

  ErrorHandler.response_code_to_exception_for :room, 'all', response
  response.parsed_response
end

#send(from, message, options_or_notify = {}) ⇒ Object

Send a notification message to this room.

Usage:

# Default
send 'nickname', 'some message'

# Notify users and color the message red
send 'nickname', 'some message', :notify => true, :color => 'red'

# Notify users (deprecated)
send 'nickname', 'some message', true

Options:

color

“yellow”, “red”, “green”, “purple”, or “random” (default “yellow”)

notify

true or false (default false)



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/hipchat/room.rb', line 131

def send(from, message, options_or_notify = {})
  if from.length > 20
    raise UsernameTooLong, "Username #{from} is `#{from.length} characters long. Limit is 20'"
  end
  if options_or_notify == true or options_or_notify == false
    warn 'DEPRECATED: Specify notify flag as an option (e.g., :notify => true)'
    options = { :notify => options_or_notify }
  else
    options = options_or_notify
  end

  merged_options = { :color => 'yellow', :notify => false, :message_format => 'html' }.merge options

  body = {
    :room_id        => room_id,
    :from           => from,
    :message        => message,
    :message_format => merged_options[:message_format],
    :color          => merged_options[:color],
    :card           => merged_options[:card],
    :notify         => @api.bool_val(merged_options[:notify])
  }.delete_if { |_k, v| v.nil? }

  response = self.class.post(@api.send_config[:url],
    :query => { :auth_token => @token },
    :body  => body.send(@api.send_config[:body_format]),
    :headers => @api.headers
  )

  ErrorHandler.response_code_to_exception_for :room, room_id, response
  true
end

#send_file(from, message, file) ⇒ Object

Send a file to this room.

Usage:

# Default
send_file 'nickname', 'some message', File.open("/path/to/file")


200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/hipchat/room.rb', line 200

def send_file(from, message, file)
  if from.length > 20
    raise UsernameTooLong, "Username #{from} is `#{from.length} characters long. Limit is 20'"
  end

  response = self.class.post(@api.send_file_config[:url],
    :query => { :auth_token => @token },
    :body  => file_body(
      {
        :room_id        => room_id,
        :from           => from,
        :message        => message,
      }.send(@api.send_config[:body_format]), file
    ),
    :headers => file_body_headers(@api.headers)
  )

  ErrorHandler.response_code_to_exception_for :room, room_id, response
  true
end

#send_message(message) ⇒ Object

Send a message to this room.

Usage:

# Default
send 'some message'


97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/hipchat/room.rb', line 97

def send_message(message)
  response = self.class.post(@api.send_message_config[:url],
    :query => { :auth_token => @token },
    :body  => {
      :room_id => room_id,
      :message => message,
    }.send(@api.send_config[:body_format]),
    :headers => @api.headers
  )

  ErrorHandler.response_code_to_exception_for :room, room_id, response
  true
end


174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/hipchat/room.rb', line 174

def share_link(from, message, link)
  if from.length > 20
    raise UsernameTooLong, "Username #{from} is `#{from.length} characters long. Limit is 20'"
  end

  response = self.class.post(@api.share_link_config[:url],
    :query => { :auth_token => @token },
    :body  => {
      :room_id        => room_id,
      :from           => from,
      :message        => message,
      :link           => link,
    }.send(@api.send_config[:body_format]),
    :headers => @api.headers
  )

  ErrorHandler.response_code_to_exception_for :room, room_id, response
  true
end

#statistics(options = {}) ⇒ Object

Pull this room’s statistics



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/hipchat/room.rb', line 295

def statistics(options = {})

  response = self.class.get(@api.statistics_config[:url],
    :query => {
      :room_id    => room_id,
      :date       => options[:date],
      :timezone   => options[:timezone],
      :format     => options[:format],
      :auth_token => @token,
    }.reject!{|k,v| v.nil?},
    :headers => @api.headers
  )

  ErrorHandler.response_code_to_exception_for :room, room_id, response
  response.body
end

#topic(new_topic, options = {}) ⇒ Object

Change this room’s topic

Usage:

# Default
topic 'my awesome topic'

Options:

from

the name of the person changing the topic

(default "API")


232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/hipchat/room.rb', line 232

def topic(new_topic, options = {})

  merged_options = { :from => 'API' }.merge options

  response = self.class.send(@api.topic_config[:method], @api.topic_config[:url],
    :query => { :auth_token => @token },
    :body  => {
      :room_id        => room_id,
      :from           => merged_options[:from],
      :topic          => new_topic
    }.send(@api.topic_config[:body_format]),
    :headers => @api.headers
  )

  ErrorHandler.response_code_to_exception_for :room, room_id, response
  true
end

#update_room(options = {}) ⇒ Object

Update a room



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/hipchat/room.rb', line 31

def update_room(options = {})
  merged_options = {
    :privacy => 'public',
    :is_archived => false,
    :is_guest_accessible => false
  }.merge symbolize(options)

  response = self.class.send(@api.topic_config[:method], @api.update_room_config[:url],
    :query => { :auth_token => @token },
    :body => {
      :name => merged_options[:name],
      :topic => merged_options[:topic],
      :privacy => merged_options[:privacy],
      :is_archived => @api.bool_val(merged_options[:is_archived]),
      :is_guest_accessible => @api.bool_val(merged_options[:is_guest_accessible]),
      :owner => merged_options[:owner]
    }.to_json,
    :headers => @api.headers)

  ErrorHandler.response_code_to_exception_for :room, room_id, response
  true
end