Class: ExceptionNotifier::SimpleEmailNotifier

Inherits:
Object
  • Object
show all
Defined in:
lib/exception_notifier/simple_email_notifier.rb

Defined Under Namespace

Classes: UndefinedDeliveryError

Constant Summary collapse

ENV_DATA_KEY =
'exception_notifier.exception_data'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ SimpleEmailNotifier

Returns a new instance of SimpleEmailNotifier.



17
18
19
# File 'lib/exception_notifier/simple_email_notifier.rb', line 17

def initialize(options)
  @options = self.class.default_options.merge(options)
end

Class Method Details

.default_optionsObject



9
10
11
12
13
14
15
# File 'lib/exception_notifier/simple_email_notifier.rb', line 9

def self.default_options
  {
    :sender_address => %("Exception Notifier" <[email protected]>),
    :exception_recipients => [],
    :email_prefix => "[ERROR] ",
  }
end

Instance Method Details

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



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

def call(exception, options = {})
  env = extract_env_from_options!(options)

  delivery_method.call(
    from: @options.fetch(:sender_address),
    to: @options.fetch(:exception_recipients),
    subject: compose_subject(exception, env, options),
    body: compose_message(exception, env, options)
  )
end

#compose_message(exception, env, options) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/exception_notifier/simple_email_notifier.rb', line 56

def compose_message(exception, env, options)
  SimpleEmailExceptionNotifier::Message.compose do |m|
    m.print_summary(exception)
    m.print_section('Backtrace', exception.backtrace)

    unless env.empty?
      m.print_request(Rack::Request.new(env)) if defined?(Rack::Request)
      m.print_section('Environment', filter_env(env))
    end

    m.print_section('Data', extract_data(env, options))
  end
end

#compose_subject(exception, env, options) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/exception_notifier/simple_email_notifier.rb', line 48

def compose_subject(exception, env, options)
  [
    @options.fetch(:email_prefix),
    env['PATH_INFO'].to_s + ' ',
    exception.class.name,
  ].join.squeeze(' ')
end

#delivery_methodObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/exception_notifier/simple_email_notifier.rb', line 32

def delivery_method
  @options.fetch(:delivery_method) do
    if defined?(Mail)
      Mail.method(:deliver)
    elsif defined?(Pony)
      Pony.method(:mail)
    else
      raise UndefinedDeliveryError.new "Undefined :delivery_method!\n" +
        " You can add gem 'mail' or 'pony' to Gemfile or provide custom method.\n" +
        " It is supposed to send message on #call.\n" +
        " Arguments are :from, :to, :subject and :body.\n" +
        " Examples: Mail.method(:deliver), Pony.method(:mail)"
    end
  end
end