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,267 @@
1
+ describe HipchatSearcher::SearchProxy::Simple do
2
+ def searcher(pattern, result, options={})
3
+ described_class.new(pattern, result, options)
4
+ end
5
+
6
+ describe '#before?' do
7
+ context 'when no "date" option' do
8
+ subject { searcher("hoge", double(:result)).before?("2014-07-13") }
9
+
10
+ it { should be_falsy }
11
+ end
12
+
13
+ context 'when argument is nil' do
14
+ subject { searcher("hoge", double(:result)).before?(nil) }
15
+
16
+ it { should be_falsy }
17
+ end
18
+
19
+ context 'when option date is later than argument date' do
20
+ subject { searcher("hoge", double(:result), date: "2014-07-13").before?("2014-07-10") }
21
+
22
+ it { should be_truthy }
23
+ end
24
+
25
+ context 'when option date is equal to argument date ' do
26
+ subject { searcher("hoge", double(:result), date: "2014-07-13").before?("2014-07-13") }
27
+
28
+ it { should be_falsy }
29
+ end
30
+
31
+ context 'when option date is newer than argument date ' do
32
+ subject { searcher("hoge", double(:result), date: "2014-07-13").before?("2014-07-20") }
33
+
34
+ it { should be_falsy }
35
+ end
36
+ end
37
+
38
+ describe '#items' do
39
+ subject { searcher(pattern, result).items }
40
+
41
+ let(:pattern) { 'hoge' }
42
+ let(:result) do
43
+ response = File.read(File.join('spec', 'data', 'item-list.json'))
44
+ HipchatSearcher::Result.new(response)
45
+ end
46
+
47
+ it { should be_instance_of Array }
48
+
49
+ it 'should return array with Hashie::Mash objects' do
50
+ subject.first.should be_instance_of ::Hashie::Mash
51
+ subject.last.should be_instance_of ::Hashie::Mash
52
+ end
53
+ end
54
+
55
+ describe '#search' do
56
+ context 'when matches pattern in messages' do
57
+ subject { searcher(pattern, result).search }
58
+
59
+ let(:pattern) { 'yare' }
60
+ let(:result) do
61
+ response = File.read(File.join('spec', 'data', 'item-list.json'))
62
+ HipchatSearcher::Result.new(response).tap do |r|
63
+ r.room = "Joestars"
64
+ end
65
+ end
66
+ let(:search_result) do
67
+ "\e[4;39;49mJoestars\e[0m" + "\n" + \
68
+ " Date: 2014-05-30T01:38:16.741565+00:00" + "\n" + \
69
+ " @jotaro: \e[0;31;49myare\e[0m\e[0;31;49myare\e[0m daze" + "\n" + \
70
+ "\n"
71
+ end
72
+
73
+ it 'should print roomname & search result' do
74
+ expect do
75
+ subject
76
+ end.to output(search_result).to_stdout
77
+ end
78
+ end
79
+
80
+ context 'when matches pattern in many messages' do
81
+ subject { searcher(pattern, result).search }
82
+
83
+ let(:pattern) { 'ze' }
84
+ let(:result) do
85
+ response = File.read(File.join('spec', 'data', 'item-list.json'))
86
+ HipchatSearcher::Result.new(response).tap do |r|
87
+ r.room = "Joestars"
88
+ end
89
+ end
90
+ let(:search_result) do
91
+ "\e[4;39;49mJoestars\e[0m" + "\n" + \
92
+ " Date: 2014-06-09T11:29:10.209014+00:00" + "\n" + \
93
+ " @polnareff: a... arinomama ima okotta koto wo hanasu \e[0;31;49mze\e[0m" + "\n" + \
94
+ "\n" + \
95
+ " Date: 2014-05-30T01:38:16.741565+00:00" + "\n" + \
96
+ " @jotaro: yareyare da\e[0;31;49mze\e[0m" + "\n" \
97
+ "\n"
98
+ end
99
+
100
+ it 'should print roomname & search result' do
101
+ expect do
102
+ subject
103
+ end.to output(search_result).to_stdout
104
+ end
105
+ end
106
+
107
+ context 'when you specify option date and include item date no later than this date' do
108
+ subject { searcher(pattern, result, date: '2014-06-10').search }
109
+
110
+ let(:pattern) { 'ze' }
111
+ let(:result) do
112
+ response = File.read(File.join('spec', 'data', 'item-list-with-overlap.json'))
113
+ HipchatSearcher::Result.new(response).tap do |r|
114
+ r.room = "Joestars"
115
+ end
116
+ end
117
+
118
+ let(:search_result) do
119
+ "\e[4;39;49mJoestars\e[0m" + "\n" + \
120
+ " Date: 2014-06-11T19:20:47.726182+00:00" + "\n" + \
121
+ " @lggy: inu zuki no kodomo ha migoroshiniha dekine-\e[0;31;49mze\e[0m" + "\n" + \
122
+ "\n"
123
+ end
124
+
125
+ it 'should print the matched message since option date' do
126
+ expect do
127
+ subject
128
+ end.to output(search_result).to_stdout
129
+ end
130
+ end
131
+
132
+ context "when don't match pattern in messages" do
133
+ subject { searcher(pattern, result).search }
134
+
135
+ let(:pattern) { 'abcd' }
136
+ let(:result) do
137
+ response = File.read(File.join('spec', 'data', 'item-list.json'))
138
+ HipchatSearcher::Result.new(response)
139
+ end
140
+
141
+ it { should be_nil }
142
+
143
+ it 'should no output to stdout' do
144
+ expect do
145
+ subject
146
+ end.to output('').to_stdout
147
+ end
148
+ end
149
+
150
+ context "when pattern can't convert regexp" do
151
+ subject { searcher(pattern, result).search }
152
+
153
+ let(:pattern) { nil }
154
+ let(:result) do
155
+ response = File.read(File.join('spec', 'data', 'item-list.json'))
156
+ HipchatSearcher::Result.new(response)
157
+ end
158
+
159
+ it 'should raise exception' do
160
+ expect do
161
+ subject
162
+ end.to raise_error
163
+ end
164
+ end
165
+ end
166
+
167
+ describe '#puts_search_result' do
168
+ def extended_items(path, pattern)
169
+ hash = JSON.parse(File.read(path))
170
+ items = ::Hashie::Mash.new(hash).items
171
+ items.map do |itm|
172
+ itm.extend(HipchatSearcher::ItemExtention).tap do |i|
173
+ i.pattern = Regexp.new(pattern)
174
+ end
175
+ end
176
+ end
177
+
178
+ context 'when only person in room' do
179
+ subject { searcher(pattern, result).puts_search_result(item) }
180
+
181
+ let(:pattern) { 'yare' }
182
+ let(:result) { double(:result, room: 'Joestars') }
183
+ let(:item) do
184
+ path = File.join('spec', 'data', 'item-list.json')
185
+ extended_items(path, pattern).first
186
+ end
187
+
188
+ let(:search_result) do
189
+ "\e[4;39;49mJoestars\e[0m" + "\n" + \
190
+ " Date: 2014-05-30T01:38:16.741565+00:00" + "\n" + \
191
+ " @jotaro: \e[0;31;49myare\e[0m\e[0;31;49myare\e[0m daze" + "\n" + \
192
+ "\n"
193
+ end
194
+
195
+ it 'should print message of search result' do
196
+ expect do
197
+ subject
198
+ end.to output(search_result).to_stdout
199
+ end
200
+ end
201
+
202
+ context 'when person and bot in room' do
203
+ subject { searcher(pattern, result).puts_search_result(item) }
204
+
205
+ let(:pattern) { 'mgi166' }
206
+ let(:result) { double(:result, room: 'Joestars') }
207
+ let(:item) do
208
+ path = File.join('spec', 'data', 'item-list-with-bot.json')
209
+ extended_items(path, pattern).first
210
+ end
211
+
212
+ let(:search_result) do
213
+ "\e[4;39;49mJoestars\e[0m" + "\n" + \
214
+ ' Date: 2014-06-17T08:14:48.305590+00:00' + "\n" + \
215
+ " @GitHub: \e[0;31;49mmgi166\e[0m commented on pull request 118 ..." + "\n\n"
216
+ end
217
+
218
+ it 'should print message of search result' do
219
+ expect do
220
+ subject
221
+ end.to output(search_result).to_stdout
222
+ end
223
+ end
224
+
225
+ context "when user options specified but the user don't speak this message" do
226
+ subject { searcher(pattern, result, user: 'jotaro').puts_search_result(item) }
227
+
228
+ let(:pattern) { 'ze' }
229
+ let(:result) { double(:result, room: 'Joestars') }
230
+ let(:item) do
231
+ path = File.join('spec', 'data', 'item-list.json')
232
+ extended_items(path, pattern).last
233
+ end
234
+
235
+ it 'should not print message' do
236
+ expect do
237
+ subject
238
+ end.to output('').to_stdout
239
+ end
240
+ end
241
+
242
+ context "when user options specified and the user speak this message" do
243
+ subject { searcher(pattern, result, user: 'jotaro').puts_search_result(item) }
244
+
245
+ let(:pattern) { 'ze' }
246
+ let(:result) { double(:result, room: 'Joestars') }
247
+ let(:item) do
248
+ path = File.join('spec', 'data', 'item-list.json')
249
+ extended_items(path, pattern).first
250
+ end
251
+
252
+ let(:search_result) do
253
+ "\e[4;39;49mJoestars\e[0m" + "\n" + \
254
+ " Date: 2014-05-30T01:38:16.741565+00:00" + "\n" + \
255
+ " @jotaro: yareyare da\e[0;31;49mze\e[0m" + "\n" \
256
+ "\n"
257
+ end
258
+
259
+ it 'should print message for specified user' do
260
+ expect do
261
+ subject
262
+ end.to output(search_result).to_stdout
263
+ end
264
+ end
265
+
266
+ end
267
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hipchat_searcher
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - mgi166
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-30 00:00:00.000000000 Z
11
+ date: 2014-07-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hipchat
@@ -108,6 +108,20 @@ dependencies:
108
108
  - - ">="
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: webmock
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
111
125
  - !ruby/object:Gem::Dependency
112
126
  name: coveralls
113
127
  requirement: !ruby/object:Gem::Requirement
@@ -131,6 +145,7 @@ extensions: []
131
145
  extra_rdoc_files: []
132
146
  files:
133
147
  - ".gitignore"
148
+ - ".rspec"
134
149
  - ".travis.yml"
135
150
  - CHANGELOG.md
136
151
  - Gemfile
@@ -142,19 +157,29 @@ files:
142
157
  - lib/hipchat_searcher.rb
143
158
  - lib/hipchat_searcher/command.rb
144
159
  - lib/hipchat_searcher/config.rb
160
+ - lib/hipchat_searcher/deep_runner.rb
161
+ - lib/hipchat_searcher/item_extention.rb
145
162
  - lib/hipchat_searcher/message.rb
146
163
  - lib/hipchat_searcher/options.rb
147
164
  - lib/hipchat_searcher/result.rb
148
165
  - lib/hipchat_searcher/room.rb
149
- - lib/hipchat_searcher/searcher.rb
166
+ - lib/hipchat_searcher/runner.rb
167
+ - lib/hipchat_searcher/search_proxy.rb
168
+ - lib/hipchat_searcher/search_proxy/grep.rb
169
+ - lib/hipchat_searcher/search_proxy/simple.rb
150
170
  - lib/hipchat_searcher/version.rb
171
+ - spec/data/item-list-100.json
172
+ - spec/data/item-list-empty.json
151
173
  - spec/data/item-list-with-bot.json
152
174
  - spec/data/item-list-with-overlap.json
153
175
  - spec/data/item-list.json
154
176
  - spec/data/room-list.txt
155
177
  - spec/hipchat_searcher_spec.rb
178
+ - spec/lib/hipchat_searcher/message_spec.rb
156
179
  - spec/lib/hipchat_searcher/result_spec.rb
157
- - spec/lib/hipchat_searcher/searcher_spec.rb
180
+ - spec/lib/hipchat_searcher/room_spec.rb
181
+ - spec/lib/hipchat_searcher/search_proxy/grep_spec.rb
182
+ - spec/lib/hipchat_searcher/search_proxy/simple_spec.rb
158
183
  - spec/spec_helper.rb
159
184
  homepage: https://github.com/mgi166/hipchat_searcher
160
185
  licenses:
@@ -181,11 +206,16 @@ signing_key:
181
206
  specification_version: 4
182
207
  summary: Search hipchat log on terminal
183
208
  test_files:
209
+ - spec/data/item-list-100.json
210
+ - spec/data/item-list-empty.json
184
211
  - spec/data/item-list-with-bot.json
185
212
  - spec/data/item-list-with-overlap.json
186
213
  - spec/data/item-list.json
187
214
  - spec/data/room-list.txt
188
215
  - spec/hipchat_searcher_spec.rb
216
+ - spec/lib/hipchat_searcher/message_spec.rb
189
217
  - spec/lib/hipchat_searcher/result_spec.rb
190
- - spec/lib/hipchat_searcher/searcher_spec.rb
218
+ - spec/lib/hipchat_searcher/room_spec.rb
219
+ - spec/lib/hipchat_searcher/search_proxy/grep_spec.rb
220
+ - spec/lib/hipchat_searcher/search_proxy/simple_spec.rb
191
221
  - spec/spec_helper.rb
@@ -1,149 +0,0 @@
1
- require 'colorize'
2
-
3
- module HipchatSearcher
4
- class Searcher
5
- def initialize(pattern, result, options={})
6
- @result = result
7
- @options = options
8
- @pattern = Regexp.new(pattern, Regexp::IGNORECASE)
9
- @print_room = false
10
- @print_item = {}
11
- end
12
-
13
- def self.search(pattern, result, options)
14
- new(pattern, result, options).search
15
- end
16
-
17
- def extended_items(index)
18
- items[range(index)].map do |itm|
19
- itm.extend(ItemExtention).tap do |i|
20
- i.pattern = @pattern
21
- end
22
- end
23
- end
24
-
25
- def items
26
- @items ||= @result.items
27
- end
28
-
29
- def puts_search_result(item)
30
- if option_user?
31
- if @options[:user] == item.mention_name
32
- puts_contents(item)
33
- end
34
- else
35
- puts_contents(item)
36
- end
37
- end
38
-
39
- def search
40
- items.each_with_index do |item, idx|
41
- next unless @pattern =~ item.message
42
-
43
- if option_search?
44
- extended_items(idx).each do |itm|
45
- puts_search_result(itm)
46
- end
47
- else
48
- ext = item.extend(ItemExtention)
49
- ext.pattern = @pattern
50
- puts_search_result(ext)
51
- end
52
- end
53
-
54
- nil
55
- end
56
-
57
- private
58
-
59
- def option_after?
60
- !!@options[:after_context]
61
- end
62
-
63
- def option_before?
64
- !!@options[:before_context]
65
- end
66
-
67
- def option_context?
68
- !!@options[:context]
69
- end
70
-
71
- def option_search?
72
- option_after? || option_before? || option_context?
73
- end
74
-
75
- def option_user?
76
- !!@options[:user]
77
- end
78
-
79
- def print_room?
80
- !!@print_room
81
- end
82
-
83
- def puts_room
84
- @print_room = true
85
- puts room.underline
86
- end
87
-
88
- def puts_contents(item)
89
- print_room? ? nil : puts_room
90
- puts item.contents unless printed?(item.id)
91
- end
92
-
93
- def range(index)
94
- param = []
95
-
96
- case
97
- when option_before?
98
- _i = index - @options[:before_context].to_i
99
- param = _i < 0 ? [0, index] : [_i, index]
100
- when option_after?
101
- _i = index + @options[:after_context].to_i
102
- param = [index, _i]
103
- when option_context?
104
- _i = index - @options[:context].to_i
105
- _j = index + @options[:context].to_i
106
- param = _i < 0 ? [0, _j] : [_i, _j]
107
- end
108
-
109
- Range.new(*param)
110
- end
111
-
112
- def room
113
- @result.room
114
- end
115
-
116
- def printed?(id)
117
- if @print_item.key?(id)
118
- true
119
- else
120
- @print_item[id] = 1
121
- false
122
- end
123
- end
124
-
125
- module ItemExtention
126
- attr_accessor :pattern
127
-
128
- def contents
129
- "%s\n%s\n\n" % [_date, _message]
130
- end
131
-
132
- def mention_name
133
- self.from.mention_name rescue self.from
134
- end
135
-
136
- def _date
137
- " Date: %s" % self.date
138
- end
139
-
140
- def _message
141
- msg = self.message.gsub(pattern) do |matched|
142
- matched.colorize(:red)
143
- end
144
-
145
- " @%s: %s" % [mention_name, msg]
146
- end
147
- end
148
- end
149
- end