hipchat 1.5.2 → 1.5.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 46a4547a9f0d2cd12a211d78560fe3dfe7305b7b
4
- data.tar.gz: 9c32aee4cd61577dff4f2d7ada82de5fc7151b95
3
+ metadata.gz: cbeb4474e9bae99465b61d971e6d5ac53f4562f1
4
+ data.tar.gz: a9e7ec6445f91871676065bf26a0c19c817d511c
5
5
  SHA512:
6
- metadata.gz: a600e14c058610d0884bdc56a35838a5146a4603451b9f98eff32086af5452fb72193ed7d338ae56cbe3540d4817803223c7ae5db9988c5c801eaee74be5b84b
7
- data.tar.gz: d4360c4bbf9958d8385c1e24d5fff0b22c13a36043b3943c05710a172a09b38c95b7a74e1da777ad5ef055b0f0c1cf0269f90149f210da8c4650a102ecb6fc68
6
+ metadata.gz: e50b678c47c408557c8443352086ab4fd2020b5d7eaad21741eddae2dfc3866ce06cb333afe0df329814d8f946ae18e0d2d14319f5a4d85b1bc331fed22aed3a
7
+ data.tar.gz: 87e12c6fcc6e94dd7b609eed2cb9f9f32626bedbe14fbe8f32f40c57c3e1abb746b88713973794e267e2beeda814da8441ed660f96ceef4e4fa32b728213078b
@@ -1,5 +1,10 @@
1
1
  language: ruby
2
2
 
3
+ before_install:
4
+ # Travis bundler versions are quite out of date and can cause install errors
5
+ # see: https://github.com/rubygems/rubygems/issues/1419
6
+ - gem update bundler
7
+
3
8
  rvm:
4
9
  - 1.9.3
5
10
  - 2.0.0
@@ -4,8 +4,8 @@ A very basic wrapper for the HipChat HTTP API.
4
4
 
5
5
  h2. CI Status
6
6
 
7
- "!https://travis-ci.org/hipchat/hipchat-rb.png!":https://travis-ci.org/hipchat/hipchat-rb
8
- !https://coveralls.io/repos/hipchat/hipchat-rb/badge.png(Coverage Status)!:https://coveralls.io/r/hipchat/hipchat-rb
7
+ "!https://travis-ci.org/hipchat/hipchat-rb.svg!":https://travis-ci.org/hipchat/hipchat-rb
8
+ !https://coveralls.io/repos/hipchat/hipchat-rb/badge.svg(Coverage Status)!:https://coveralls.io/r/hipchat/hipchat-rb
9
9
 
10
10
  h2. Requirements
11
11
  * Ruby 1.9.3 or higher
@@ -26,6 +26,9 @@ h1. Usage
26
26
  h2. "API v1":https://www.hipchat.com/docs/api
27
27
 
28
28
  bc.. client = HipChat::Client.new(api_token)
29
+ # Set http proxy
30
+ client = HipChat::Client.new(api_token, :http_proxy => 'http://proxy_host:proxy_port')
31
+
29
32
  # 'username' is the name for which the message will be presented as from
30
33
  client['my room'].send('username', 'I talk')
31
34
 
@@ -56,6 +59,9 @@ client.create_room("Name", :owner_user_id => 'user_id')
56
59
  h2. "API v2":https://www.hipchat.com/docs/apiv2
57
60
 
58
61
  bc.. client = HipChat::Client.new(api_token, :api_version => 'v2')
62
+ # Set http proxy
63
+ client = HipChat::Client.new(api_token, :api_version => 'v2', :http_proxy => 'http://proxy_host:proxy_port')
64
+
59
65
  # 'username' is the name for which the message will be presented as from
60
66
  client['my room'].send('username', 'I talk')
61
67
 
@@ -93,6 +99,9 @@ client['my room'].get_room
93
99
  It's recommended to call client['my room'].get_room then pass in modified hash attributes to #update_room
94
100
  client['my room'].update_room(options = {})
95
101
 
102
+ # Delete room (see https://www.hipchat.com/docs/apiv2/method/delete_room)
103
+ client['my room'].delete_room
104
+
96
105
  # Invite user to room (see https://www.hipchat.com/docs/apiv2/method/invite_user)
97
106
  client['my room'].invite("USER_ID_OR_NAME", options = {})
98
107
 
@@ -224,7 +233,7 @@ h2. Chef Handler
224
233
  *APIv1 ONLY, use APIv1 Key*
225
234
  NOTE: APIv2 required for HipChatServer Beta
226
235
 
227
- Report on Chef runs.
236
+ Report on Chef runs.
228
237
 
229
238
  h3. Within a Recipe:
230
239
 
@@ -248,8 +257,8 @@ bc.. require 'hipchat/chef'
248
257
  h3. With HipChat Server Beta
249
258
 
250
259
  Add an "options" hash to set api v2 and your URL:
251
- * arguments ['API_KEY', 'HIPCHAT_ROOM', options={api_version: "v2", server_url: "https://hipchat.example.com"}]
252
- * hipchat_handler = HipChat::NotifyRoom.new("API_KEY", "HIPCHAT_ROOM", options={api_version: "v2", server_url: "https://hipchat.example.com"})
260
+ * arguments ['API_KEY', 'HIPCHAT_ROOM', options={hipchat_options: {api_version: "v2", server_url: "https://hipchat.example.com"}}]
261
+ * hipchat_handler = HipChat::NotifyRoom.new("API_KEY", "HIPCHAT_ROOM", options={hipchat_options: {api_version: "v2", server_url: "https://hipchat.example.com"}})
253
262
 
254
263
  h2. Copyright
255
264
 
@@ -25,9 +25,9 @@ Gem::Specification.new do |spec|
25
25
 
26
26
  spec.add_development_dependency "rspec", "~> 3.0"
27
27
  spec.add_development_dependency "rr"
28
- spec.add_development_dependency "bundler", "~> 1.3"
28
+ spec.add_development_dependency "bundler", "~> 1.11.1"
29
29
  spec.add_development_dependency "rake"
30
- spec.add_development_dependency "webmock"
30
+ spec.add_development_dependency "webmock", "= 1.22.1"
31
31
  spec.add_development_dependency 'rdoc', '> 2.4.2'
32
32
  spec.add_development_dependency 'coveralls'
33
33
  end
@@ -20,10 +20,12 @@ module HipChat
20
20
  @base_uri = "#{options[:server_url]}/v1"
21
21
  @headers = {'Accept' => 'application/json',
22
22
  'Content-Type' => 'application/x-www-form-urlencoded'}
23
- else
23
+ elsif @version.eql?('v2')
24
24
  @base_uri = "#{options[:server_url]}/v2"
25
25
  @headers = {'Accept' => 'application/json',
26
26
  'Content-Type' => 'application/json'}
27
+ else
28
+ raise InvalidApiVersion, 'Couldn\'t recognize API version'
27
29
  end
28
30
  end
29
31
 
@@ -56,6 +58,19 @@ module HipChat
56
58
  }[version]
57
59
  end
58
60
 
61
+ def create_user_config
62
+ {
63
+ 'v1' => {
64
+ :url => '/users/create',
65
+ :body_format => :to_hash
66
+ },
67
+ 'v2' => {
68
+ :url => '/user',
69
+ :body_format => :to_json
70
+ }
71
+ }[version]
72
+ end
73
+
59
74
  def users_config
60
75
  {
61
76
  'v1' => {
@@ -116,6 +131,21 @@ module HipChat
116
131
  }[version]
117
132
  end
118
133
 
134
+ def delete_room_config
135
+ {
136
+ 'v1' => {
137
+ :url => URI::escape("/delete"),
138
+ :method => :post,
139
+ :query_params => { :room_id => room_id }
140
+ },
141
+ 'v2' => {
142
+ :url => URI::escape("/#{room_id}"),
143
+ :method => :delete,
144
+ :query_params => {}
145
+ }
146
+ }[version]
147
+ end
148
+
119
149
  def invite_config
120
150
  {
121
151
  'v2' => {
@@ -125,6 +155,24 @@ module HipChat
125
155
  }[version]
126
156
  end
127
157
 
158
+ def add_member_config
159
+ {
160
+ 'v2' => {
161
+ :url => URI::escape("/#{room_id}/member"),
162
+ :body_format => :to_json
163
+ }
164
+ }[version]
165
+ end
166
+
167
+ def send_message_config
168
+ {
169
+ 'v2' => {
170
+ :url => URI::escape("/#{room_id}/message"),
171
+ :body_format => :to_json
172
+ }
173
+ }[version]
174
+ end
175
+
128
176
  def send_config
129
177
  {
130
178
  'v1' => {
@@ -147,6 +195,15 @@ module HipChat
147
195
  }[version]
148
196
  end
149
197
 
198
+ def share_link_config
199
+ {
200
+ 'v2' => {
201
+ :url => URI::escape("/#{room_id}/share/link"),
202
+ :body_format => :to_json
203
+ }
204
+ }[version]
205
+ end
206
+
150
207
  def topic_config
151
208
  {
152
209
  'v1' => {
@@ -180,6 +237,15 @@ module HipChat
180
237
  }
181
238
  }[version]
182
239
  end
240
+
241
+ def webhook_config
242
+ raise InvalidApiVersion, 'This functionality is not supported in API v1' unless version.eql?('v2')
243
+ {
244
+ 'v2' => {
245
+ :url => URI::escape("/#{room_id}/webhook")
246
+ }
247
+ }[version]
248
+ end
183
249
  end
184
250
 
185
251
  class User
@@ -236,6 +302,22 @@ module HipChat
236
302
  }[version]
237
303
  end
238
304
 
305
+
306
+ def delete_config
307
+ {
308
+ 'v1' => {
309
+ :url => URI::escape('/delete'),
310
+ :body_format => :to_json,
311
+ :query_params => { :user_id => user_id }
312
+ },
313
+ 'v2' => {
314
+ :url => URI::escape("/#{user_id}"),
315
+ :body_format => :to_json,
316
+ :query_params => {}
317
+ }
318
+ }[version]
319
+ end
320
+
239
321
  def history_config
240
322
  raise InvalidApiVersion, 'This functionality is not supported in API v1' unless version.eql?('v2')
241
323
 
@@ -65,18 +65,21 @@ namespace :hipchat do
65
65
 
66
66
  def deployment_name
67
67
  if fetch(:branch, nil)
68
- application = fetch(:application)
69
68
  branch = fetch(:branch)
70
69
  real_revision = fetch(:real_revision)
71
70
 
72
- name = "#{application}/#{branch}"
71
+ name = "#{application_name}/#{branch}"
73
72
  name += " (revision #{real_revision[0..7]})" if real_revision
74
73
  name
75
74
  else
76
- application
75
+ application_name
77
76
  end
78
77
  end
79
78
 
79
+ def application_name
80
+ alt_application_name.nil? ? fetch(:application) : alt_application_name
81
+ end
82
+
80
83
  def message_color
81
84
  fetch(:hipchat_color, 'yellow')
82
85
  end
@@ -101,6 +104,10 @@ namespace :hipchat do
101
104
  fetch(:hipchat_deploy_user, 'Deploy')
102
105
  end
103
106
 
107
+ def alt_application_name
108
+ fetch(:hipchat_app_name, nil)
109
+ end
110
+
104
111
  def human
105
112
  user = ENV['HIPCHAT_USER'] || fetch(:hipchat_human)
106
113
  user = user || if (u = %x{git config user.name}.strip) != ''
@@ -44,16 +44,26 @@ module HipChat
44
44
  :headers => @api.headers
45
45
  )
46
46
 
47
- case response.code
48
- when 201, 200 #CREATED
49
- response.parsed_response
50
- when 400
51
- raise UnknownRoom, "Error: #{response.message}"
52
- when 401
53
- raise Unauthorized, 'Access denied'
54
- else
55
- raise UnknownResponseCode, "Unexpected error #{response.code}"
47
+ ErrorHandler.response_code_to_exception_for :room, name, response
48
+ response.parsed_response
49
+ end
50
+
51
+ def create_user(name, email, options={})
52
+ if name.length > 50
53
+ raise UsernameTooLong, "User name #{name} is #{name.length} characters long. Limit is 50."
56
54
  end
55
+
56
+ response = self.class.post(@api.create_user_config[:url],
57
+ :query => { :auth_token => @token },
58
+ :body => {
59
+ :name => name,
60
+ :email => email
61
+ }.merge(options).send(@api.create_user_config[:body_format]),
62
+ :headers => @api.headers
63
+ )
64
+
65
+ ErrorHandler.response_code_to_exception_for :user, email, response
66
+ response.parsed_response
57
67
  end
58
68
 
59
69
  def user(name)
@@ -92,13 +102,10 @@ module HipChat
92
102
  },
93
103
  :headers => @api.headers
94
104
  )
95
- case response.code
96
- when 200
97
- response[@api.rooms_config[:data_key]].map do |r|
98
- HipChat::Room.new(@token, r.merge(:api_version => @api_version, :server_url => @options[:server_url]))
99
- end
100
- else
101
- raise UnknownResponseCode, "Unexpected #{response.code} for room"
105
+
106
+ ErrorHandler.response_code_to_exception_for :room, nil, response
107
+ response[@api.rooms_config[:data_key]].map do |r|
108
+ HipChat::Room.new(@token, r.merge(:api_version => @api_version, :server_url => @options[:server_url]))
102
109
  end
103
110
  end
104
111
 
@@ -110,13 +117,10 @@ module HipChat
110
117
  },
111
118
  :headers => @api.headers
112
119
  )
113
- case response.code
114
- when 200
115
- response[@api.users_config[:data_key]].map do |u|
116
- HipChat::User.new(@token, u.merge(:api_version => @api_version, :server_url => @options[:server_url]))
117
- end
118
- else
119
- raise UnknownResponseCode, "Unexpected #{response.code} for user"
120
+
121
+ ErrorHandler.response_code_to_exception_for :user, nil, response
122
+ response[@api.users_config[:data_key]].map do |u|
123
+ HipChat::User.new(@token, u.merge(:api_version => @api_version, :server_url => @options[:server_url]))
120
124
  end
121
125
  end
122
126
  end
@@ -1,10 +1,49 @@
1
1
  module HipChat
2
- class UnknownRoom < StandardError; end
3
- class RoomNameTooLong < StandardError; end
4
- class RoomMissingOwnerUserId < StandardError; end
5
- class Unauthorized < StandardError; end
6
- class UsernameTooLong < StandardError; end
7
- class UnknownResponseCode < StandardError; end
8
- class UnknownUser < StandardError; end
9
- class InvalidApiVersion < StandardError; end
2
+ class ServiceError < StandardError; end
3
+ class RoomNameTooLong < ServiceError; end
4
+ class RoomMissingOwnerUserId < ServiceError; end
5
+ class UnknownResponseCode < ServiceError; end
6
+ class Unauthorized < UnknownResponseCode; end
7
+ class BadRequest < ServiceError; end
8
+ class MethodNotAllowed < ServiceError; end
9
+ class UsernameTooLong < ServiceError; end
10
+ class InvalidApiVersion < ServiceError; end
11
+ class InvalidUrl < ServiceError; end
12
+ class InvalidEvent < ServiceError; end
13
+ class ObjectNotFound < UnknownResponseCode; end
14
+ class UnknownRoom < ObjectNotFound; end
15
+ class UnknownUser < ObjectNotFound; end
16
+ class UnknownWebhook < ObjectNotFound; end
17
+ class TooManyRequests < UnknownResponseCode; end
18
+
19
+ class ErrorHandler
20
+
21
+ # Pass-through to catch error states and raise their appropriate exceptions
22
+ # @param klass {Symbol} The class of object we are handling the error for
23
+ # @param identifier {String} An identifying string for the object causing the error
24
+ # @param response {HTTParty::Response} The HTTParty response/request object
25
+ def self.response_code_to_exception_for(klass, identifier, response)
26
+ # Supports user, room and webhook objects. If we get something other than that, bail.
27
+ raise ServiceError "Unknown class #{klass}" unless [:user, :room, :webhook].include? klass
28
+ # Grab the corresponding unknown object exception class and constantize it for the 404 case to call
29
+ not_found_exception = Module.const_get(HipChat.to_s.to_sym).const_get("Unknown#{klass.capitalize}".to_sym)
30
+ case response.code
31
+ when 200, 201, 202, 204;
32
+ return
33
+ when 400
34
+ raise BadRequest, "The request was invalid. You may be missing a required argument or provided bad data. path:#{response.request.path.to_s} method:#{response.request.http_method.to_s}"
35
+ when 401, 403
36
+ raise Unauthorized, "Access denied to #{klass} `#{identifier}'"
37
+ when 404
38
+ raise not_found_exception, "Unknown #{klass}: `#{identifier}'"
39
+ when 405
40
+ raise MethodNotAllowed, "You requested an invalid method. path:#{response.request.path.to_s} method:#{response.request.http_method.to_s}"
41
+ when 429
42
+ raise TooManyRequests, 'You have exceeded the rate limit. `https://www.hipchat.com/docs/apiv2/rate_limiting`'
43
+ else
44
+ raise UnknownResponseCode, "Unexpected #{response.code} for #{klass} `#{identifier}'"
45
+ end
46
+ end
47
+ end
48
+
10
49
  end
@@ -23,16 +23,8 @@ module HipChat
23
23
  :headers => @api.headers
24
24
  )
25
25
 
26
- case response.code
27
- when 200
28
- response.parsed_response
29
- when 404
30
- raise UnknownRoom, "Unknown room: `#{room_id}'"
31
- when 401
32
- raise Unauthorized, "Access denied to room `#{room_id}'"
33
- else
34
- raise UnknownResponseCode, "Unexpected #{response.code} for room `#{room_id}'"
35
- end
26
+ ErrorHandler.response_code_to_exception_for :room, room_id, response
27
+ response.parsed_response
36
28
  end
37
29
 
38
30
  # Update a room
@@ -55,15 +47,17 @@ module HipChat
55
47
  }.to_json,
56
48
  :headers => @api.headers)
57
49
 
58
- case response.code
59
- when 200, 204; true
60
- when 404
61
- raise Unknown Room, "Unknown room: `#{room_id}'"
62
- when 401
63
- raise Unauthorized, "Access denied to room `#{room_id}'"
64
- else
65
- raise UnknownResponseCode, "Unexpected #{response.code} for room `#{room_id}'"
66
- end
50
+ ErrorHandler.response_code_to_exception_for :room, room_id, response
51
+ true
52
+ end
53
+
54
+ # Delete a room
55
+ def delete_room
56
+ response = self.class.send(@api.delete_room_config[:method], @api.delete_room_config[:url],
57
+ :query => {:auth_token => @token }.merge(@api.get_room_config[:query_params]),
58
+ :headers => @api.headers)
59
+ ErrorHandler.response_code_to_exception_for :room, room_id, response
60
+ true
67
61
  end
68
62
 
69
63
  # Invite user to this room
@@ -75,15 +69,21 @@ module HipChat
75
69
  }.to_json,
76
70
  :headers => @api.headers)
77
71
 
78
- case response.code
79
- when 200, 204; true
80
- when 404
81
- raise UnknownRoom, "Unknown room: `#{room_id}'"
82
- when 401
83
- raise Unauthorized, "Access denied to room `#{room_id}'"
84
- else
85
- raise UnknownResponseCode, "Unexpected #{response.code} for room `#{room_id}'"
86
- end
72
+ ErrorHandler.response_code_to_exception_for :room, room_id, response
73
+ true
74
+ end
75
+
76
+ # Add member to this room
77
+ def add_member(user, room_roles=['room_member'])
78
+ response = self.class.put(@api.add_member_config[:url]+"/#{user}",
79
+ :query => { :auth_token => @token },
80
+ :body => {
81
+ :room_roles => room_roles
82
+ }.to_json,
83
+ :headers => @api.headers)
84
+
85
+ ErrorHandler.response_code_to_exception_for :room, room_id, response
86
+ true
87
87
  end
88
88
 
89
89
 
@@ -92,6 +92,28 @@ module HipChat
92
92
  # Usage:
93
93
  #
94
94
  # # Default
95
+ # send 'some message'
96
+ #
97
+ def send_message(message)
98
+ response = self.class.post(@api.send_message_config[:url],
99
+ :query => { :auth_token => @token },
100
+ :body => {
101
+ :room_id => room_id,
102
+ :message => message,
103
+ }.send(@api.send_config[:body_format]),
104
+ :headers => @api.headers
105
+ )
106
+
107
+ ErrorHandler.response_code_to_exception_for :room, room_id, response
108
+ true
109
+ end
110
+
111
+
112
+ # Send a notification message to this room.
113
+ #
114
+ # Usage:
115
+ #
116
+ # # Default
95
117
  # send 'nickname', 'some message'
96
118
  #
97
119
  # # Notify users and color the message red
@@ -107,8 +129,8 @@ module HipChat
107
129
  # +notify+:: true or false
108
130
  # (default false)
109
131
  def send(from, message, options_or_notify = {})
110
- if from.length > 15
111
- raise UsernameTooLong, "Username #{from} is `#{from.length} characters long. Limit is 15'"
132
+ if from.length > 20
133
+ raise UsernameTooLong, "Username #{from} is `#{from.length} characters long. Limit is 20'"
112
134
  end
113
135
  options = if options_or_notify == true or options_or_notify == false
114
136
  warn 'DEPRECATED: Specify notify flag as an option (e.g., :notify => true)'
@@ -117,30 +139,46 @@ module HipChat
117
139
  options_or_notify || {}
118
140
  end
119
141
 
120
- options = { :color => 'yellow', :notify => false }.merge options
142
+ options = { :color => 'yellow', :notify => false, :message_format => 'html' }.merge options
143
+
144
+ body = {
145
+ :room_id => room_id,
146
+ :from => from,
147
+ :message => message,
148
+ :message_format => options[:message_format],
149
+ :color => options[:color],
150
+ :card => options[:card],
151
+ :notify => @api.bool_val(options[:notify])
152
+ }.delete_if { |_k, v| v.nil? }
121
153
 
122
154
  response = self.class.post(@api.send_config[:url],
155
+ :query => { :auth_token => @token },
156
+ :body => body.send(@api.send_config[:body_format]),
157
+ :headers => @api.headers
158
+ )
159
+
160
+ ErrorHandler.response_code_to_exception_for :room, room_id, response
161
+ true
162
+ end
163
+
164
+ def share_link(from, message, link)
165
+ if from.length > 20
166
+ raise UsernameTooLong, "Username #{from} is `#{from.length} characters long. Limit is 20'"
167
+ end
168
+
169
+ response = self.class.post(@api.share_link_config[:url],
123
170
  :query => { :auth_token => @token },
124
171
  :body => {
125
172
  :room_id => room_id,
126
173
  :from => from,
127
174
  :message => message,
128
- :message_format => options[:message_format] || 'html',
129
- :color => options[:color],
130
- :notify => @api.bool_val(options[:notify])
175
+ :link => link,
131
176
  }.send(@api.send_config[:body_format]),
132
177
  :headers => @api.headers
133
178
  )
134
179
 
135
- case response.code
136
- when 200, 204; true
137
- when 404
138
- raise UnknownRoom, "Unknown room: `#{room_id}'"
139
- when 401
140
- raise Unauthorized, "Access denied to room `#{room_id}'"
141
- else
142
- raise UnknownResponseCode, "Unexpected #{response.code} for room `#{room_id}'"
143
- end
180
+ ErrorHandler.response_code_to_exception_for :room, room_id, response
181
+ true
144
182
  end
145
183
 
146
184
  # Send a file to this room.
@@ -150,8 +188,8 @@ module HipChat
150
188
  # # Default
151
189
  # send_file 'nickname', 'some message', File.open("/path/to/file")
152
190
  def send_file(from, message, file)
153
- if from.length > 15
154
- raise UsernameTooLong, "Username #{from} is `#{from.length} characters long. Limit is 15'"
191
+ if from.length > 20
192
+ raise UsernameTooLong, "Username #{from} is `#{from.length} characters long. Limit is 20'"
155
193
  end
156
194
 
157
195
  response = self.class.post(@api.send_file_config[:url],
@@ -166,15 +204,8 @@ module HipChat
166
204
  :headers => file_body_headers(@api.headers)
167
205
  )
168
206
 
169
- case response.code
170
- when 200, 204; true
171
- when 404
172
- raise UnknownRoom, "Unknown room: `#{room_id}'"
173
- when 401
174
- raise Unauthorized, "Access denied to room `#{room_id}'"
175
- else
176
- raise UnknownResponseCode, "Unexpected #{response.code} for room `#{room_id}'"
177
- end
207
+ ErrorHandler.response_code_to_exception_for :room, room_id, response
208
+ true
178
209
  end
179
210
 
180
211
  # Change this room's topic
@@ -202,15 +233,8 @@ module HipChat
202
233
  :headers => @api.headers
203
234
  )
204
235
 
205
- case response.code
206
- when 204,200; true
207
- when 404
208
- raise UnknownRoom, "Unknown room: `#{room_id}'"
209
- when 401
210
- raise Unauthorized, "Access denied to room `#{room_id}'"
211
- else
212
- raise UnknownResponseCode, "Unexpected #{response.code} for room `#{room_id}'"
213
- end
236
+ ErrorHandler.response_code_to_exception_for :room, room_id, response
237
+ true
214
238
  end
215
239
 
216
240
  # Pull this room's history
@@ -235,7 +259,8 @@ module HipChat
235
259
  :timezone => 'UTC',
236
260
  :format => 'JSON',
237
261
  :'max-results' => 100,
238
- :'start-index' => 0
262
+ :'start-index' => 0,
263
+ :'end-date' => nil
239
264
  }.merge options
240
265
 
241
266
  response = self.class.get(@api.history_config[:url],
@@ -246,21 +271,14 @@ module HipChat
246
271
  :format => options[:format],
247
272
  :'max-results' => options[:'max-results'],
248
273
  :'start-index' => options[:'start-index'],
274
+ :'end-date' => options[:'end-date'],
249
275
  :auth_token => @token
250
276
  },
251
277
  :headers => @api.headers
252
278
  )
253
279
 
254
- case response.code
255
- when 200
256
- response.body
257
- when 404
258
- raise UnknownRoom, "Unknown room: `#{room_id}'"
259
- when 401
260
- raise Unauthorized, "Access denied to room `#{room_id}'"
261
- else
262
- raise UnknownResponseCode, "Unexpected #{response.code} for room `#{room_id}'"
263
- end
280
+ ErrorHandler.response_code_to_exception_for :room, room_id, response
281
+ response.body
264
282
  end
265
283
 
266
284
  # Pull this room's statistics
@@ -277,16 +295,113 @@ module HipChat
277
295
  :headers => @api.headers
278
296
  )
279
297
 
280
- case response.code
281
- when 200
282
- response.body
283
- when 404
284
- raise UnknownRoom, "Unknown room: `#{room_id}'"
285
- when 401
286
- raise Unauthorized, "Access denied to room `#{room_id}'"
287
- else
288
- raise UnknownResponseCode, "Unexpected #{response.code} for room `#{room_id}'"
298
+ ErrorHandler.response_code_to_exception_for :room, room_id, response
299
+ response.body
300
+ end
301
+
302
+ # Create a webhook for this room
303
+ #
304
+ # Usage:
305
+ #
306
+ # # Default
307
+ # create_webhook 'http://example.org/path/to/my/webhook', 'room_event'
308
+ #
309
+ # Options:
310
+ #
311
+ # +pattern+:: The regular expression pattern to match against messages. Only applicable for message events.
312
+ # (default "")
313
+ # +name+:: The label for this webhook
314
+ # (default "")
315
+ def create_webhook(url, event, options = {})
316
+ raise InvalidEvent unless %w(room_message room_notification room_exit room_enter room_topic_change room_archived room_deleted room_unarchived).include? event
317
+
318
+ begin
319
+ u = URI::parse(url)
320
+ raise InvalidUrl unless %w(http https).include? u.scheme
321
+ rescue URI::InvalidURIError
322
+ raise InvalidUrl
289
323
  end
324
+
325
+ options = {
326
+ :pattern => '',
327
+ :name => ''
328
+ }.merge options
329
+
330
+ response = self.class.post(@api.webhook_config[:url],
331
+ :query => {
332
+ :auth_token => @token
333
+ },
334
+ :body => {:url => url, :pattern => options[:pattern], :event => event, :name => options[:name]}.send(@api.send_config[:body_format]),
335
+ :headers => @api.headers
336
+ )
337
+
338
+ ErrorHandler.response_code_to_exception_for :room, room_id, response
339
+ response.body
340
+ end
341
+
342
+ # Delete a webhook for this room
343
+ #
344
+ # Usage:
345
+ #
346
+ # # Default
347
+ # delete_webhook 'webhook_id'
348
+ def delete_webhook(webhook_id)
349
+ response = self.class.delete("#{@api.webhook_config[:url]}/#{URI::escape(webhook_id)}",
350
+ :query => {
351
+ :auth_token => @token
352
+ },
353
+ :headers => @api.headers
354
+ )
355
+
356
+ ErrorHandler.response_code_to_exception_for :webhook, webhook_id, response
357
+ true
358
+ end
359
+
360
+ # Gets all webhooks for this room
361
+ #
362
+ # Usage:
363
+ #
364
+ # # Default
365
+ # get_all_webhooks
366
+ #
367
+ # Options:
368
+ #
369
+ # +start-index+:: The regular expression pattern to match against messages. Only applicable for message events.
370
+ # (default "")
371
+ # +max-results+:: The label for this webhook
372
+ # (default "")
373
+ def get_all_webhooks(options = {})
374
+ options = {:'start-index' => 0, :'max-results' => 100}.merge(options)
375
+
376
+ response = self.class.get(@api.webhook_config[:url],
377
+ :query => {
378
+ :auth_token => @token,
379
+ :'start-index' => options[:'start-index'],
380
+ :'max-results' => options[:'max-results']
381
+ },
382
+ :headers => @api.headers
383
+ )
384
+
385
+ ErrorHandler.response_code_to_exception_for :room, room_id, response
386
+ response.body
387
+ end
388
+
389
+ # Get a webhook for this room
390
+ #
391
+ # Usage:
392
+ #
393
+ # # Default
394
+ # get_webhook 'webhook_id'
395
+ def get_webhook(webhook_id)
396
+ response = self.class.get("#{@api.webhook_config[:url]}/#{URI::escape(webhook_id)}",
397
+ :query => {
398
+ :auth_token => @token
399
+ },
400
+ :headers => @api.headers
401
+ )
402
+
403
+ ErrorHandler.response_code_to_exception_for :webhook, webhook_id, response
404
+ response.body
290
405
  end
291
406
 
292
407
  private