Module: HooksNotifier

Defined in:
lib/hooks_notifier.rb

Class Method Summary collapse

Class Method Details

.build_web_hook_params(config, exception, controller, request, data = {}, the_blamed = nil) ⇒ Object

Parameters hash based on Merb Exceptions example



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/hooks_notifier.rb', line 18

def self.build_web_hook_params(config, exception, controller, request, data={}, the_blamed = nil)
  host = (request.env["HTTP_X_FORWARDED_HOST"] || request.env["HTTP_HOST"])
  p = {
    'environment'              => (defined?(Rails) ? Rails.env : RAILS_ENV),
    'exceptions'               => [{
      :class      => exception.class.to_s,
      :backtrace  => exception.backtrace,
      :message    => exception.message
      }],
    'app_name'                 => config[:app_name],
    'version'                  => config[:version],
    'blame'                    => "#{the_blamed}"
  }
  if !request.nil?
    p.merge!({'request_url'         => "#{request.protocol}#{host}#{request.request_uri}"})
    p.merge!({'request_action'      => request.parameters['action']})
    p.merge!({'request_params'      => request.parameters.inspect})
  end
  p.merge!({'request_controller'    => controller.class.name}) if !controller.nil?
  p.merge!({'status'                => exception.status}) if exception.respond_to?(:status)
  return p
end

.deliver_exception_to_web_hooks(config, exception, controller, request, data = {}, the_blamed = nil) ⇒ Object

Deliver exception data hash to web hooks, if any



7
8
9
10
11
12
13
# File 'lib/hooks_notifier.rb', line 7

def self.deliver_exception_to_web_hooks(config, exception, controller, request, data={}, the_blamed = nil)
  params = build_web_hook_params(config, exception, controller, request, data, the_blamed)
  # TODO: use threads here
  config[:web_hooks].each do |address|
    post_hook(params, address)
  end
end

.post_hook(params, address) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/hooks_notifier.rb', line 41

def self.post_hook(params, address)
  uri = URI.parse(address)
  uri.path = '/' if uri.path=='' # set a path if one isn't provided to keep Net::HTTP happy
  
  headers = { 'Content-Type' => 'text/x-json' }
  data = params.to_json
  Net::HTTP.start(uri.host, uri.port) do |http|
    http.request_post(uri.path, data, headers)
  end
  data
end