Class: Exceptify::WebhookNotifier

Inherits:
BaseNotifier show all
Defined in:
lib/exceptify/webhook_notifier.rb

Instance Attribute Summary

Attributes inherited from BaseNotifier

#base_options

Instance Method Summary collapse

Methods inherited from BaseNotifier

#_post_callback, #_pre_callback, #send_notice

Constructor Details

#initialize(options) ⇒ WebhookNotifier

Returns a new instance of WebhookNotifier.



8
9
10
11
12
13
14
# File 'lib/exceptify/webhook_notifier.rb', line 8

def initialize(options)
  options = options.dup
  @http_client = options.delete(:http_client) || HTTParty
  super()
  self.base_options = options
  @default_options = options
end

Instance Method Details

#call(exception, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/exceptify/webhook_notifier.rb', line 16

def call(exception, options = {})
  notification = Notification.new(exception, options)
  env = notification.env

  options = options.reverse_merge(@default_options)
  url = options.delete(:url)
  raise ArgumentError, "You must provide 'url' option" if blank?(url)

  http_method = options.delete(:http_method) || :post

  options[:body] ||= {}
  options[:body][:server] = notification.hostname
  options[:body][:process] = Process.pid
  options[:body][:rails_root] = Rails.root if defined?(Rails) && Rails.respond_to?(:root)
  options[:body][:exception] = {
    error_class: exception.class.to_s,
    message: exception.message.inspect,
    backtrace: notification.backtrace
  }
  options[:body][:data] = notification.data

  unless env.nil?
    request = notification.request_context.request

    request_items = {
      url: request.original_url,
      http_method: request.method,
      ip_address: request.remote_ip,
      parameters: request.filtered_parameters,
      timestamp: notification.timestamp
    }

    options[:body][:request] = request_items
    options[:body][:session] = request.session
    options[:body][:environment] = request.filtered_env
  end
  send_notice(exception, options, nil, @default_options) do |_, _|
    @http_client.send(http_method, url, options)
  end
end