hipchat_searcher 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2006db1fe811be9753fc96fb820a59263d92168d
4
+ data.tar.gz: 262e28b8397dead9223a35b50bb3b9df4edf872e
5
+ SHA512:
6
+ metadata.gz: 5df17c395a593f1397b68b89b7265b29c16bcc37735dff21241f543de4ba3cca1f1e3ce191ca89610bca0d97de23a9413cc619361ba10dbf9c5e3aed523ad982
7
+ data.tar.gz: 3eab7cf6cecdbe9c0ebabcb793b522eae83328a6912ee50fc49eb63c397205cb02dc3a7ea9c7d17624cf6c85d164b8464f7b2cda1896ea2eec777f1eb10e7f24
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - 2.1.2
6
+ branches:
7
+ only:
8
+ - master
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in hipchat_searcher.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 mgi166
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # HipchatSearcher
2
+
3
+ [![Build Status](https://travis-ci.org/mgi166/hipchat_searcher.svg?branch=master)](https://travis-ci.org/mgi166/hipchat_searcher)
4
+ [![Coverage Status](https://coveralls.io/repos/mgi166/hipchat_searcher/badge.png?branch=master)](https://coveralls.io/r/mgi166/hipchat_searcher?branch=master)
5
+ [![Code Climate](https://codeclimate.com/github/mgi166/hipchat_searcher.png)](https://codeclimate.com/github/mgi166/hipchat_searcher)
6
+
7
+ This is the command line tool for [hipchat](https://www.hipchat.com/).
8
+ You will be able to search hipchat log more easily.
9
+
10
+ ## Installation
11
+
12
+ $ gem install hipchat_searcher
13
+
14
+ ## Prepare
15
+
16
+ * `hitchat_searcher` requires access token on [hipchat](https://www.hipchat.com/). so you visit [hipchat](https://www.hipchat.com/). login and get access token.
17
+ * if you get access token, execute command like this.
18
+
19
+ ```
20
+ echo {access_token} > ~/.hps
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ * Search word in all room
26
+
27
+ ```
28
+ hps word
29
+ ```
30
+
31
+ * Search word in specified room
32
+
33
+ ```
34
+ hps word -r room-name
35
+ ```
36
+
37
+ * Search word since target date
38
+
39
+ ```
40
+ hps word -d 2014-01-01
41
+ ```
42
+
43
+ * Search word in archived room
44
+
45
+ ```
46
+ hps word -a
47
+ ```
48
+
49
+ ## Contributing
50
+
51
+ 1. Fork it ( https://github.com/mgi166/hipchat_searcher/fork )
52
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
53
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
54
+ 4. Push to the branch (`git push origin my-new-feature`)
55
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
data/bin/hps ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ $LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
3
+ require 'hipchat_searcher'
4
+ require 'optparse'
5
+
6
+ pattern = ARGV.shift
7
+ hash = ARGV.getopts(HipchatSearcher::Options.short_names, *HipchatSearcher::Options.long_names)
8
+ options = HipchatSearcher::Options.new(hash)
9
+ command = HipchatSearcher::Command.new(pattern, options)
10
+ command.run
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'hipchat_searcher/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "hipchat_searcher"
8
+ spec.version = HipchatSearcher::VERSION
9
+ spec.authors = ["mgi166"]
10
+ spec.email = ["skskoari@gmail.com"]
11
+ spec.summary = %q{Search hipchat log on terminal}
12
+ spec.homepage = ""
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "hipchat"
21
+ spec.add_dependency "colorize"
22
+ spec.add_dependency "hashie"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.6"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "rspec"
27
+ spec.add_development_dependency "coveralls"
28
+ end
@@ -0,0 +1,8 @@
1
+ require 'hipchat_searcher/command'
2
+ require 'hipchat_searcher/config'
3
+ require 'hipchat_searcher/message'
4
+ require 'hipchat_searcher/options'
5
+ require 'hipchat_searcher/result'
6
+ require 'hipchat_searcher/room'
7
+ require 'hipchat_searcher/searcher'
8
+ require 'hipchat_searcher/version'
@@ -0,0 +1,25 @@
1
+ module HipchatSearcher
2
+ class Command
3
+ def initialize(pattern, options)
4
+ @pattern = pattern
5
+ @options = options
6
+ @config = Config.new
7
+ end
8
+
9
+ def run
10
+ rooms = if @options.room?
11
+ [@options.room]
12
+ else
13
+ room = Room.new(@config.token, @options.room_options)
14
+ room.room
15
+ end
16
+
17
+ message = Message.new(@config.token, @options.message_options)
18
+
19
+ rooms.inject([]) do |result, room|
20
+ hist = message.history(room)
21
+ Searcher.search(@pattern, hist, @options.search_options)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,31 @@
1
+ module HipchatSearcher
2
+ class Config
3
+ attr_reader :token
4
+ def initialize
5
+ print_not_exist! unless File.exist?(config_path)
6
+ @token = File.read(config_path).chomp
7
+ end
8
+
9
+ def valid?
10
+ !!@token
11
+ end
12
+
13
+ def print_not_exist!
14
+ puts <<-EOS
15
+ Config file is not exsited.
16
+
17
+ * To create config file, run this command
18
+ `echo {auth_token} > ~/.hps`
19
+
20
+ * To get new auth_token, visit and sign in.
21
+ https://www.hipchat.com/
22
+ EOS
23
+
24
+ exit 1
25
+ end
26
+
27
+ def config_path
28
+ File.expand_path(File.join('~', '.hps'))
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,23 @@
1
+ require 'hipchat'
2
+
3
+ module HipchatSearcher
4
+ class Message
5
+ def initialize(token, options={})
6
+ @token = token
7
+ @options = options
8
+ @client = ::HipChat::Client.new(@token, api_version: 'v2')
9
+ end
10
+
11
+ def get_history(id)
12
+ @client[id].history(@options)
13
+ end
14
+
15
+ def history(room)
16
+ id = room.id rescue room
17
+ room_name = room.name rescue room
18
+
19
+ h = get_history(id)
20
+ Result.new(h).tap {|r| r.room = room_name }
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,59 @@
1
+ require 'hashie/mash'
2
+
3
+ module HipchatSearcher
4
+ class Options < Hashie::Mash
5
+ def archived
6
+ self['a'] || self['archived']
7
+ end
8
+
9
+ def archived?
10
+ !!self['a'] || !!self['archived']
11
+ end
12
+
13
+ def date
14
+ self['d'] || self['date']
15
+ end
16
+
17
+ def date?
18
+ !!self['d'] || !!self['date']
19
+ end
20
+
21
+ def message_options
22
+ date? ? { date: date } : {}
23
+ end
24
+
25
+ def room_options
26
+ archived? ? { 'include-archived' => true } : {}
27
+ end
28
+
29
+ def room
30
+ self['r'] || self['room']
31
+ end
32
+
33
+ def room?
34
+ !!self['r'] || !!self['room']
35
+ end
36
+
37
+ def user
38
+ self['u'] || self['user']
39
+ end
40
+
41
+ def user?
42
+ !!self['u'] || !!self['user']
43
+ end
44
+
45
+ def search_options
46
+ user? ? { user: user } : {}
47
+ end
48
+
49
+ class << self
50
+ def short_names
51
+ 'a:d:h:r:u'
52
+ end
53
+
54
+ def long_names
55
+ ['room' 'user', 'date', 'archived', 'help']
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,37 @@
1
+ require 'json'
2
+
3
+ module HipchatSearcher
4
+ class Result
5
+ class InvalidResponse < StandardError; end
6
+
7
+ attr_accessor :room
8
+
9
+ def initialize(response)
10
+ @response = response
11
+ valid!
12
+ end
13
+
14
+ def rooms
15
+ @response['items'].map {|i| ::Hashie::Mash.new(i) }
16
+ end
17
+
18
+ def items
19
+ @messages = JSON.parse(@response)['items'].map {|i| ::Hashie::Mash.new(i) }
20
+ end
21
+
22
+ def valid!
23
+ case @response
24
+ when String
25
+ if @response.empty?
26
+ raise InvalidResponse, "`#{@response}' is invalid response as hipchat. expect String size is over than 0"
27
+ end
28
+ when Hash
29
+ if !@response.has_key?('items')
30
+ raise InvalidResponse, "`#{@response}' is invalid response as hipchat. expect Hash key has 'items'"
31
+ end
32
+ else
33
+ raise InvalidResponse, "`#{@response}' is invalid response as hipchat. expect String or Hash"
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,40 @@
1
+ require 'hipchat'
2
+ require 'httparty'
3
+
4
+ module HipchatSearcher
5
+ class Room
6
+ include HTTParty
7
+
8
+ def initialize(token, options={})
9
+ @token = token
10
+ default_options = { api_version: 'v2', server_url: 'https://api.hipchat.com' }
11
+ @option = default_options.merge(options)
12
+ @api = ::HipChat::ApiVersion::Room.new(@option)
13
+ self.class.base_uri(@api.base_uri)
14
+ end
15
+
16
+ def get_all_room
17
+ response = self.class.get(
18
+ '',
19
+ query: {auth_token: @token},
20
+ header: @api.headers
21
+ )
22
+
23
+ case response.code
24
+ when 200
25
+ response.parsed_response
26
+ when 404
27
+ raise ::HipChat::UnknownRoom, 'Unknown room'
28
+ when 401
29
+ raise ::HipChat::Unauthorized, 'Access denied to room'
30
+ else
31
+ raise ::HipChat::UnknownResponseCode, "Unexpected #{response.code} for room"
32
+ end
33
+ end
34
+
35
+ def room
36
+ result = Result.new(get_all_room)
37
+ result.rooms
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,48 @@
1
+ require 'colorize'
2
+
3
+ module HipchatSearcher
4
+ class Searcher
5
+ def initialize(result, options={})
6
+ @result = result
7
+ @options = options
8
+ @print_room = false
9
+ end
10
+
11
+ def self.search(pattern, result, options)
12
+ new(result, options).search(pattern)
13
+ end
14
+
15
+ def search(pattern)
16
+ pattern = Regexp.new(pattern)
17
+
18
+ @result.items.each do |item|
19
+ if pattern =~ item.message
20
+ @print_room ? nil : puts_room
21
+ puts_search_result(pattern, item)
22
+ end
23
+ end
24
+
25
+ nil
26
+ end
27
+
28
+ def room
29
+ @result.room
30
+ end
31
+
32
+ def puts_room
33
+ @print_room = true
34
+ puts room
35
+ end
36
+
37
+ def puts_search_result(pattern, item)
38
+ msg = item.message.gsub(pattern) do |matched|
39
+ matched.underline
40
+ end
41
+
42
+ date = " Date: #{item.date}"
43
+ name = item.from.mention_name rescue item.from
44
+ msg = " @#{name}" + ': ' + msg
45
+ puts "%s\n%s\n\n" % [date, msg]
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,3 @@
1
+ module HipchatSearcher
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,58 @@
1
+ {
2
+ "items": [
3
+ {
4
+ "color": null,
5
+ "date": "2014-06-17T08:14:48.305590+00:00",
6
+ "from": "GitHub",
7
+ "id": "ae1140b3-be80-4bbd-9e97-a61c77185d02",
8
+ "mentions": [],
9
+ "message": "mgi166 commented on pull request 118 ...",
10
+ "message_format": "html"
11
+ },
12
+ {
13
+ "color": "green",
14
+ "date": "2014-06-17T08:15:12.787725+00:00",
15
+ "from": "Jenkins",
16
+ "id": "0955d2a9-98c8-4ecf-ade6-140d201dad11",
17
+ "mentions": [],
18
+ "message": "hipchat-searcher - #12 Success after 7 \u5206 17 \u79d2 (<a href=\"http://192.168.1.1:8080/job/hipchat-searcher-feature/12/\">Open</a>)",
19
+ "message_format": "html"
20
+ },
21
+ {
22
+ "date": "2014-06-17T08:33:52.311338+00:00",
23
+ "from": {
24
+ "id": 12345,
25
+ "links": {"self": "https://api.hipchat.com/v2/user/710659"},
26
+ "mention_name": "light",
27
+ "name": "light yagami"
28
+ },
29
+ "id": "2f1c99c2-c49d-4be1-b8d0-9c6c50557385",
30
+ "mentions": [
31
+ {
32
+ "id": 23456,
33
+ "links": {"self": "https://api.hipchat.com/v2/user/710653"},
34
+ "mention_name": "ray",
35
+ "name": "ray penber"
36
+ }
37
+ ],
38
+ "message": "good bye ray penber @ray "
39
+ },
40
+ {
41
+ "date": "2014-06-17T08:35:12.094892+00:00",
42
+ "from": {
43
+ "id": 23456,
44
+ "links": {"self": "https://api.hipchat.com/v2/user/710653"},
45
+ "mention_name": "ray",
46
+ "name": "ray penber"
47
+ },
48
+ "id": "abe58437-977c-49fb-bd9b-2d8016356983",
49
+ "mentions": [],
50
+ "message": "(beer)"
51
+ }
52
+ ],
53
+ "links": {
54
+ "self": "https://api.hipchat.com/v2/room/death-note/history"
55
+ },
56
+ "maxResults": 100,
57
+ "startIndex": 0
58
+ }
@@ -0,0 +1,43 @@
1
+ {
2
+ "items": [
3
+ {
4
+ "date": "2014-05-30T01:38:16.741565+00:00",
5
+ "from": {
6
+ "id": 12345,
7
+ "links": {"self": "https://api.hipchat.com/v2/user/12345"},
8
+ "mention_name": "jotaro",
9
+ "name": "Jotaro Kujo"
10
+ },
11
+ "id": "aaaa-bbbb-cccc",
12
+ "mentions": [],
13
+ "message": "yareyare daze"
14
+ },
15
+ {
16
+ "date": "2014-05-30T01:39:02.186319+00:00",
17
+ "from": {
18
+ "id": 23456,
19
+ "links": {"self": "https://api.hipchat.com/v2/user/23456"},
20
+ "mention_name": "noriaki",
21
+ "name": "Noriaki Kakyoin"
22
+ },
23
+ "id": "dddd-eeee-ffff",
24
+ "mentions": [],
25
+ "message": "rerorero"
26
+ },
27
+ {
28
+ "date": "2014-06-09T11:29:10.209014+00:00",
29
+ "from": {
30
+ "id": 34567,
31
+ "links": {"self": "https://api.hipchat.com/v2/user/34567"},
32
+ "mention_name": "polnareff",
33
+ "name": "Jean Pierre Polnareff"
34
+ },
35
+ "id": "gggg-hhhh-iiii",
36
+ "mentions": [],
37
+ "message": "a... arinomama ima okotta koto wo hanasu ze"
38
+ }
39
+ ],
40
+ "links": {"self": "https://api.hipchat.com/v2/room/jojo-part3/history"},
41
+ "maxResults": 100,
42
+ "startIndex": 0
43
+ }
@@ -0,0 +1,23 @@
1
+ {
2
+ "items"=>
3
+ [
4
+ {
5
+ "id"=>12345,
6
+ "links"=>{"self"=>"https://api.hipchat.com/v2/room/12345", "webhooks"=>"https://api.hipchat.com/v2/room/12345/webhook"},
7
+ "name"=>"sample-1"
8
+ },
9
+ {
10
+ "id"=>23456,
11
+ "links"=>{"self"=>"https://api.hipchat.com/v2/room/23456", "webhooks"=>"https://api.hipchat.com/v2/room/23456/webhook"},
12
+ "name"=>"sample-2"
13
+ },
14
+ {
15
+ "id"=>34567,
16
+ "links"=>{"self"=>"https://api.hipchat.com/v2/room/34567", "webhooks"=>"https://api.hipchat.com/v2/room/34567/webhook"},
17
+ "name"=>"sample-3"
18
+ }
19
+ ],
20
+ "links"=>{"self"=>"https://api.hipchat.com/v2/room"},
21
+ "maxResults"=>100,
22
+ "startIndex"=>0
23
+ }
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe HipchatSearcher do
4
+ it 'has a version number' do
5
+ expect(HipchatSearcher::VERSION).not_to be '0.0.1'
6
+ end
7
+ end
@@ -0,0 +1,112 @@
1
+ require 'spec_helper'
2
+
3
+ describe HipchatSearcher::Options do
4
+ describe '#message_options' do
5
+ context 'when self has "archived" key' do
6
+ subject { described_class.new(hash).message_options }
7
+ let(:hash) { {'d' => '2014-06-01' } }
8
+
9
+ it 'should return hash include key "include-archived"' do
10
+ should == { :date => '2014-06-01' }
11
+ end
12
+ end
13
+
14
+ context "when self don't have 'archived' key" do
15
+ subject { described_class.new(hash).message_options }
16
+ let(:hash) { {'room' => 'room-name' } }
17
+
18
+ it 'should return empty hash' do
19
+ should be_empty
20
+ end
21
+ end
22
+ end
23
+
24
+ describe '#room_options' do
25
+ context 'when self has "archived" key' do
26
+ subject { described_class.new(hash).room_options }
27
+ let(:hash) { {'archived' => true } }
28
+
29
+ it 'should return hash include key "include-archived"' do
30
+ should == {"include-archived" => true}
31
+ end
32
+ end
33
+
34
+ context "when self don't have 'archived' key" do
35
+ subject { described_class.new(hash).room_options }
36
+ let(:hash) { {'d' => '2014-06-01' } }
37
+
38
+ it 'should return empty hash' do
39
+ should be_empty
40
+ end
41
+ end
42
+ end
43
+
44
+ describe '#room?' do
45
+ context 'given hash has a "room" key as argument' do
46
+ subject { described_class.new(hash).room? }
47
+ let(:hash) { {'room' => 'room-name' } }
48
+
49
+ it { should be_truthy }
50
+ end
51
+
52
+ context 'given hash has a "r" key as argument' do
53
+ subject { described_class.new(hash).room? }
54
+ let(:hash) { {'r' => 'room-name' } }
55
+
56
+ it { should be_truthy }
57
+ end
58
+
59
+ context 'given hash have any other keys as argument' do
60
+ subject { described_class.new(hash).room? }
61
+ let(:hash) { {'other' => 'xxxx' } }
62
+
63
+ it { should be_falsey }
64
+ end
65
+ end
66
+
67
+ describe '#user?' do
68
+ context 'given hash has a "user" key as argument' do
69
+ subject { described_class.new(hash).user? }
70
+ let(:hash) { {'user' => 'jotaro kujo' } }
71
+
72
+ it { should be_truthy }
73
+ end
74
+
75
+ context 'given hash has a "u" key as argument' do
76
+ subject { described_class.new(hash).user? }
77
+ let(:hash) { {'u' => 'jojo' } }
78
+
79
+ it { should be_truthy }
80
+ end
81
+
82
+ context 'given hash have any other keys as argument' do
83
+ subject { described_class.new(hash).user? }
84
+ let(:hash) { {'other' => 'xxxx' } }
85
+
86
+ it { should be_falsey }
87
+ end
88
+ end
89
+
90
+ describe '#date?' do
91
+ context 'given hash has a "date" key as argument' do
92
+ subject { described_class.new(hash).date? }
93
+ let(:hash) { {'date' => '2014-06-1' } }
94
+
95
+ it { should be_truthy }
96
+ end
97
+
98
+ context 'given hash has a "u" key as argument' do
99
+ subject { described_class.new(hash).date? }
100
+ let(:hash) { {'d' => '2014-06-1' } }
101
+
102
+ it { should be_truthy }
103
+ end
104
+
105
+ context 'given hash have any other keys as argument' do
106
+ subject { described_class.new(hash).date? }
107
+ let(:hash) { {'other' => 'xxxx' } }
108
+
109
+ it { should be_falsey }
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,147 @@
1
+ require 'spec_helper'
2
+
3
+ describe HipchatSearcher::Result do
4
+ def result(response)
5
+ described_class.new(response)
6
+ end
7
+
8
+ describe '#new' do
9
+ describe 'given valid response(a json like string)' do
10
+ subject { result(response) }
11
+
12
+ let(:response) { '{"google":"value1", "twitter":"value2"}' }
13
+
14
+ it { should be_instance_of HipchatSearcher::Result }
15
+ end
16
+
17
+ describe 'given valid response(hash has key "items")' do
18
+ subject { result(response) }
19
+
20
+ let(:response) do
21
+ {'items' => ['a', 'b', 'c'], 'links'=>'http://sample.co.jp', 'maxResult'=>100, 'startIndex'=>0}
22
+ end
23
+
24
+ it { should be_instance_of HipchatSearcher::Result }
25
+ end
26
+
27
+ describe 'given empty string as response' do
28
+ subject { result(response) }
29
+
30
+ let(:response) { '' }
31
+
32
+ it 'should raise error' do
33
+ expect do
34
+ subject
35
+ end.to raise_error(HipchatSearcher::Result::InvalidResponse)
36
+ end
37
+ end
38
+
39
+ describe 'given a hash without "items" key as response' do
40
+ subject { result(response) }
41
+
42
+ let(:response) { {} }
43
+
44
+ it 'should raise error' do
45
+ expect do
46
+ subject
47
+ end.to raise_error(HipchatSearcher::Result::InvalidResponse)
48
+ end
49
+ end
50
+
51
+ describe 'given other object as response' do
52
+ subject { result(response) }
53
+
54
+ let(:response) { nil }
55
+
56
+ it 'should raise error' do
57
+ expect do
58
+ subject
59
+ end.to raise_error(HipchatSearcher::Result::InvalidResponse)
60
+ end
61
+ end
62
+ end
63
+
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
+ describe '#items' do
88
+ context 'the value' do
89
+ subject { result(response).items }
90
+
91
+ let(:response) { File.read(path) }
92
+ let(:path) { File.join('spec', 'data', 'item-list.json') }
93
+
94
+ it { should be_instance_of Array }
95
+ end
96
+
97
+ describe 'the item elements' do
98
+ let(:item_element) { result(response).items.first }
99
+ let(:response) { File.read(path) }
100
+ let(:path) { File.join('spec', 'data', 'item-list.json') }
101
+
102
+ context 'class' do
103
+ subject { item_element }
104
+
105
+ it { should be_instance_of Hashie::Mash }
106
+ end
107
+
108
+ context '#date' do
109
+ subject { item_element.date }
110
+
111
+ it 'should return the value of "date"' do
112
+ should == '2014-05-30T01:38:16.741565+00:00'
113
+ end
114
+ end
115
+
116
+ context '#from' do
117
+ subject { item_element.from }
118
+
119
+ it { should be_instance_of Hashie::Mash }
120
+ end
121
+
122
+ context '#id' do
123
+ subject { item_element.id }
124
+
125
+ it 'should return the value of "id"' do
126
+ should == 'aaaa-bbbb-cccc'
127
+ end
128
+ end
129
+
130
+ context '#mentions' do
131
+ subject { item_element.mentions }
132
+
133
+ it 'should return the value of "mention" ' do
134
+ should == []
135
+ end
136
+ end
137
+
138
+ context '#item' do
139
+ subject { item_element.message }
140
+
141
+ it 'should return the value of "message"' do
142
+ should == 'yareyare daze'
143
+ end
144
+ end
145
+ end
146
+ end
147
+ end
@@ -0,0 +1,136 @@
1
+ require 'spec_helper'
2
+
3
+ describe HipchatSearcher::Searcher do
4
+ def searcher(result)
5
+ described_class.new(result)
6
+ end
7
+
8
+ describe '#search' do
9
+ context 'when matches pattern in messages' do
10
+ subject { searcher(result).search(pattern) }
11
+
12
+ let(:pattern) { 'yare' }
13
+ let(:response) { File.read(File.join('spec', 'data', 'item-list.json')) }
14
+ let(:result) do
15
+ r = HipchatSearcher::Result.new(response)
16
+ r.room = "Joestars"
17
+ r
18
+ end
19
+ let(:search_result) do
20
+ "Joestars" + "\n" + \
21
+ " Date: 2014-05-30T01:38:16.741565+00:00" + "\n" + \
22
+ " @jotaro: \e[4;39;49myare\e[0m\e[4;39;49myare\e[0m daze" + "\n" + \
23
+ "\n"
24
+ end
25
+
26
+ it 'should print roomname & search result' do
27
+ expect do
28
+ subject
29
+ end.to output(search_result).to_stdout
30
+ end
31
+ end
32
+
33
+ context 'when matches pattern in many messages' do
34
+ subject { searcher(result).search(pattern) }
35
+
36
+ let(:pattern) { 'ze' }
37
+ let(:response) { File.read(File.join('spec', 'data', 'item-list.json')) }
38
+ let(:result) do
39
+ r = HipchatSearcher::Result.new(response)
40
+ r.room = "Joestars"
41
+ r
42
+ end
43
+ let(:search_result) do
44
+ "Joestars" + "\n" + \
45
+ " Date: 2014-05-30T01:38:16.741565+00:00" + "\n" + \
46
+ " @jotaro: yareyare da\e[4;39;49mze\e[0m" + "\n" \
47
+ "\n" + \
48
+ " Date: 2014-06-09T11:29:10.209014+00:00" + "\n" + \
49
+ " @polnareff: a... arinomama ima okotta koto wo hanasu \e[4;39;49mze\e[0m" + "\n" + \
50
+ "\n"
51
+ end
52
+
53
+ it 'should print roomname & search result' do
54
+ expect do
55
+ subject
56
+ end.to output(search_result).to_stdout
57
+ end
58
+ end
59
+
60
+ context "when don't match pattern in messages" do
61
+ subject { searcher(result).search(pattern) }
62
+
63
+ let(:pattern) { 'abcd' }
64
+ let(:result) { HipchatSearcher::Result.new(response) }
65
+ let(:response) { File.read(File.join('spec', 'data', 'item-list.json')) }
66
+
67
+ it { should be_nil }
68
+
69
+ it 'should no output to stdout' do
70
+ expect do
71
+ subject
72
+ end.to output('').to_stdout
73
+ end
74
+ end
75
+
76
+ context "when pattern can't convert regexp" do
77
+ subject { searcher(result).search(pattern) }
78
+
79
+ let(:pattern) { nil }
80
+ let(:result) { HipchatSearcher::Result.new(response) }
81
+ let(:response) { File.read(File.join('spec', 'data', 'item-list.json')) }
82
+
83
+ it 'should raise exception' do
84
+ expect do
85
+ subject
86
+ end.to raise_error
87
+ end
88
+ end
89
+ end
90
+
91
+ describe '#puts_search_result' do
92
+ context 'when only person in room' do
93
+ subject { searcher(double(:result)).puts_search_result(pattern, item) }
94
+
95
+ let(:pattern) { Regexp.new('yare') }
96
+ let(:item) do
97
+ src = File.read(File.join('spec', 'data', 'item-list.json'))
98
+ hash = JSON.parse(src)
99
+ ::Hashie::Mash.new(hash).items.first
100
+ end
101
+
102
+ let(:search_result) do
103
+ " Date: 2014-05-30T01:38:16.741565+00:00" + "\n" + \
104
+ " @jotaro: \e[4;39;49myare\e[0m\e[4;39;49myare\e[0m daze" + "\n\n"
105
+ end
106
+
107
+ it 'should print string of search result' do
108
+ expect do
109
+ subject
110
+ end.to output(search_result).to_stdout
111
+ end
112
+ end
113
+
114
+ context 'when person and bot in room' do
115
+ subject { searcher(double(:result)).puts_search_result(pattern, item) }
116
+
117
+ let(:pattern) { Regexp.new('mgi166') }
118
+ let(:item) do
119
+ src = File.read(File.join('spec', 'data', 'item-list-with-bot.json'))
120
+ hash = JSON.parse(src)
121
+ ::Hashie::Mash.new(hash).items.first
122
+ end
123
+
124
+ let(:search_result) do
125
+ ' Date: 2014-06-17T08:14:48.305590+00:00' + "\n" + \
126
+ " @GitHub: \e[4;39;49mmgi166\e[0m commented on pull request 118 ..." + "\n\n"
127
+ end
128
+
129
+ it 'should print string of search result' do
130
+ expect do
131
+ subject
132
+ end.to output(search_result).to_stdout
133
+ end
134
+ end
135
+ end
136
+ end
@@ -0,0 +1,10 @@
1
+ require 'coveralls'
2
+ Coveralls.wear!
3
+
4
+ require 'hipchat_searcher'
5
+
6
+ RSpec.configure do |config|
7
+ config.order = :random
8
+ config.filter_run :focus
9
+ config.run_all_when_everything_filtered = true
10
+ end
metadata ADDED
@@ -0,0 +1,176 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hipchat_searcher
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - mgi166
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: hipchat
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: colorize
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: hashie
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.6'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.6'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: coveralls
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description:
112
+ email:
113
+ - skskoari@gmail.com
114
+ executables:
115
+ - hps
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - ".gitignore"
120
+ - ".travis.yml"
121
+ - Gemfile
122
+ - LICENSE.txt
123
+ - README.md
124
+ - Rakefile
125
+ - bin/hps
126
+ - hipchat_searcher.gemspec
127
+ - lib/hipchat_searcher.rb
128
+ - lib/hipchat_searcher/command.rb
129
+ - lib/hipchat_searcher/config.rb
130
+ - lib/hipchat_searcher/message.rb
131
+ - lib/hipchat_searcher/options.rb
132
+ - lib/hipchat_searcher/result.rb
133
+ - lib/hipchat_searcher/room.rb
134
+ - lib/hipchat_searcher/searcher.rb
135
+ - lib/hipchat_searcher/version.rb
136
+ - spec/data/item-list-with-bot.json
137
+ - spec/data/item-list.json
138
+ - spec/data/room-list.txt
139
+ - spec/hipchat_searcher_spec.rb
140
+ - spec/lib/hipchat_searcher/options_spec.rb
141
+ - spec/lib/hipchat_searcher/result_spec.rb
142
+ - spec/lib/hipchat_searcher/searcher_spec.rb
143
+ - spec/spec_helper.rb
144
+ homepage: ''
145
+ licenses:
146
+ - MIT
147
+ metadata: {}
148
+ post_install_message:
149
+ rdoc_options: []
150
+ require_paths:
151
+ - lib
152
+ required_ruby_version: !ruby/object:Gem::Requirement
153
+ requirements:
154
+ - - ">="
155
+ - !ruby/object:Gem::Version
156
+ version: '0'
157
+ required_rubygems_version: !ruby/object:Gem::Requirement
158
+ requirements:
159
+ - - ">="
160
+ - !ruby/object:Gem::Version
161
+ version: '0'
162
+ requirements: []
163
+ rubyforge_project:
164
+ rubygems_version: 2.2.2
165
+ signing_key:
166
+ specification_version: 4
167
+ summary: Search hipchat log on terminal
168
+ test_files:
169
+ - spec/data/item-list-with-bot.json
170
+ - spec/data/item-list.json
171
+ - spec/data/room-list.txt
172
+ - spec/hipchat_searcher_spec.rb
173
+ - spec/lib/hipchat_searcher/options_spec.rb
174
+ - spec/lib/hipchat_searcher/result_spec.rb
175
+ - spec/lib/hipchat_searcher/searcher_spec.rb
176
+ - spec/spec_helper.rb