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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1fd098a01a0e50974e9e94aad6531217212b4f02
4
- data.tar.gz: ff849519de62525a531f4705e0f4dcfe4c0c7f39
3
+ metadata.gz: a8309c6be22ec8e7c5c8f3f84f6f6821cd6cb5b8
4
+ data.tar.gz: f63d664d10d4d230be61613ab41522969985c215
5
5
  SHA512:
6
- metadata.gz: 88be4494c9f37e92b7668dcee89e9b57b106d788bf6b2d50c3ea967c5fb6d21140dff7ad5f9413cf69290677766b404b5c5a0f849e932bf81927a627dd682827
7
- data.tar.gz: 2633cdb60c1494e9545048b0cc73031c647f876afa64bce65644c3c20043857752b6448db960ad4f285ed9a3ff042bdc3121dc312d3a37aeb819125a1d37088f
6
+ metadata.gz: cd7a35657366b4ab599b2a455c4cf7e1b1bf66aa9b659d6b26adf4f9caf46087e666ab4dda00aa8dae24224097e9d7e77d25b64049a4a20d620112717edc3fcd
7
+ data.tar.gz: 32da7707e122689d756fd2e137bd6521f4c1c59ea44c7142e9da0e91c86025cf56b5939fb2bdc15fe5f79bf74e8ae0a3052daff4afb16c90177c486032ae5c41
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format documentation
3
+ --require spec_helper
data/CHANGELOG.md CHANGED
@@ -1,3 +1,15 @@
1
+ # 1.0.0
2
+ ### Feature
3
+ * Newer search result is output to the upstream
4
+ * Enable to search option `-e` named `deep-search`
5
+ * Specify this option, search the log older than 75 recently
6
+ * Specify `date` option, also search the log with option `-e`
7
+ * The reason is that refer to the `Bugfix`
8
+ * Refactor the code
9
+
10
+ ### Bugfix
11
+ * Date option searches the date *until* you specify, Fix to search to the date of latest from you specified
12
+
1
13
  # 0.0.3
2
14
  ### Feature
3
15
  * Enable to search option `-A`, `-B`, `-C`, such as `grep`
data/README.md CHANGED
@@ -25,8 +25,7 @@ echo {access_token} > ~/.hps
25
25
  `hipchat_searcher` search for a regular expression the words that you specify.
26
26
  In the specifications of the [hipchat api](https://www.hipchat.com/docs/apiv2/), `hipchat_searcher` search of the upcoming 100 comments.
27
27
 
28
- * Search the words in all room
29
- * But it searches ALL the room that user know, so there may be heavy.
28
+ * Search the words in all room. (but it searches ALL the room that user know, so there may be heavy)
30
29
 
31
30
  ```
32
31
  hps word
@@ -38,6 +37,24 @@ hps word
38
37
  hps word -r room-name
39
38
  ```
40
39
 
40
+ * Search the words of trailing context after each match. Such as `grep` command option.
41
+
42
+ ```
43
+ hps word -A 2
44
+ ```
45
+
46
+ * Search the words of trailing context before each match. Such as `grep` command option.
47
+
48
+ ```
49
+ hps word -B 2
50
+ ```
51
+
52
+ * Search the words of trailing context surrounding each match. The following is equivalent to -A 2 -B 2.
53
+
54
+ ```
55
+ hps word -C 2
56
+ ```
57
+
41
58
  * Search the words that specified user talks
42
59
 
43
60
  ```
data/bin/hps CHANGED
@@ -23,4 +23,9 @@ end.to_hash
23
23
  options = HipchatSearcher::Options.new(hash)
24
24
 
25
25
  # run command
26
- HipchatSearcher::Command.run(ARGV.shift, options)
26
+ begin
27
+ HipchatSearcher::Command.run(ARGV.shift, options)
28
+ rescue Interrupt
29
+ $stderr.puts "HipchatSearcher execute interrupt, exit..."
30
+ exit 1
31
+ end
@@ -25,5 +25,6 @@ Gem::Specification.new do |spec|
25
25
  spec.add_development_dependency "bundler", "~> 1.6"
26
26
  spec.add_development_dependency "rake"
27
27
  spec.add_development_dependency "rspec"
28
+ spec.add_development_dependency "webmock"
28
29
  spec.add_development_dependency "coveralls"
29
30
  end
@@ -1,8 +1,13 @@
1
1
  require 'hipchat_searcher/command'
2
2
  require 'hipchat_searcher/config'
3
+ require 'hipchat_searcher/item_extention'
3
4
  require 'hipchat_searcher/message'
4
5
  require 'hipchat_searcher/options'
6
+ require 'hipchat_searcher/search_proxy'
7
+ require 'hipchat_searcher/search_proxy/simple'
8
+ require 'hipchat_searcher/search_proxy/grep'
5
9
  require 'hipchat_searcher/result'
6
10
  require 'hipchat_searcher/room'
7
- require 'hipchat_searcher/searcher'
11
+ require 'hipchat_searcher/runner'
12
+ require 'hipchat_searcher/deep_runner'
8
13
  require 'hipchat_searcher/version'
@@ -3,7 +3,9 @@ module HipchatSearcher
3
3
  def initialize(pattern, options)
4
4
  @pattern = pattern
5
5
  @options = options
6
- @config = Config.new
6
+ @options.config = Config.new
7
+
8
+ config_valid!
7
9
  end
8
10
 
9
11
  def self.run(pattern, options)
@@ -11,22 +13,58 @@ module HipchatSearcher
11
13
  end
12
14
 
13
15
  def run
14
- all_room = Room.new(@config.token, @options.room_options).all_room
15
-
16
- rooms = if @options.room?
17
- room_names = @options.room.split(',')
18
- all_room.select do |r|
19
- room_names.include?(r.name)
20
- end
21
- else
22
- all_room
23
- end
16
+ if @options.deep?
17
+ deep_run
18
+ else
19
+ simple_run
20
+ end
21
+ end
24
22
 
25
- message = Message.new(@config.token, @options.message_options)
23
+ def deep_run
24
+ rooms.each do |room|
25
+ DeepRunner.run(@pattern, room, @options)
26
+ end
27
+ end
26
28
 
29
+ def simple_run
27
30
  rooms.each do |room|
28
- hist = message.history(room)
29
- Searcher.search(@pattern, hist, @options.search_options)
31
+ Runner.run(@pattern, room, @options)
32
+ end
33
+ end
34
+
35
+ private
36
+
37
+ def all_room
38
+ Room.new(@options.config.token, @options.room_options).all_room
39
+ end
40
+
41
+ def config_valid!
42
+ unless @options.config.valid?
43
+ print_no_authorization_token
44
+ exit 1
45
+ end
46
+ end
47
+
48
+ def print_no_authorization_token
49
+ puts <<-EOS
50
+ Authorization token required.
51
+
52
+ * To create config file, run this command
53
+ `echo {auth_token} > ~/.hps`
54
+
55
+ * To get new auth_token, visit and sign in.
56
+ https://www.hipchat.com/
57
+ EOS
58
+ end
59
+
60
+ def rooms
61
+ if @options.room?
62
+ room_names = @options.room.split(',')
63
+ all_room.select do |r|
64
+ room_names.include?(r.name)
65
+ end
66
+ else
67
+ all_room
30
68
  end
31
69
  end
32
70
  end
@@ -0,0 +1,27 @@
1
+ module HipchatSearcher
2
+ class DeepRunner < Runner
3
+ def run
4
+ result = @message.history(@room)
5
+ i = 1
6
+ while result.items.size == limit
7
+ break if @options.end?
8
+
9
+ if i == 1
10
+ i += 1
11
+ search(result)
12
+ else
13
+ option = { date: result.oldest_date }
14
+ result = @message.history(@room, option)
15
+ @limit_flag = true
16
+ search(result)
17
+ end
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def limit
24
+ @limit_flag ? 100 : 75
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,25 @@
1
+ module HipchatSearcher
2
+ module ItemExtention
3
+ attr_accessor :pattern
4
+
5
+ def contents
6
+ "%s\n%s\n\n" % [_date, _message]
7
+ end
8
+
9
+ def mention_name
10
+ self.from.mention_name rescue self.from
11
+ end
12
+
13
+ def _date
14
+ " Date: %s" % self.date
15
+ end
16
+
17
+ def _message
18
+ msg = self.message.gsub(pattern) do |matched|
19
+ matched.colorize(:red)
20
+ end
21
+
22
+ " @%s: %s" % [mention_name, msg]
23
+ end
24
+ end
25
+ end
@@ -8,15 +8,16 @@ module HipchatSearcher
8
8
  @client = ::HipChat::Client.new(@token, api_version: 'v2')
9
9
  end
10
10
 
11
- def get_history(id)
12
- @client[id].history(@options)
11
+ def get_history(id, options)
12
+ option = @options.merge(options)
13
+ @client[id].history(option)
13
14
  end
14
15
 
15
- def history(room)
16
+ def history(room, options={})
16
17
  id = room.id rescue room
17
18
  room_name = room.name rescue room
18
19
 
19
- h = get_history(id)
20
+ h = get_history(id, options)
20
21
  Result.new(h).tap {|r| r.room = room_name }
21
22
  end
22
23
  end
@@ -2,8 +2,12 @@ require 'hashie/mash'
2
2
 
3
3
  module HipchatSearcher
4
4
  class Options < Hashie::Mash
5
+ def deep?
6
+ !!self['deep'] || !!self['date']
7
+ end
8
+
5
9
  def message_options
6
- date? ? { date: date } : {}
10
+ {}
7
11
  end
8
12
 
9
13
  def room_options
@@ -16,9 +20,14 @@ module HipchatSearcher
16
20
  o.merge!(after_context: after_context) if after_context?
17
21
  o.merge!(before_context: before_context) if before_context?
18
22
  o.merge!(context: context) if context?
23
+ o.merge!(date: date) if date?
19
24
  end
20
25
  end
21
26
 
27
+ def grep_options?
28
+ !!before_context || !!after_context || !!context
29
+ end
30
+
22
31
  class << self
23
32
 
24
33
  # [shortname, longname, description]
@@ -26,7 +35,7 @@ module HipchatSearcher
26
35
  [
27
36
  ['r=', 'room=', 'Search only the log of the room that you specified'],
28
37
  ['u=', 'user=', 'Search only the log that specified user talk'],
29
- ['d=', 'date=', 'Search the log since specified date'],
38
+ ['d=', 'date=', 'Search the log until the day of latest from the date you specified'],
30
39
  ['A=', 'after_context=', 'Search the log that trails context after each match'],
31
40
  ['B=', 'before_context=', 'Search the log that trails context before each match'],
32
41
  ['C=', 'context=', 'Search the log that trails context surround each match'],
@@ -36,7 +45,8 @@ module HipchatSearcher
36
45
  # [shortname, longname, description]
37
46
  def with_boolean
38
47
  [
39
- ['a', 'archived', 'Include in the search of the room that have been archived']
48
+ ['a', 'archived', 'Include in the search of the room that have been archived'],
49
+ ['e', 'deep', 'Search older than comment of 75 recently']
40
50
  ]
41
51
  end
42
52
  end
@@ -11,12 +11,16 @@ module HipchatSearcher
11
11
  valid!
12
12
  end
13
13
 
14
- def rooms
15
- @response['items'].map {|i| ::Hashie::Mash.new(i) }
14
+ def items
15
+ @items ||= JSON.parse(@response)['items'].map {|i| ::Hashie::Mash.new(i) }
16
16
  end
17
17
 
18
- def items
19
- @messages = JSON.parse(@response)['items'].map {|i| ::Hashie::Mash.new(i) }
18
+ def oldest_date
19
+ items.first.date rescue nil
20
+ end
21
+
22
+ def rooms
23
+ @response['items'].map {|i| ::Hashie::Mash.new(i) }
20
24
  end
21
25
 
22
26
  def valid!
@@ -0,0 +1,33 @@
1
+ module HipchatSearcher
2
+ class Runner
3
+ def initialize(pattern, room, options)
4
+ @pattern = pattern
5
+ @room = room
6
+ @options = options
7
+ @message = Message.new(@options.config.token, @options.message_options)
8
+ end
9
+
10
+ def self.run(pattern, room, options)
11
+ new(pattern, room, options).run
12
+ end
13
+
14
+ def run
15
+ result = @message.history(@room)
16
+ search(result)
17
+ end
18
+
19
+ def search(result)
20
+ search_proxy.search(@pattern, result, @options.search_options)
21
+ end
22
+
23
+ private
24
+
25
+ def search_proxy
26
+ if @options.grep_options?
27
+ SearchProxy.new('grep')
28
+ else
29
+ SearchProxy.new('simple')
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,12 @@
1
+ module HipchatSearcher
2
+ class SearchProxy
3
+ def initialize(proxy)
4
+ @proxy = proxy
5
+ end
6
+
7
+ def search(pattern, result, options)
8
+ klass = self.class.const_get(@proxy.to_s.capitalize)
9
+ klass.search(pattern, result, options)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,91 @@
1
+ require 'colorize'
2
+ require 'date'
3
+
4
+ module HipchatSearcher
5
+ class SearchProxy
6
+ class Grep < Simple
7
+
8
+ def initialize(pattern, result, options={})
9
+ super
10
+ @print_room = false
11
+ @print_item = {}
12
+ end
13
+
14
+ def self.search(pattern, result, options)
15
+ new(pattern, result, options).search
16
+ end
17
+
18
+ def around_items(index)
19
+ items[range(index)].map do |itm|
20
+ itm.extend(HipchatSearcher::ItemExtention).tap do |i|
21
+ i.pattern = pattern
22
+ end
23
+ end
24
+ end
25
+
26
+ def search
27
+ items.each_with_index do |item, idx|
28
+ if before?(item.date)
29
+ @options[:end] = true
30
+ break
31
+ end
32
+
33
+ if pattern =~ item.message
34
+ around_items(idx).each do |itm|
35
+ puts_search_result(itm)
36
+ end
37
+ end
38
+ end
39
+
40
+ nil
41
+ end
42
+
43
+ private
44
+
45
+ def option_after?
46
+ !!@options[:after_context]
47
+ end
48
+
49
+ def option_before?
50
+ !!@options[:before_context]
51
+ end
52
+
53
+ def option_context?
54
+ !!@options[:context]
55
+ end
56
+
57
+ def printed?(id)
58
+ if @print_item.key?(id)
59
+ true
60
+ else
61
+ @print_item[id] = 1
62
+ false
63
+ end
64
+ end
65
+
66
+ def range(index)
67
+ param = []
68
+
69
+ case
70
+ when option_before?
71
+ _i = index - @options[:before_context].to_i
72
+ param = _i < 0 ? [0, index] : [_i, index]
73
+ when option_after?
74
+ _i = index + @options[:after_context].to_i
75
+ param = [index, _i]
76
+ when option_context?
77
+ _i = index - @options[:context].to_i
78
+ _j = index + @options[:context].to_i
79
+ param = _i < 0 ? [0, _j] : [_i, _j]
80
+ end
81
+
82
+ Range.new(*param)
83
+ end
84
+
85
+ def puts_contents(item)
86
+ print_room? ? nil : puts_room
87
+ puts item.contents unless printed?(item.id)
88
+ end
89
+ end
90
+ end
91
+ end