Skip to content

Commit

Permalink
Merge pull request #162 from pzimmerman-github/hipchat
Browse files Browse the repository at this point in the history
Hipchat
  • Loading branch information
eric committed Mar 6, 2014
2 parents c4cdc40 + 6b1762c commit 6445038
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 1 deletion.
23 changes: 23 additions & 0 deletions doc/god.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,29 @@ ssl - A Boolean determining whether or not to use SSL
(default: false).
```

Hipchat
~~~~~~~~

Send a notice to a Hipchat room (http://hipchat.com).

```ruby
God::Contacts::Hipchat.defaults do |d|
...
end

God.contact(:hipchat) do |c|
...
end
```

```
token - The String token used for authentication.
room - The String room name to which the message should be sent.
ssl - A Boolean determining whether or not to use SSL
(default: false).
from - The String representing who the message should be sent as.
```

Email
~~~~~

Expand Down
4 changes: 3 additions & 1 deletion god.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Gem::Specification.new do |s|

s.name = 'god'
s.version = '0.13.3'
s.date = '2013-09-25'
s.date = '2014-03-04'

s.summary = "Process monitoring framework."
s.description = "An easy to configure, easy to extend monitoring framework written in Ruby."
Expand Down Expand Up @@ -83,6 +83,7 @@ Gem::Specification.new do |s|
lib/god/contacts/airbrake.rb
lib/god/contacts/campfire.rb
lib/god/contacts/email.rb
lib/god/contacts/hipchat.rb
lib/god/contacts/jabber.rb
lib/god/contacts/prowl.rb
lib/god/contacts/scout.rb
Expand Down Expand Up @@ -154,6 +155,7 @@ Gem::Specification.new do |s|
test/test_event_handler.rb
test/test_god.rb
test/test_handlers_kqueue_handler.rb
test/test_hipchat.rb
test/test_jabber.rb
test/test_logger.rb
test/test_metric.rb
Expand Down
1 change: 1 addition & 0 deletions lib/god.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def load_contact(name)

require 'god/contact'
load_contact(:campfire)
load_contact(:hipchat)
load_contact(:email)
load_contact(:jabber)
load_contact(:prowl)
Expand Down
117 changes: 117 additions & 0 deletions lib/god/contacts/hipchat.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# Send a notice to a Hipchat room (http://hipchat.com).
#
# token - The String token used for authentication.
# room - The String room name to which the message should be sent.
# ssl - A Boolean determining whether or not to use SSL
# (default: false).
# from - The String representing who the message should be sent as.

require 'net/http'
require 'net/https'

CONTACT_DEPS[:hipchat] = ['json']
CONTACT_DEPS[:hipchat].each do |d|
require d
end

module Marshmallow
class Connection
def initialize(options)
raise "Required option :token not set." unless options[:token]
@options = options
end

def base_url
scheme = @options[:ssl] ? 'https' : 'http'
"#{scheme}://api.hipchat.com/v1/rooms"
end

def find_room_id_by_name(room_name)
url = URI.parse("#{base_url}/list?format=json&auth_token=#{@options[:token]}")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true if @options[:ssl]

req = Net::HTTP::Get.new(url.request_uri)
req.set_content_type('application/json')

res = http.request(req)
case res
when Net::HTTPSuccess
rooms = JSON.parse(res.body)
room = rooms['rooms'].select { |x| x['name'] == room_name }
rooms.empty? ? nil : room.first['room_id'].to_i
else
raise res.error!
end
end

def speak(room, message)
room_id = find_room_id_by_name(room)
puts "in spark: room_id = #{room_id}"
raise "No such room: #{room}." unless room_id

escaped_message = URI.escape(message)

url = URI.parse("#{base_url}/message?message_format=text&format=json&auth_token=#{@options[:token]}&from=#{@options[:from]}&room_id=#{room}&message=#{escaped_message}")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true if @options[:ssl]

req = Net::HTTP::Post.new(url.request_uri)
req.set_content_type('application/json')
res = http.request(req)
case res
when Net::HTTPSuccess
true
else
raise res.error!
end
end
end
end

module God
module Contacts

class Hipchat < Contact
class << self
attr_accessor :token, :room, :ssl, :from
attr_accessor :format
end

self.ssl = false

self.format = lambda do |message, time, priority, category, host|
"[#{time.strftime('%H:%M:%S')}] #{host} - #{message}"
end

attr_accessor :token, :room, :ssl, :from

def valid?
valid = true
valid &= complain("Attribute 'token' must be specified", self) unless arg(:token)
valid &= complain("Attribute 'room' must be specified", self) unless arg(:room)
valid &= complain("Attribute 'from' must be specified", self) unless arg(:from)
valid
end

def notify(message, time, priority, category, host)
body = Hipchat.format.call(message, time, priority, category, host)

conn = Marshmallow::Connection.new(
:token => arg(:token),
:ssl => arg(:ssl),
:from => arg(:from)
)

conn.speak(arg(:room), body)

self.info = "notified hipchat: #{arg(:room)}"
rescue Object => e
applog(nil, :info, "failed to notify hipchat: #{e.message}")
applog(nil, :debug, e.backtrace.join("\n"))
end
end

end
end
10 changes: 10 additions & 0 deletions test/configs/contact/contact.god
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@
# c.name = 'tom4'
# end

# God::Contacts::Hipchat.defaults do |d|
# d.token = '9fb768e421975cc1c6ff3f4f8306f890cb46e24f'
# d.room = 'Notices'
# d.ssl = true
# end
#
# God.contact(:hipchat) do |c|
# c.name = 'hip1'
# end

# God.contact(:email) do |c|
# c.name = 'tom'
# c.group = 'developers'
Expand Down
23 changes: 23 additions & 0 deletions test/test_hipchat.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require File.dirname(__FILE__) + '/helper'

class TestHipchat < Test::Unit::TestCase
def setup
@hipchat = God::Contacts::Hipchat.new
end

def test_exists
God::Contacts::Hipchat
end

def test_notify
@hipchat.token = 'ee64d6e2337310af'
@hipchat.ssl = 'true'
@hipchat.room = 'testroom'
@hipchat.from = 'test'

time = Time.now
body = "[#{time.strftime('%H:%M:%S')}] host - msg"
Marshmallow::Connection.any_instance.expects(:speak).with('testroom', body)
@hipchat.notify('msg', time, 'prio', 'cat', 'host')
end
end

0 comments on commit 6445038

Please sign in to comment.