Class: ExceptionNotifier::SlackNotifier

Inherits:
Object
  • Object
show all
Defined in:
lib/support/exception_slack_notify/exception_notification.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ SlackNotifier

Returns a new instance of SlackNotifier.



12
13
14
15
16
17
18
19
20
# File 'lib/support/exception_slack_notify/exception_notification.rb', line 12

def initialize(options)
  begin
    webhook_url = options.fetch(:webhook_url)
    @message_opts = options.fetch(:additional_parameters, {})
    @notifier = Slack::Notifier.new webhook_url, options
  rescue
    @notifier = nil
  end
end

Instance Attribute Details

#notifierObject

Returns the value of attribute notifier.



10
11
12
# File 'lib/support/exception_slack_notify/exception_notification.rb', line 10

def notifier
  @notifier
end

Instance Method Details

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



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/support/exception_slack_notify/exception_notification.rb', line 22

def call(exception, options={})
  message = [
      "时间: #{ Time.now.to_s }",
      "URL: #{ options[:env]["REQUEST_URI"] }",
      "参数: #{ options[:env]["action_dispatch.request.parameters"] }",
      "用户IP: #{ options[:env]["action_dispatch.remote_ip"] }",
      "异常: #{ [exception.message, exception.backtrace].flatten.join("\n") }",
      "==============================================="
  ].join("\n\n")

  @notifier.ping(message, @message_opts) if valid?
end