Class: HipChat::Room
- Inherits:
-
OpenStruct
- Object
- OpenStruct
- HipChat::Room
- Includes:
- HTTParty, FileHelper
- Defined in:
- lib/hipchat/room.rb
Constant Summary
Constants included from FileHelper
Instance Method Summary collapse
-
#add_member(user, room_roles = ['room_member']) ⇒ Object
Add member to this room.
-
#create_webhook(url, event, options = {}) ⇒ Object
Create a webhook for this room.
-
#delete_room ⇒ Object
Delete a room.
-
#delete_webhook(webhook_id) ⇒ Object
Delete a webhook for this room.
-
#get_all_webhooks(options = {}) ⇒ Object
Gets all webhooks for this room.
-
#get_room ⇒ Object
Retrieve data for this room.
-
#get_webhook(webhook_id) ⇒ Object
Get a webhook for this room.
-
#history(options = {}) ⇒ Object
Pull this room’s history.
-
#initialize(token, params) ⇒ Room
constructor
A new instance of Room.
-
#invite(user, reason = '') ⇒ Object
Invite user to this room.
- #reply(parent_message_id, message) ⇒ Object
-
#send(from, message, options_or_notify = {}) ⇒ Object
Send a notification message to this room.
-
#send_file(from, message, file) ⇒ Object
Send a file to this room.
-
#send_message(message) ⇒ Object
Send a message to this room.
- #share_link(from, message, link) ⇒ Object
-
#statistics(options = {}) ⇒ Object
Pull this room’s statistics.
-
#topic(new_topic, options = {}) ⇒ Object
Change this room’s topic.
-
#update_room(options = {}) ⇒ Object
Update a room.
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 "")
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, = {}) 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 = { :pattern => '', :name => '' }.merge response = self.class.post(@api.webhook_config[:url], :query => { :auth_token => @token }, :body => {:url => url, :pattern => [:pattern], :event => event, :name => [:name]}.send(@api.send_config[:body_format]), :headers => @api.headers ) ErrorHandler.response_code_to_exception_for :room, room_id, response response.body end |
#delete_room ⇒ Object
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( = {}) = {:'start-index' => 0, :'max-results' => 100}.merge() response = self.class.get(@api.webhook_config[:url], :query => { :auth_token => @token, :'start-index' => [:'start-index'], :'max-results' => [:'max-results'] }, :headers => @api.headers ) ErrorHandler.response_code_to_exception_for :room, room_id, response response.body end |
#get_room ⇒ Object
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( = {}) = { :date => 'recent', :timezone => 'UTC', :format => 'JSON', :'max-results' => 100, :'start-index' => 0, :'end-date' => nil }.merge response = self.class.get(@api.history_config[:url], :query => { :room_id => room_id, :date => [:date], :timezone => [:timezone], :format => [:format], :'max-results' => [:'max-results'], :'start-index' => [:'start-index'], :'end-date' => [:'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(, ) query_params = { auth_token: @token }.merge(@api.reply_config[:query_params]) body = { message: , 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, , = {}) if from.length > 20 raise UsernameTooLong, "Username #{from} is `#{from.length} characters long. Limit is 20'" end if == true or == false warn 'DEPRECATED: Specify notify flag as an option (e.g., :notify => true)' = { :notify => } else = end = { :color => 'yellow', :notify => false, :message_format => 'html' }.merge body = { :room_id => room_id, :from => from, :message => , :message_format => [:message_format], :color => [:color], :card => [:card], :notify => @api.bool_val([: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, , 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 => , }.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 () response = self.class.post(@api.[:url], :query => { :auth_token => @token }, :body => { :room_id => room_id, :message => , }.send(@api.send_config[:body_format]), :headers => @api.headers ) ErrorHandler.response_code_to_exception_for :room, room_id, response true end |
#share_link(from, message, link) ⇒ Object
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, , 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 => , :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( = {}) response = self.class.get(@api.statistics_config[:url], :query => { :room_id => room_id, :date => [:date], :timezone => [:timezone], :format => [: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, = {}) = { :from => 'API' }.merge response = self.class.send(@api.topic_config[:method], @api.topic_config[:url], :query => { :auth_token => @token }, :body => { :room_id => room_id, :from => [: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( = {}) = { :privacy => 'public', :is_archived => false, :is_guest_accessible => false }.merge symbolize() response = self.class.send(@api.topic_config[:method], @api.update_room_config[:url], :query => { :auth_token => @token }, :body => { :name => [:name], :topic => [:topic], :privacy => [:privacy], :is_archived => @api.bool_val([:is_archived]), :is_guest_accessible => @api.bool_val([:is_guest_accessible]), :owner => [:owner] }.to_json, :headers => @api.headers) ErrorHandler.response_code_to_exception_for :room, room_id, response true end |