hatchet-hipchat 0.0.1
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.
- data/LICENSE +22 -0
- data/lib/hatchet-hipchat/version.rb +5 -0
- data/lib/hatchet-hipchat.rb +65 -0
- data/spec/helpers/fake_client.rb +19 -0
- data/spec/hipchat_appender_spec.rb +45 -0
- data/spec/spec_helper.rb +8 -0
- metadata +76 -0
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Garry Shutler
|
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.
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'hatchet-hipchat/version'
|
2
|
+
require 'hatchet'
|
3
|
+
require 'hipchat-api'
|
4
|
+
|
5
|
+
module Hatchet
|
6
|
+
|
7
|
+
# Public: Class for sending log messages to a HipChat room.
|
8
|
+
#
|
9
|
+
class HipChatAppender
|
10
|
+
include LevelManager
|
11
|
+
|
12
|
+
# Interal: The class to use to create HipChat clients.
|
13
|
+
#
|
14
|
+
CLIENT = HipChat::API
|
15
|
+
|
16
|
+
# Public: The formatter used to format messages before sending them to the
|
17
|
+
# HipChat room.
|
18
|
+
#
|
19
|
+
attr_accessor :formatter
|
20
|
+
|
21
|
+
# Public: The API token to use to connect to HipChat's API.
|
22
|
+
#
|
23
|
+
attr_accessor :api_token
|
24
|
+
|
25
|
+
# Public: The ID of the room to send messages to.
|
26
|
+
#
|
27
|
+
attr_accessor :room_id
|
28
|
+
|
29
|
+
# Public: Creates a new instance.
|
30
|
+
#
|
31
|
+
# By default the appender is created with a SimpleFormatter.
|
32
|
+
#
|
33
|
+
# block - Optional block that can be used to customize the appender. The
|
34
|
+
# appender itself is passed to the block.
|
35
|
+
#
|
36
|
+
def initialize
|
37
|
+
@formatter = SimpleFormatter.new
|
38
|
+
yield self if block_given?
|
39
|
+
end
|
40
|
+
|
41
|
+
# Internal: Sends a message to HipChat if the appender is configured to send
|
42
|
+
# messages at the given level for the given context.
|
43
|
+
#
|
44
|
+
# level - The level of the message.
|
45
|
+
# context - The context of the message.
|
46
|
+
# message - The message to add to the appender if it is configured to log
|
47
|
+
# messages at the given level for the given context.
|
48
|
+
#
|
49
|
+
# Returns nothing.
|
50
|
+
#
|
51
|
+
def add(level, context, message)
|
52
|
+
return unless enabled? level, context
|
53
|
+
message = @formatter.format(level, context, message)
|
54
|
+
client.rooms_message @room_id, 'Hatchet', message
|
55
|
+
end
|
56
|
+
|
57
|
+
# Internal: Returns the client used to send messages to HipChat.
|
58
|
+
#
|
59
|
+
def client
|
60
|
+
@client ||= CLIENT.new(@api_token)
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
|
3
|
+
class FakeClient
|
4
|
+
|
5
|
+
attr_reader :api_token
|
6
|
+
|
7
|
+
attr_reader :messages
|
8
|
+
|
9
|
+
def initialize(api_token)
|
10
|
+
@api_token = api_token
|
11
|
+
@messages = []
|
12
|
+
end
|
13
|
+
|
14
|
+
def rooms_message(room_id, from, message)
|
15
|
+
@messages << OpenStruct.new(room_id: room_id, from: from, message: message)
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe HipChatAppender do
|
4
|
+
before do
|
5
|
+
# Replace the client class.
|
6
|
+
HipChatAppender.send :remove_const, :CLIENT
|
7
|
+
HipChatAppender::CLIENT = FakeClient
|
8
|
+
end
|
9
|
+
|
10
|
+
describe 'default formatter' do
|
11
|
+
let(:subject) { HipChatAppender.new }
|
12
|
+
|
13
|
+
it 'uses the SimpleFormatter by default' do
|
14
|
+
assert_instance_of(SimpleFormatter, subject.formatter)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe 'sending messages' do
|
19
|
+
let(:subject) do
|
20
|
+
HipChatAppender.new do |appender|
|
21
|
+
appender.level :warn
|
22
|
+
appender.api_token = 'MY_TOKEN'
|
23
|
+
appender.room_id = 'ROOM_ID'
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
before do
|
28
|
+
subject.add :warn, 'Custom::Context', 'Hello, World'
|
29
|
+
end
|
30
|
+
|
31
|
+
let(:message) { subject.client.messages.last }
|
32
|
+
|
33
|
+
it 'configures the client with the API token' do
|
34
|
+
assert 'MY_TOKEN' == subject.client.api_token
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'sends a formatted message to the configured room' do
|
38
|
+
assert 'ROOM_ID' == message.room_id
|
39
|
+
formatted_message = subject.formatter.format(:warn, 'Custom::Context', 'Hello, World')
|
40
|
+
assert 'Hatchet' == message.from
|
41
|
+
assert formatted_message == message.message
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hatchet-hipchat
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Garry Shutler
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-07-22 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: hatchet
|
16
|
+
requirement: &70143422560160 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.0.8
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70143422560160
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: hipchat-api
|
27
|
+
requirement: &70143422559660 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 1.0.4
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70143422559660
|
36
|
+
description: Hatchet appender that sends messages to HipChat
|
37
|
+
email:
|
38
|
+
- garry@robustsoftware.co.uk
|
39
|
+
executables: []
|
40
|
+
extensions: []
|
41
|
+
extra_rdoc_files: []
|
42
|
+
files:
|
43
|
+
- lib/hatchet-hipchat/version.rb
|
44
|
+
- lib/hatchet-hipchat.rb
|
45
|
+
- spec/helpers/fake_client.rb
|
46
|
+
- spec/hipchat_appender_spec.rb
|
47
|
+
- spec/spec_helper.rb
|
48
|
+
- LICENSE
|
49
|
+
homepage: ''
|
50
|
+
licenses: []
|
51
|
+
post_install_message:
|
52
|
+
rdoc_options: []
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ! '>='
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ! '>='
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
requirements: []
|
68
|
+
rubyforge_project:
|
69
|
+
rubygems_version: 1.8.17
|
70
|
+
signing_key:
|
71
|
+
specification_version: 3
|
72
|
+
summary: Hatchet appender that sends messages to HipChat
|
73
|
+
test_files:
|
74
|
+
- spec/helpers/fake_client.rb
|
75
|
+
- spec/hipchat_appender_spec.rb
|
76
|
+
- spec/spec_helper.rb
|