Created
July 19, 2011 17:02
-
-
Save kern/1093086 to your computer and use it in GitHub Desktop.
WebhookCallbacks for God
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module God | |
module Contacts | |
class WebhookCallbacks < Webhook | |
def notify(message, time, priority, category, host) | |
data = { | |
:message => message, | |
:time => time, | |
:priority => priority, | |
:category => category, | |
:host => host | |
} | |
uri = URI.parse(arg(:url)) | |
http = Net::HTTP.new(uri.host, uri.port) | |
req = nil | |
res = nil | |
case arg(:format) | |
when :form | |
req = Net::HTTP::Post.new(uri.path) | |
req.set_form_data(data) | |
when :json | |
req = Net::HTTP::Post.new(uri.path) | |
req.body = data.to_json | |
end | |
res = http.request(req) | |
case res | |
when Net::HTTPSuccess | |
self.info = "sent webhook to #{arg(:url)}" | |
success(res) | |
else | |
self.info = "failed to send webhook to #{arg(:url)}: #{res.error!}" | |
failure(res) | |
end | |
rescue Object => e | |
applog(nil, :info, "failed to send email to #{arg(:url)}: #{e.message}") | |
applog(nil, :debug, e.backtrace.join("\n")) | |
failure(res) | |
end | |
def success(res) | |
raise AbstractMethodNotOverriddenError.new("WebhookCallbacks#success must be overridden in subclasses") | |
end | |
def failure(res) | |
raise AbstractMethodNotOverriddenError.new("WebhookCallbacks#failure must be overridden in subclasses") | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment