Class: ExceptionNotifier::EmailNotifier

Inherits:
BaseNotifier show all
Defined in:
lib/exception_notifier/email_notifier.rb

Defined Under Namespace

Modules: Mailer

Instance Attribute Summary collapse

Attributes inherited from BaseNotifier

#base_options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseNotifier

#_post_callback, #_pre_callback, #send_notice

Constructor Details

#initialize(options) ⇒ EmailNotifier

Returns a new instance of EmailNotifier.



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/exception_notifier/email_notifier.rb', line 125

def initialize(options)
  super
  delivery_method = (options[:delivery_method] || :smtp)
  mailer_settings_key = "#{delivery_method}_settings".to_sym
  options[:mailer_settings] = options.delete(mailer_settings_key)

  options.reverse_merge(EmailNotifier.default_options).select{|k,v|[
    :sender_address, :exception_recipients,
    :pre_callback, :post_callback,
    :email_prefix, :email_format, :sections, :background_sections,
    :verbose_subject, :normalize_subject, :delivery_method, :mailer_settings,
    :email_headers, :mailer_parent, :template_path, :deliver_with].include?(k)}.each{|k,v| send("#{k}=", v)}
end

Instance Attribute Details

#background_sectionsObject

Returns the value of attribute background_sections.



8
9
10
# File 'lib/exception_notifier/email_notifier.rb', line 8

def background_sections
  @background_sections
end

#deliver_withObject

Returns the value of attribute deliver_with.



8
9
10
# File 'lib/exception_notifier/email_notifier.rb', line 8

def deliver_with
  @deliver_with
end

#delivery_methodObject

Returns the value of attribute delivery_method.



8
9
10
# File 'lib/exception_notifier/email_notifier.rb', line 8

def delivery_method
  @delivery_method
end

#email_formatObject

Returns the value of attribute email_format.



8
9
10
# File 'lib/exception_notifier/email_notifier.rb', line 8

def email_format
  @email_format
end

#email_headersObject

Returns the value of attribute email_headers.



8
9
10
# File 'lib/exception_notifier/email_notifier.rb', line 8

def email_headers
  @email_headers
end

#email_prefixObject

Returns the value of attribute email_prefix.



8
9
10
# File 'lib/exception_notifier/email_notifier.rb', line 8

def email_prefix
  @email_prefix
end

#exception_recipientsObject

Returns the value of attribute exception_recipients.



8
9
10
# File 'lib/exception_notifier/email_notifier.rb', line 8

def exception_recipients
  @exception_recipients
end

#mailer_parentObject

Returns the value of attribute mailer_parent.



8
9
10
# File 'lib/exception_notifier/email_notifier.rb', line 8

def mailer_parent
  @mailer_parent
end

#mailer_settingsObject

Returns the value of attribute mailer_settings.



8
9
10
# File 'lib/exception_notifier/email_notifier.rb', line 8

def mailer_settings
  @mailer_settings
end

#normalize_subjectObject

Returns the value of attribute normalize_subject.



8
9
10
# File 'lib/exception_notifier/email_notifier.rb', line 8

def normalize_subject
  @normalize_subject
end

#post_callbackObject

Returns the value of attribute post_callback.



8
9
10
# File 'lib/exception_notifier/email_notifier.rb', line 8

def post_callback
  @post_callback
end

#pre_callbackObject

Returns the value of attribute pre_callback.



8
9
10
# File 'lib/exception_notifier/email_notifier.rb', line 8

def pre_callback
  @pre_callback
end

#sectionsObject

Returns the value of attribute sections.



8
9
10
# File 'lib/exception_notifier/email_notifier.rb', line 8

def sections
  @sections
end

#sender_addressObject

Returns the value of attribute sender_address.



8
9
10
# File 'lib/exception_notifier/email_notifier.rb', line 8

def sender_address
  @sender_address
end

#template_pathObject

Returns the value of attribute template_path.



8
9
10
# File 'lib/exception_notifier/email_notifier.rb', line 8

def template_path
  @template_path
end

#verbose_subjectObject

Returns the value of attribute verbose_subject.



8
9
10
# File 'lib/exception_notifier/email_notifier.rb', line 8

def verbose_subject
  @verbose_subject
end

Class Method Details

.default_optionsObject



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/exception_notifier/email_notifier.rb', line 170

def self.default_options
  {
    :sender_address => %("Exception Notifier" <[email protected]>),
    :exception_recipients => [],
    :email_prefix => "[ERROR] ",
    :email_format => :text,
    :sections => %w(request session environment backtrace),
    :background_sections => %w(backtrace data),
    :verbose_subject => true,
    :normalize_subject => false,
    :delivery_method => nil,
    :mailer_settings => nil,
    :email_headers => {},
    :mailer_parent => 'ActionMailer::Base',
    :template_path => 'exception_notifier',
    :deliver_with => :deliver_now
  }
end

.normalize_digits(string) ⇒ Object



189
190
191
# File 'lib/exception_notifier/email_notifier.rb', line 189

def self.normalize_digits(string)
  string.gsub(/[0-9]+/, 'N')
end

Instance Method Details

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



152
153
154
# File 'lib/exception_notifier/email_notifier.rb', line 152

def call(exception, options={})
  create_email(exception, options).send(deliver_with)
end

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



156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/exception_notifier/email_notifier.rb', line 156

def create_email(exception, options={})
  env = options[:env]
  default_options = self.options
  if env.nil?
    send_notice(exception, options, nil, default_options) do |_, default_opts|
      mailer.background_exception_notification(exception, options, default_opts)
    end
  else
    send_notice(exception, options, nil, default_options) do |_, default_opts|
      mailer.exception_notification(env, exception, options, default_opts)
    end
  end
end

#mailerObject



145
146
147
148
149
150
# File 'lib/exception_notifier/email_notifier.rb', line 145

def mailer
  @mailer ||= Class.new(mailer_parent.constantize).tap do |mailer|
    mailer.extend(EmailNotifier::Mailer)
    mailer.mailer_name = template_path
  end
end

#optionsObject



139
140
141
142
143
# File 'lib/exception_notifier/email_notifier.rb', line 139

def options
  @options ||= {}.tap do |opts|
    self.instance_variables.each { |var| opts[var[1..-1].to_sym] = self.instance_variable_get(var) }
  end
end