Class: ErrorStalker::Plugin::EmailSender

Inherits:
Base
  • Object
show all
Defined in:
lib/error_stalker/plugin/email_sender.rb

Overview

The email sender plugin will send an email to an address the first time an exception in a group is reported. Future exceptions that go in the same group will not trigger emails.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#exception_links

Constructor Details

#initialize(app, mail_params = {}) ⇒ EmailSender

Create a new instance of this plugin. mail_params is a hash of parameters that are used to build the report email.



17
18
19
20
# File 'lib/error_stalker/plugin/email_sender.rb', line 17

def initialize(app, mail_params = {})
  super(app, mail_params)
  @mail_params = mail_params
end

Instance Attribute Details

#mail_paramsObject (readonly)

Parameters that are used in order to figure out how and where to send the report email. These parameters are passed directly to the mail gem. See that page for a reference. The subject and body are created by the plugin, but all other parameters (to, from, etc.) will have to be passed in.



13
14
15
# File 'lib/error_stalker/plugin/email_sender.rb', line 13

def mail_params
  @mail_params
end

Instance Method Details

#after_create(app, report) ⇒ Object

Hook to trigger an email when a new exception report with report‘s digest comes in.



53
54
55
56
57
58
# File 'lib/error_stalker/plugin/email_sender.rb', line 53

def after_create(app, report)
  # Only send an email if it's the first exception of this type
  # we've seen
  send_email(app, report) if app.store.group(report.digest).count == 1
  super(app, report)
end

#build_email(exception_report, exception_url) ⇒ Object

Builds the mail object from exception_report that we can later deliver. This is mostly here to make testing easier.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/error_stalker/plugin/email_sender.rb', line 24

def build_email(exception_report, exception_url)
  @report = exception_report
  @url = exception_url
  
  mail = Mail.new({
      'subject' => "Exception on #{exception_report.machine} - #{exception_report.exception.to_s[0, 64]}",
      'body' => ERB.new(File.read(File.expand_path('views/exception_email.erb', File.dirname(__FILE__)))).result(binding)
    }.merge(mail_params))

  if mail_params['delivery_method']
    mail.delivery_method(mail_params['delivery_method'].to_sym, (mail_params['delivery_settings'] || {}))
  end
  
  mail
end

#send_email(app, exception_report) ⇒ Object

Sets up the parameters we need to build a new exception report email, and sends the mail.



42
43
44
45
46
47
48
49
# File 'lib/error_stalker/plugin/email_sender.rb', line 42

def send_email(app, exception_report)
  request = app.request
  host_with_port = request.host
  host_with_port << ":#{request.port}" if request.port != 80
  url = "#{request.scheme}://#{host_with_port}/exceptions/#{exception_report.id}.html"
  mail = build_email(exception_report, url)
  mail.deliver
end