hipchat 1.3.0 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 87814d532992250965bb8fd683295acd9ed7e8c1
4
- data.tar.gz: d0e551de4271ce51a6d60fdac6821b0b7c30f929
3
+ metadata.gz: 1b54a53da40893dd5e1fbd4d0fafa3f75f00c685
4
+ data.tar.gz: c5f30c699dda1135934c4085faeefd3c6667a837
5
5
  SHA512:
6
- metadata.gz: 8cb2ae5a7ddcc189db17a21b09a63739ebf4dbc523ab62be6908ab14120b442f5d7d5dce6fed6ce5f8c77bb0509a401fe49e5b0b2754638089588e136e36fcef
7
- data.tar.gz: 6aed60e1f7cb4df9b021cc461e47b9f814ba06c51ae3d2c5fc8a10b528ea0690e2aa468bf569500dc5e9e83973df901113f46543c997248e9b0190c28ebb8c39
6
+ metadata.gz: a61559bdf4536909fcd9857a0f2afed9eb4ee2baf96cbe69b76a0dd4dce36f6888b7f089759189bce1a13848973c71d42cb49659f26fb75c233a9bbe1e9dbbb0
7
+ data.tar.gz: cf8f7bed717b1b5854d17b4297d680e324f131ac5d32f1ef8a509ad4fb04882a687fbb6e99156a6edf4530025b7459af5bcb6ccace87f34e5503e7295909c099
data/.gitignore CHANGED
@@ -13,6 +13,10 @@ tmtags
13
13
  ## VIM
14
14
  *.swp
15
15
 
16
+ ## IntelliJ
17
+ *.iml
18
+ .idea
19
+
16
20
  ## PROJECT::GENERAL
17
21
  coverage
18
22
  rdoc
@@ -32,4 +36,4 @@ test/tmp
32
36
  test/version_tmp
33
37
  tmp
34
38
 
35
- ## PROJECT::SPECIFIC
39
+ ## PROJECT::SPECIFIC
@@ -80,12 +80,19 @@ client['my room'].history()
80
80
  # Get history for a date in time with a particular timezone (default is latest 75 messages, timezone default is 'UTC')
81
81
  client['my room'].history(:date => '2010-11-19', :timezone => 'PST')
82
82
 
83
+ # Get statistics from a room
84
+ client['my room'].statistics()
85
+
83
86
  # Create a new room (see https://www.hipchat.com/docs/apiv2/method/create_room)
84
87
  client.create_room("Name", options = {})
85
88
 
86
89
  # Get room data (see https://www.hipchat.com/docs/apiv2/method/get_room)
87
90
  client['my room'].get_room
88
91
 
92
+ # Update room data (see https://www.hipchat.com/docs/apiv2/method/update_room)
93
+ It's recommended to call client['my room'].get_room then pass in modified hash attributes to #update_room
94
+ client['my room'].update_room(options = {})
95
+
89
96
  # Invite user to room (see https://www.hipchat.com/docs/apiv2/method/invite_user)
90
97
  client['my room'].invite("USER_ID_OR_NAME", options = {})
91
98
 
@@ -125,6 +132,10 @@ client.create_room("Name", options = {})
125
132
  # Get room data (see https://www.hipchat.com/docs/apiv2/method/get_room)
126
133
  client['my room'].get_room
127
134
 
135
+ # Update room data (see https://www.hipchat.com/docs/apiv2/method/update_room)
136
+ It's easiest to call client['my room'].get_room, parse the json and then pass in modified hash attributes
137
+ client['my room'].update_room(options = {})
138
+
128
139
  # Invite user to room (see https://www.hipchat.com/docs/apiv2/method/invite_user)
129
140
  client['my room'].invite("USER_ID_OR_NAME", options = {})
130
141
 
@@ -186,7 +197,7 @@ Options like the room, API token, user name and notification flag can be set in
186
197
  RAILS_ROOT/config/hipchat.yml:
187
198
 
188
199
  bc.. token: "<your token>"
189
- room: "Your room"
200
+ room: ["Room name(s) or id(s)"] # this is an array
190
201
  user: "Your name" # Default to `whoami`
191
202
  notify: true # Defaults to false
192
203
  api_version: "v2" # optional, defaults to v1
@@ -209,8 +220,11 @@ end
209
220
  h2. Chef Handler
210
221
 
211
222
  *APIv1 ONLY, use APIv1 Key*
223
+ NOTE: APIv2 required for HipChatServer Beta
224
+
225
+ Report on Chef runs.
212
226
 
213
- Report on Chef runs. Within a Recipe:
227
+ h3. Within a Recipe:
214
228
 
215
229
  bc.. include_recipe 'chef_handler'
216
230
 
@@ -223,6 +237,18 @@ chef_handler 'HipChat::NotifyRoom' do
223
237
  'hipchat', 'chef.rb')
224
238
  end
225
239
 
240
+ h3. With client.rb:
241
+
242
+ bc.. require 'hipchat/chef'
243
+ hipchat_handler = HipChat::NotifyRoom.new("API_KEY", "HIPCHAT_ROOM")
244
+ exception_handlers << hipchat_handler
245
+
246
+ h3. With HipChat Server Beta
247
+
248
+ Add an "options" hash to set api v2 and your URL:
249
+ * arguments ['API_KEY', 'HIPCHAT_ROOM', options={api_version: "v2", server_url: "https://hipchat.example.com"}]
250
+ * hipchat_handler = HipChat::NotifyRoom.new("API_KEY", "HIPCHAT_ROOM", options={api_version: "v2", server_url: "https://hipchat.example.com"})
251
+
226
252
  h2. Copyright
227
253
 
228
254
  Copyright (c) 2012 Atlassian. See LICENSE for details.
@@ -90,6 +90,16 @@ module HipChat
90
90
  }[version]
91
91
  end
92
92
 
93
+ def update_room_config
94
+ {
95
+ "v2" => {
96
+ :url => URI::escape("/#{room_id}"),
97
+ :method => :put,
98
+ :body_format => :to_json
99
+ }
100
+ }[version]
101
+ end
102
+
93
103
  def invite_config
94
104
  {
95
105
  'v2' => {
@@ -137,6 +147,14 @@ module HipChat
137
147
  }
138
148
  }[version]
139
149
  end
150
+
151
+ def statistics_config
152
+ {
153
+ 'v2' => {
154
+ :url => URI::escape("/#{room_id}/statistics")
155
+ }
156
+ }[version]
157
+ end
140
158
  end
141
159
 
142
160
  class User
@@ -15,11 +15,13 @@ require 'hipchat'
15
15
  module HipChat
16
16
  class NotifyRoom < Chef::Handler
17
17
 
18
- def initialize(api_token, room_name, notify_users=false, report_success=false, excluded_envs=[])
18
+ def initialize(api_token, room_name, options={}, msg_options={}, notify_users=false, report_success=false, excluded_envs=[], msg_prefix='')
19
19
  @api_token = api_token
20
20
  @room_name = room_name
21
- @notify_users = notify_users
21
+ @options = options
22
22
  @report_success = report_success
23
+ @msg_options = msg_options
24
+ @msg_options[:notify] = notify_users
23
25
  @excluded_envs = excluded_envs
24
26
  end
25
27
 
@@ -31,13 +33,13 @@ module HipChat
31
33
  else nil
32
34
  end
33
35
 
34
- color = if run_status.success? then 'green'
36
+ @msg_options[:color]= if run_status.success? then 'green'
35
37
  else 'red'
36
38
  end
37
39
 
38
40
  if msg
39
- client = HipChat::Client.new(@api_token)
40
- client[@room_name].send('Chef', msg, :notify => @notify_users, :color => color)
41
+ client = HipChat::Client.new(@api_token, @options)
42
+ client[@room_name].send('Chef', [msg_prefix, msg].join(' '), @msg_options)
41
43
  end
42
44
  end
43
45
  end
@@ -118,7 +118,7 @@ module HipChat
118
118
  User.new(@token, r.merge(:api_version => @api_version, :user_id => r['id']))
119
119
  end
120
120
  else
121
- raise UnknownResponseCode, "Unexpected #{response.code} for room"
121
+ raise UnknownResponseCode, "Unexpected #{response.code} for user"
122
122
  end
123
123
  end
124
124
  end
@@ -42,6 +42,8 @@ namespace :hipchat do
42
42
 
43
43
  client = HipChat::Client.new(options[:token], options)
44
44
 
45
- client[options[:room]].send(options[:user], options[:message], { :color => options[:color], :notify => options[:notify] })
45
+ options[:room].each do |r|
46
+ client[options[r]].send(options[:user], options[:message], { :color => options[:color], :notify => options[:notify] })
47
+ end
46
48
  end
47
49
  end
@@ -34,6 +34,39 @@ module HipChat
34
34
  end
35
35
  end
36
36
 
37
+ # Update a room
38
+ def update_room(options = {})
39
+ options = {
40
+ :privacy => "public",
41
+ :is_archived => false,
42
+ :is_guest_accessible => false
43
+ }.merge symbolize(options)
44
+
45
+ response = self.class.send(@api.topic_config[:method], @api.update_room_config[:url],
46
+ :query => { :auth_token => @token },
47
+ :body => {
48
+ :name => options[:name],
49
+ :topic => options[:topic],
50
+ :privacy => options[:privacy],
51
+ :is_archived => @api.bool_val(options[:is_archived]),
52
+ :is_guest_accessible => @api.bool_val(options[:is_guest_accessible]),
53
+ :owner => options[:owner]
54
+ }.to_json,
55
+ :headers => @api.headers)
56
+
57
+ puts response.body
58
+
59
+ case response.code
60
+ when 200, 204; true
61
+ when 404
62
+ raise Unknown Room, "Unknown room: `#{room_id}'"
63
+ when 401
64
+ raise Unauthorized, "Access denied to room `#{room_id}'"
65
+ else
66
+ raise UnknownResponseCode, "Unexpected #{response.code} for room `#{room_id}'"
67
+ end
68
+ end
69
+
37
70
  # Invite user to this room
38
71
  def invite(user, reason="")
39
72
  response = self.class.post(@api.invite_config[:url]+"/#{user}",
@@ -188,5 +221,44 @@ module HipChat
188
221
  raise UnknownResponseCode, "Unexpected #{response.code} for room `#{room_id}'"
189
222
  end
190
223
  end
224
+
225
+ # Pull this room's statistics
226
+ def statistics(options = {})
227
+
228
+ response = self.class.get(@api.statistics_config[:url],
229
+ :query => {
230
+ :room_id => room_id,
231
+ :date => options[:date],
232
+ :timezone => options[:timezone],
233
+ :format => options[:format],
234
+ :auth_token => @token,
235
+ },
236
+ :headers => @api.headers
237
+ )
238
+
239
+ case response.code
240
+ when 200
241
+ response.body
242
+ when 404
243
+ raise UnknownRoom, "Unknown room: `#{room_id}'"
244
+ when 401
245
+ raise Unauthorized, "Access denied to room `#{room_id}'"
246
+ else
247
+ raise UnknownResponseCode, "Unexpected #{response.code} for room `#{room_id}'"
248
+ end
249
+ end
250
+
251
+ private
252
+ def symbolize(obj)
253
+ return obj.reduce({}) do |memo, (k, v)|
254
+ memo.tap { |m| m[k.to_sym] = symbolize(v) }
255
+ end if obj.is_a? Hash
256
+
257
+ return obj.reduce([]) do |memo, v|
258
+ memo << symbolize(v); memo
259
+ end if obj.is_a? Array
260
+ obj
261
+ end
262
+
191
263
  end
192
264
  end
@@ -1,3 +1,3 @@
1
1
  module HipChat
2
- VERSION = "1.3.0"
2
+ VERSION = "1.4.0"
3
3
  end
@@ -54,6 +54,48 @@ describe "HipChat (API V2)" do
54
54
  end
55
55
  end
56
56
 
57
+ describe "#statistics" do
58
+ include_context "HipChatV2"
59
+ it "is successful without custom options" do
60
+ mock_successful_statistics
61
+
62
+ room.statistics().should be_true
63
+ end
64
+
65
+ it "is successful from fetched room" do
66
+ mock_successful_rooms
67
+ mock_successful_statistics
68
+
69
+ subject.rooms.should be_true
70
+ subject.rooms.first.statistics.should be_true
71
+ end
72
+
73
+ it "fails when the room doen't exist" do
74
+ mock(HipChat::Room).get(anything, anything) {
75
+ OpenStruct.new(:code => 404)
76
+ }
77
+
78
+ lambda { room.statistics }.should raise_error(HipChat::UnknownRoom)
79
+ end
80
+
81
+ it "fails when we're not allowed to do so" do
82
+ mock(HipChat::Room).get(anything, anything) {
83
+ OpenStruct.new(:code => 401)
84
+ }
85
+
86
+ lambda { room.statistics }.should raise_error(HipChat::Unauthorized)
87
+ end
88
+
89
+ it "fails if we get an unknown response code" do
90
+ mock(HipChat::Room).get(anything, anything) {
91
+ OpenStruct.new(:code => 403)
92
+ }
93
+
94
+ lambda { room.statistics }.
95
+ should raise_error(HipChat::UnknownResponseCode)
96
+ end
97
+ end
98
+
57
99
  describe "#topic" do
58
100
  include_context "HipChatV2"
59
101
  it "is successful without custom options" do
@@ -182,6 +224,24 @@ describe "HipChat (API V2)" do
182
224
 
183
225
  end
184
226
 
227
+ describe "#update_room" do
228
+ include_context "HipChatV2"
229
+ let(:room_info) {
230
+ {
231
+ "name" => "hipchat",
232
+ "topic" => "hipchat topic",
233
+ "privacy" => "public",
234
+ "is_archived" => false,
235
+ "is_guest_accessible" => false,
236
+ "owner" => { "id" => "12345" }
237
+ }
238
+ }
239
+ it "successfully" do
240
+ mock_successful_update_room("Hipchat", room_info)
241
+ room.update_room(room_info).should be_true
242
+ end
243
+ end
244
+
185
245
  describe "#invite" do
186
246
  include_context "HipChatV2"
187
247
 
@@ -112,6 +112,17 @@ shared_context "HipChatV2" do
112
112
  :format => options[:format]}).to_return(canned_response)
113
113
  end
114
114
 
115
+ def mock_successful_statistics(options={})
116
+ stub_request(:get, "https://api.hipchat.com/v2/room/Hipchat/statistics").with(:query => {:auth_token => "blah",
117
+ :room_id => "Hipchat",
118
+ :date => options[:date],
119
+ :timezone => options[:timezone],
120
+ :format => options[:format]}).to_return(
121
+ :status => 200,
122
+ :body => '{"last_active": "2014-09-02T21:33:54+00:00", "links": {"self": "https://api.hipchat.com/v2/room/12345/statistics"}, "messages_sent": 10}',
123
+ :headers => {})
124
+ end
125
+
115
126
  def mock_successful_room_creation(name, options={})
116
127
  stub_request(:post, "https://api.hipchat.com/v2/room").with(
117
128
  :query => {:auth_token => "blah"},
@@ -131,6 +142,24 @@ shared_context "HipChatV2" do
131
142
  'Content-Type' => 'application/json'}).to_return(:status => 200, :body => "{\"id\":\"#{room_id}\"}", :headers => {})
132
143
  end
133
144
 
145
+ def mock_successful_update_room(room_id="1234", options={})
146
+ stub_request(:put, "https://api.hipchat.com/v2/room/#{room_id}").with(
147
+ :query => {:auth_token => "blah"},
148
+ :body => {
149
+ :name => "hipchat",
150
+ :topic => "hipchat topic",
151
+ :privacy => "public",
152
+ :is_archived => false,
153
+ :is_guest_accessible => false,
154
+ :owner => { :id => "12345" }
155
+ }.to_json,
156
+ :headers => {"Accept" => "application/json",
157
+ "Content-Type" => "application/json"}).to_return(
158
+ :status => 204,
159
+ :body => "",
160
+ :headers => {})
161
+ end
162
+
134
163
  def mock_successful_invite(options={})
135
164
  options = {:user_id => "1234"}.merge(options)
136
165
  stub_request(:post, "https://api.hipchat.com/v2/room/Hipchat/invite/#{options[:user_id]}").with(
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hipchat
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - HipChat/Atlassian
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-08 00:00:00.000000000 Z
11
+ date: 2014-10-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty