Class: Opticon::Notifier::Email

Inherits:
Object
  • Object
show all
Defined in:
lib/opticon/notifier.rb

Overview

Sends failure notifications via email to the given list of recipients.

To set options, configure Opticon::Mailer the same way you would any other ActionMailer. For example, to send via SMTP:

Opticon::Mailer.delivery_method = :smtp

Opticon::Mailer.server_settings = {
  :address => "mail.nowhere.foo",
  :domain => "nowhere.foo",
  :authentication => :login,
  :user_name => "mail_user",
  :password => "topsecret"
}

Constant Summary collapse

@@resend =

The same notification will not be resent over and over again unless this is set to true.

false

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(recipients, options = {}) ⇒ Email

Returns a new instance of Email.



28
29
30
31
# File 'lib/opticon/notifier.rb', line 28

def initialize(recipients, options = {})
  @recipients = recipients
  @from = options[:from] || "opticon@#{ENV['HOSTNAME']}"
end

Instance Attribute Details

#fromObject

Returns the value of attribute from.



22
23
24
# File 'lib/opticon/notifier.rb', line 22

def from
  @from
end

#recipientsObject

Returns the value of attribute recipients.



22
23
24
# File 'lib/opticon/notifier.rb', line 22

def recipients
  @recipients
end

Instance Method Details

#notify(failures) ⇒ Object



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

def notify(failures)
  failures = [failures] unless failures.kind_of? Array
  
  failures_by_uri = {}
  failures.each do |f|
    failures_by_uri[f.uri] ||= []
    failures_by_uri[f.uri] << f
  end
  
  if ARGV.include?("-v")
    puts "Notifying #{recipients.inspect} about #{failures.size} failures:"
    failures.each{|f| puts "  #{f.failure_message}"}
  end
  
  failures_by_uri.each do |uri, failures|
    Opticon::Mailer.deliver_failure_notification(
      uri,
      failures.collect{|f| f.failure_message}.join("\n"),
      recipients, from
    )
  end
end