Class: ExceptionNotifier::LarkNotifier

Inherits:
Object
  • Object
show all
Includes:
BacktraceCleaner
Defined in:
lib/exception_notifier/lark_notifier.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ LarkNotifier

Returns a new instance of LarkNotifier.



21
22
23
24
25
26
27
28
29
# File 'lib/exception_notifier/lark_notifier.rb', line 21

def initialize(options)
  @default_app_name = self.class.rails_app_name
  @default_env_name = self.class.rails_env_name
  @default_options = options

  @notifier = ExceptionNotificationLarkNotifier::Client.new(
    options.slice(:webhook_url, :webhook_secret, :http)
  )
end

Class Method Details

.rails_app_nameObject



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

def rails_app_name
  return unless defined?(::Rails) && ::Rails.respond_to?(:application)
  if ::Gem::Version.new(::Rails.version) >= ::Gem::Version.new("6.0")
    ::Rails.application.class.module_parent_name
  else
    ::Rails.application.class.parent_name
  end
end

.rails_env_nameObject



15
16
17
18
# File 'lib/exception_notifier/lark_notifier.rb', line 15

def rails_env_name
  return unless defined?(::Rails) && ::Rails.respond_to?(:env)
  ::Rails.env
end

Instance Method Details

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



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/exception_notifier/lark_notifier.rb', line 31

def call(exception, call_options = {})
  options = call_options.merge(@default_options)
  app_name = options.delete(:app_name) || @default_app_name
  env_name = options.delete(:env_name) || @default_env_name

  title = "Exception Occurred"
  title += " in #{app_name}" if app_name
  title += " (env: #{env_name})" if env_name

  backtrace = exception.backtrace ? clean_backtrace(exception) : []
  summary, data = information_from_options(exception.class, options)
  fields = fields(exception.message, backtrace, data)

  @notifier.post interactive: {
    header: {
      title: {tag: "plain_text", content: title}
    },
    elements: [{
      tag: "div",
      text: {tag: "lark_md", content: summary},
      fields: fields
    }]
  }
end