hipchat_searcher 0.0.3 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ {
2
+ "items": [],
3
+ "links": {"self": "https://api.hipchat.com/v2/room/12345/history"},
4
+ "maxResults": 100,
5
+ "startIndex": 0
6
+ }
7
+
@@ -1,7 +1,6 @@
1
- require 'spec_helper'
2
1
 
3
2
  describe HipchatSearcher do
4
3
  it 'has a version number' do
5
- HipchatSearcher::VERSION.should == '0.0.3'
4
+ HipchatSearcher::VERSION.should == '1.0.0'
6
5
  end
7
6
  end
@@ -0,0 +1,79 @@
1
+ require 'webmock/rspec'
2
+
3
+ describe HipchatSearcher::Message do
4
+ def stub_request_with_headers(uri)
5
+ stub_request(:get, uri).with(headers: headers)
6
+ end
7
+
8
+ let(:headers) do
9
+ {
10
+ 'Accept' => 'application/json',
11
+ 'Content-Type' => 'application/json',
12
+ }
13
+ end
14
+
15
+ describe '#get_history' do
16
+ context 'with no options' do
17
+ before do
18
+ stub_request_with_headers('https://api.hipchat.com/v2/room/id-12345/history?auth_token=abcd&date=recent&format=JSON&room_id=id-12345&timezone=UTC')
19
+ end
20
+
21
+ it 'should request to hipchat-api with default option of hipchat api' do
22
+ message = described_class.new('abcd')
23
+ message.get_history('id-12345', {})
24
+ end
25
+ end
26
+
27
+ context 'with options' do
28
+ before do
29
+ stub_request_with_headers('https://api.hipchat.com/v2/room/id-12345/history?auth_token=abcd&date=2014-07-12&format=JSON&room_id=id-12345&timezone=JST')
30
+ end
31
+
32
+ it 'should request to hipchat-api with option overridden of hipchat api' do
33
+ message = described_class.new('abcd')
34
+ message.get_history('id-12345', {date: '2014-07-12', timezone: 'JST'})
35
+ end
36
+ end
37
+
38
+ context 'when id must be escaped' do
39
+ before do
40
+ stub_request_with_headers('https://api.hipchat.com/v2/room/id%2012345/history?auth_token=abcd&date=recent&format=JSON&room_id=id%2012345&timezone=UTC')
41
+ end
42
+
43
+ it 'should request to url that is escaped' do
44
+ message = described_class.new('abcd')
45
+ message.get_history('id 12345', {})
46
+ end
47
+ end
48
+ end
49
+
50
+ describe '#history' do
51
+ before do
52
+ stub_request_with_headers('https://api.hipchat.com/v2/room/id-12345/history?auth_token=abcd&date=recent&format=JSON&room_id=id-12345&timezone=UTC').to_return(body: response)
53
+ end
54
+
55
+ let(:response) do
56
+ path = File.join('spec', 'data', 'item-list.json')
57
+ File.read(path)
58
+ end
59
+
60
+ it 'should return HipchatSearcher::Result instance' do
61
+ message = described_class.new('abcd')
62
+ response = message.history('id-12345')
63
+ response.should be_instance_of HipchatSearcher::Result
64
+ end
65
+
66
+ it 'when arguments is String should return the room name the same string of arguments' do
67
+ message = described_class.new('abcd')
68
+ response = message.history('id-12345')
69
+ response.room.should == 'id-12345'
70
+ end
71
+
72
+ it 'when arguments is Hashie::Mash object should return the room name objects given' do
73
+ room = Hashie::Mash.new(id: 'id-12345', name: 'room1')
74
+ message = described_class.new('abcd')
75
+ response = message.history(room)
76
+ response.room.should == 'room1'
77
+ end
78
+ end
79
+ end
@@ -1,5 +1,3 @@
1
- require 'spec_helper'
2
-
3
1
  describe HipchatSearcher::Result do
4
2
  def result(response)
5
3
  described_class.new(response)
@@ -61,29 +59,6 @@ describe HipchatSearcher::Result do
61
59
  end
62
60
  end
63
61
 
64
- describe '#rooms' do
65
- context 'the value' do
66
- subject { result(response).rooms }
67
-
68
- let(:response) { eval File.read(path) }
69
- let(:path) { File.join('spec', 'data', 'room-list.txt') }
70
-
71
- it { should be_instance_of Array }
72
- end
73
-
74
- context 'the element' do
75
- let(:rooms) { result(response).rooms.first }
76
- let(:response) { eval File.read(path) }
77
- let(:path) { File.join('spec', 'data', 'room-list.txt') }
78
-
79
- it { rooms.should be_instance_of ::Hashie::Mash }
80
-
81
- it 'should have keys "id", "links", "name"' do
82
- rooms.keys.should == ['id', 'links', 'name']
83
- end
84
- end
85
- end
86
-
87
62
  describe '#items' do
88
63
  context 'the value' do
89
64
  subject { result(response).items }
@@ -144,4 +119,51 @@ describe HipchatSearcher::Result do
144
119
  end
145
120
  end
146
121
  end
122
+
123
+ describe '#oldest_date' do
124
+ context 'when items is not empty' do
125
+ subject { result(response).oldest_date }
126
+
127
+ let(:response) { File.read(path) }
128
+ let(:path) { File.join('spec', 'data', 'item-list.json') }
129
+
130
+ it { should be_instance_of String }
131
+
132
+ it 'should return the oldest date in any items' do
133
+ should == "2014-05-30T01:38:16.741565+00:00"
134
+ end
135
+ end
136
+
137
+ context 'when item is empty' do
138
+ subject { result(response).oldest_date }
139
+
140
+ let(:response) { File.read(path) }
141
+ let(:path) { File.join('spec', 'data', 'item-list-empty.json') }
142
+
143
+ it { should be_nil }
144
+ end
145
+ end
146
+
147
+ describe '#rooms' do
148
+ context 'the value' do
149
+ subject { result(response).rooms }
150
+
151
+ let(:response) { eval File.read(path) }
152
+ let(:path) { File.join('spec', 'data', 'room-list.txt') }
153
+
154
+ it { should be_instance_of Array }
155
+ end
156
+
157
+ context 'the element' do
158
+ let(:rooms) { result(response).rooms.first }
159
+ let(:response) { eval File.read(path) }
160
+ let(:path) { File.join('spec', 'data', 'room-list.txt') }
161
+
162
+ it { rooms.should be_instance_of ::Hashie::Mash }
163
+
164
+ it 'should have keys "id", "links", "name"' do
165
+ rooms.keys.should == ['id', 'links', 'name']
166
+ end
167
+ end
168
+ end
147
169
  end
@@ -0,0 +1,58 @@
1
+ require 'webmock/rspec'
2
+
3
+ describe HipchatSearcher::Room do
4
+ describe '#get_all_room' do
5
+ context 'with no options' do
6
+ before do
7
+ stub_request(:get, 'https://api.hipchat.com/v2/room?auth_token=abcde')
8
+ end
9
+
10
+ it 'should request to hipchat api' do
11
+ room = described_class.new('abcde')
12
+ room.get_all_room
13
+ end
14
+ end
15
+
16
+ context 'when response code 400 from hipchat api' do
17
+ before do
18
+ stub_request(:get, 'https://api.hipchat.com/v2/room?auth_token=abcde')
19
+ .to_return(status: 401)
20
+ end
21
+
22
+ it 'should raise error HipChat::Unauthorized' do
23
+ room = described_class.new('abcde')
24
+ expect do
25
+ room.get_all_room
26
+ end.to raise_error HipChat::Unauthorized
27
+ end
28
+ end
29
+
30
+ context 'when response code 404 from hipchat api' do
31
+ before do
32
+ stub_request(:get, 'https://api.hipchat.com/v2/room?auth_token=abcde')
33
+ .to_return(status: 404)
34
+ end
35
+
36
+ it 'should raise error HipChat::UnknownRoom' do
37
+ room = described_class.new('abcde')
38
+ expect do
39
+ room.get_all_room
40
+ end.to raise_error HipChat::UnknownRoom
41
+ end
42
+ end
43
+
44
+ context 'when response code other type from hipchat api' do
45
+ before do
46
+ stub_request(:get, 'https://api.hipchat.com/v2/room?auth_token=abcde')
47
+ .to_return(status: 400)
48
+ end
49
+
50
+ it 'should raise error HipChat::UnknownResponseCode' do
51
+ room = described_class.new('abcde')
52
+ expect do
53
+ room.get_all_room
54
+ end.to raise_error HipChat::UnknownResponseCode
55
+ end
56
+ end
57
+ end
58
+ end
@@ -1,6 +1,4 @@
1
- require 'spec_helper'
2
-
3
- describe HipchatSearcher::Searcher do
1
+ describe HipchatSearcher::SearchProxy::Grep do
4
2
  def searcher(pattern, result, options={})
5
3
  described_class.new(pattern, result, options)
6
4
  end
@@ -22,13 +20,13 @@ describe HipchatSearcher::Searcher do
22
20
  end
23
21
  end
24
22
 
25
- describe '#extended_items' do
23
+ describe '#around_items' do
26
24
  describe 'you speciy :before_context option' do
27
25
  context 'when specify the range is included in the array' do
28
- subject { searcher(pattern, result, before_context: '3').extended_items(3) }
26
+ subject { searcher(pattern, result, before_context: '3').around_items(3) }
29
27
 
30
28
  let(:pattern) { 'fuga' }
31
- let(:result) { double(:result, items: %w|1 2 3 4 5 6|) }
29
+ let(:result) { double(:result, items: %w|1 2 3 4 5 6|.reverse) }
32
30
 
33
31
  it 'should return the array of range to include index you specify' do
34
32
  should == %w|1 2 3 4|
@@ -36,10 +34,10 @@ describe HipchatSearcher::Searcher do
36
34
  end
37
35
 
38
36
  context 'when specify the range is exluded in the array' do
39
- subject { searcher(pattern, result, before_context: '3').extended_items(1) }
37
+ subject { searcher(pattern, result, before_context: '3').around_items(1) }
40
38
 
41
39
  let(:pattern) { 'fuga' }
42
- let(:result) { double(:result, items: %w|1 2 3 4 5 6|) }
40
+ let(:result) { double(:result, items: %w|1 2 3 4 5 6|.reverse) }
43
41
 
44
42
  it 'should return the array includes range until index from first' do
45
43
  should == %w|1 2|
@@ -49,21 +47,21 @@ describe HipchatSearcher::Searcher do
49
47
 
50
48
  describe 'you specify :after_context option' do
51
49
  context 'when specify the range is included in the array' do
52
- subject { searcher(pattern, result, after_context: '3').extended_items(1) }
50
+ subject { searcher(pattern, result, after_context: '3').around_items(3) }
53
51
 
54
52
  let(:pattern) { 'fuga' }
55
- let(:result) { double(:result, items: %w|1 2 3 4 5 6|) }
53
+ let(:result) { double(:result, items: %w|1 2 3 4 5 6|.reverse) }
56
54
 
57
55
  it 'should return the array of range to include index you specify' do
58
- should == %w|2 3 4 5|
56
+ should == %w|4 5 6|
59
57
  end
60
58
  end
61
59
 
62
60
  context 'when specify the range is exluded in the array' do
63
- subject { searcher(pattern, result, after_context: '3').extended_items(4) }
61
+ subject { searcher(pattern, result, after_context: '3').around_items(4) }
64
62
 
65
63
  let(:pattern) { 'fuga' }
66
- let(:result) { double(:result, items: %w|1 2 3 4 5 6|) }
64
+ let(:result) { double(:result, items: %w|1 2 3 4 5 6|.reverse) }
67
65
 
68
66
  it 'should return the array includes range until the end from index' do
69
67
  should == %w|5 6|
@@ -73,10 +71,10 @@ describe HipchatSearcher::Searcher do
73
71
 
74
72
  describe 'you specify :context option' do
75
73
  context 'when specify the range is included in the array' do
76
- subject { searcher(pattern, result, context: '2').extended_items(3) }
74
+ subject { searcher(pattern, result, context: '2').around_items(3) }
77
75
 
78
76
  let(:pattern) { 'fuga' }
79
- let(:result) { double(:result, items: %w|1 2 3 4 5 6|) }
77
+ let(:result) { double(:result, items: %w|1 2 3 4 5 6|.reverse) }
80
78
 
81
79
  it 'should return the array of range to surround index you specify' do
82
80
  should == %w|2 3 4 5 6|
@@ -84,10 +82,10 @@ describe HipchatSearcher::Searcher do
84
82
  end
85
83
 
86
84
  context 'when specify the range is exluded in the array' do
87
- subject { searcher(pattern, result, context: '5').extended_items(3) }
85
+ subject { searcher(pattern, result, context: '5').around_items(3) }
88
86
 
89
87
  let(:pattern) { 'fuga' }
90
- let(:result) { double(:result, items: %w|1 2 3 4 5 6|) }
88
+ let(:result) { double(:result, items: %w|1 2 3 4 5 6|.reverse) }
91
89
 
92
90
  it 'should return the array includes range to surround as much as possible' do
93
91
  should == %w|1 2 3 4 5 6|
@@ -97,61 +95,10 @@ describe HipchatSearcher::Searcher do
97
95
  end
98
96
 
99
97
  describe '#search' do
100
- context 'when matches pattern in messages' do
101
- subject { searcher(pattern, result).search }
102
-
103
- let(:pattern) { 'yare' }
104
- let(:result) do
105
- response = File.read(File.join('spec', 'data', 'item-list.json'))
106
- HipchatSearcher::Result.new(response).tap do |r|
107
- r.room = "Joestars"
108
- end
109
- end
110
- let(:search_result) do
111
- "\e[4;39;49mJoestars\e[0m" + "\n" + \
112
- " Date: 2014-05-30T01:38:16.741565+00:00" + "\n" + \
113
- " @jotaro: \e[0;31;49myare\e[0m\e[0;31;49myare\e[0m daze" + "\n" + \
114
- "\n"
115
- end
116
-
117
- it 'should print roomname & search result' do
118
- expect do
119
- subject
120
- end.to output(search_result).to_stdout
121
- end
122
- end
123
-
124
- context 'when matches pattern in many messages' do
125
- subject { searcher(pattern, result).search }
126
-
127
- let(:pattern) { 'ze' }
128
- let(:result) do
129
- response = File.read(File.join('spec', 'data', 'item-list.json'))
130
- HipchatSearcher::Result.new(response).tap do |r|
131
- r.room = "Joestars"
132
- end
133
- end
134
- let(:search_result) do
135
- "\e[4;39;49mJoestars\e[0m" + "\n" + \
136
- " Date: 2014-05-30T01:38:16.741565+00:00" + "\n" + \
137
- " @jotaro: yareyare da\e[0;31;49mze\e[0m" + "\n" \
138
- "\n" + \
139
- " Date: 2014-06-09T11:29:10.209014+00:00" + "\n" + \
140
- " @polnareff: a... arinomama ima okotta koto wo hanasu \e[0;31;49mze\e[0m" + "\n" + \
141
- "\n"
142
- end
143
-
144
- it 'should print roomname & search result' do
145
- expect do
146
- subject
147
- end.to output(search_result).to_stdout
148
- end
149
- end
150
-
151
98
  context 'when search_option --before_context' do
152
99
  subject { searcher(pattern, result, before_context: '1').search }
153
100
 
154
- let(:pattern) { 'inu' }
101
+ let(:pattern) { 'chirp' }
155
102
  let(:result) do
156
103
  response = File.read(File.join('spec', 'data', 'item-list-with-overlap.json'))
157
104
  HipchatSearcher::Result.new(response).tap do |r|
@@ -160,11 +107,11 @@ describe HipchatSearcher::Searcher do
160
107
  end
161
108
  let(:search_result) do
162
109
  "\e[4;39;49mJoestars\e[0m" + "\n" + \
163
- " Date: 2014-06-10T08:36:09.281643+00:00" + "\n" + \
164
- " @abdul: chirp chirp chirp" + "\n" + \
165
- "\n" + \
166
110
  " Date: 2014-06-11T19:20:47.726182+00:00" + "\n" + \
167
- " @lggy: \e[0;31;49minu\e[0m zuki no kodomo ha migoroshiniha dekine-ze" + "\n" + \
111
+ " @lggy: inu zuki no kodomo ha migoroshiniha dekine-ze" + "\n" + \
112
+ "\n" + \
113
+ " Date: 2014-06-10T08:36:09.281643+00:00" + "\n" + \
114
+ " @abdul: \e[0;31;49mchirp\e[0m \e[0;31;49mchirp\e[0m \e[0;31;49mchirp\e[0m" + "\n" + \
168
115
  "\n"
169
116
  end
170
117
 
@@ -190,8 +137,8 @@ describe HipchatSearcher::Searcher do
190
137
  " Date: 2014-05-30T01:39:02.186319+00:00" + "\n" + \
191
138
  " @noriaki: \e[0;31;49mrero\e[0m\e[0;31;49mrero\e[0m" + "\n" + \
192
139
  "\n" + \
193
- " Date: 2014-06-09T11:29:10.209014+00:00" + "\n" + \
194
- " @polnareff: a... arinomama ima okotta koto wo hanasu ze" + "\n" + \
140
+ " Date: 2014-05-30T01:38:16.741565+00:00" + "\n" + \
141
+ " @jotaro: yareyare daze" + "\n" + \
195
142
  "\n"
196
143
  end
197
144
 
@@ -203,7 +150,7 @@ describe HipchatSearcher::Searcher do
203
150
  end
204
151
 
205
152
  context 'when search_option --context' do
206
- subject { searcher(pattern, result, context: '2').search }
153
+ subject { searcher(pattern, result, context: '1').search }
207
154
 
208
155
  let(:pattern) { 'chirp' }
209
156
  let(:result) do
@@ -214,17 +161,14 @@ describe HipchatSearcher::Searcher do
214
161
  end
215
162
  let(:search_result) do
216
163
  "\e[4;39;49mJoestars\e[0m" + "\n" + \
217
- " Date: 2014-05-30T01:39:02.186319+00:00" + "\n" + \
218
- " @noriaki: rerorero" + "\n" + \
219
- "\n" + \
220
- " Date: 2014-06-09T11:29:10.209014+00:00" + "\n" + \
221
- " @polnareff: a... arinomama ima okotta koto wo hanasu ze" + "\n" + \
164
+ " Date: 2014-06-11T19:20:47.726182+00:00" + "\n" + \
165
+ " @lggy: inu zuki no kodomo ha migoroshiniha dekine-ze" + "\n" + \
222
166
  "\n" + \
223
167
  " Date: 2014-06-10T08:36:09.281643+00:00" + "\n" + \
224
168
  " @abdul: \e[0;31;49mchirp\e[0m \e[0;31;49mchirp\e[0m \e[0;31;49mchirp\e[0m" + "\n" + \
225
169
  "\n" + \
226
- " Date: 2014-06-11T19:20:47.726182+00:00" + "\n" + \
227
- " @lggy: inu zuki no kodomo ha migoroshiniha dekine-ze" + "\n" + \
170
+ " Date: 2014-06-09T11:29:10.209014+00:00" + "\n" + \
171
+ " @polnareff: a... arinomama ima okotta koto wo hanasu ze" + "\n" + \
228
172
  "\n"
229
173
  end
230
174
 
@@ -247,20 +191,20 @@ describe HipchatSearcher::Searcher do
247
191
  end
248
192
  let(:search_result) do
249
193
  "\e[4;39;49mJoestars\e[0m" + "\n" + \
250
- " Date: 2014-05-30T01:38:16.741565+00:00" + "\n" + \
251
- " @jotaro: yareyare da\e[0;31;49mze\e[0m" + "\n" + \
194
+ " Date: 2014-06-11T19:20:47.726182+00:00" + "\n" + \
195
+ " @lggy: inu zuki no kodomo ha migoroshiniha dekine-\e[0;31;49mze\e[0m" + "\n" + \
252
196
  "\n" + \
253
- " Date: 2014-05-30T01:39:02.186319+00:00" + "\n" + \
254
- " @noriaki: rerorero" + "\n" + \
197
+ " Date: 2014-06-10T08:36:09.281643+00:00" + "\n" + \
198
+ " @abdul: chirp chirp chirp" + "\n" + \
255
199
  "\n" + \
256
200
  " Date: 2014-06-09T11:29:10.209014+00:00" + "\n" + \
257
201
  " @polnareff: a... arinomama ima okotta koto wo hanasu \e[0;31;49mze\e[0m" + "\n" + \
258
202
  "\n" + \
259
- " Date: 2014-06-10T08:36:09.281643+00:00" + "\n" + \
260
- " @abdul: chirp chirp chirp" + "\n" + \
203
+ " Date: 2014-05-30T01:39:02.186319+00:00" + "\n" + \
204
+ " @noriaki: rerorero" + "\n" + \
261
205
  "\n" + \
262
- " Date: 2014-06-11T19:20:47.726182+00:00" + "\n" + \
263
- " @lggy: inu zuki no kodomo ha migoroshiniha dekine-\e[0;31;49mze\e[0m" + "\n" + \
206
+ " Date: 2014-05-30T01:38:16.741565+00:00" + "\n" + \
207
+ " @jotaro: yareyare da\e[0;31;49mze\e[0m" + "\n" + \
264
208
  "\n"
265
209
  end
266
210
 
@@ -271,37 +215,28 @@ describe HipchatSearcher::Searcher do
271
215
  end
272
216
  end
273
217
 
274
- context "when don't match pattern in messages" do
275
- subject { searcher(pattern, result).search }
218
+ context 'when you specify option date and include item date no later than this date' do
219
+ subject { searcher(pattern, result, date: '2014-06-10', before_context: 1).search }
276
220
 
277
- let(:pattern) { 'abcd' }
221
+ let(:pattern) { 'ze' }
278
222
  let(:result) do
279
- response = File.read(File.join('spec', 'data', 'item-list.json'))
280
- HipchatSearcher::Result.new(response)
281
- end
282
-
283
- it { should be_nil }
284
-
285
- it 'should no output to stdout' do
286
- expect do
287
- subject
288
- end.to output('').to_stdout
223
+ response = File.read(File.join('spec', 'data', 'item-list-with-overlap.json'))
224
+ HipchatSearcher::Result.new(response).tap do |r|
225
+ r.room = "Joestars"
226
+ end
289
227
  end
290
- end
291
-
292
- context "when pattern can't convert regexp" do
293
- subject { searcher(pattern, result).search }
294
228
 
295
- let(:pattern) { nil }
296
- let(:result) do
297
- response = File.read(File.join('spec', 'data', 'item-list.json'))
298
- HipchatSearcher::Result.new(response)
229
+ let(:search_result) do
230
+ "\e[4;39;49mJoestars\e[0m" + "\n" + \
231
+ " Date: 2014-06-11T19:20:47.726182+00:00" + "\n" + \
232
+ " @lggy: inu zuki no kodomo ha migoroshiniha dekine-\e[0;31;49mze\e[0m" + "\n" + \
233
+ "\n"
299
234
  end
300
235
 
301
- it 'should raise exception' do
236
+ it 'should print the matched message since option date' do
302
237
  expect do
303
238
  subject
304
- end.to raise_error
239
+ end.to output(search_result).to_stdout
305
240
  end
306
241
  end
307
242
  end
@@ -311,7 +246,7 @@ describe HipchatSearcher::Searcher do
311
246
  hash = JSON.parse(File.read(path))
312
247
  items = ::Hashie::Mash.new(hash).items
313
248
  items.map do |itm|
314
- itm.extend(HipchatSearcher::Searcher::ItemExtention).tap do |i|
249
+ itm.extend(HipchatSearcher::ItemExtention).tap do |i|
315
250
  i.pattern = Regexp.new(pattern)
316
251
  end
317
252
  end