Class: RDSBackup::Email

Inherits:
Object
  • Object
show all
Defined in:
lib/rds_backup_service/model/email.rb

Overview

an email representation of a Job, that can send itself to recipients.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(backup_job) ⇒ Email

constructor - requires an RDSBackup::Job



9
10
11
12
13
14
15
# File 'lib/rds_backup_service/model/email.rb', line 9

def initialize(backup_job)
  @job, @settings = backup_job, RDSBackup.settings
  use_tls = settings['email_tls'] != 'false'
  Mail.defaults do
    delivery_method :smtp, {enable_starttls_auto: use_tls}
  end
end

Instance Attribute Details

#jobObject (readonly)

Returns the value of attribute job.



6
7
8
# File 'lib/rds_backup_service/model/email.rb', line 6

def job
  @job
end

#settingsObject (readonly)

Returns the value of attribute settings.



6
7
8
# File 'lib/rds_backup_service/model/email.rb', line 6

def settings
  @settings
end

Instance Method Details

#bodyObject

defines the body of a Job’s status email



36
37
38
39
40
41
42
43
44
45
# File 'lib/rds_backup_service/model/email.rb', line 36

def body
  msg = "Hello.\n\n"
  if job.status == 200
    msg += "Your backup of database #{job.rds_id} is complete.\n"+
      (job.files.empty? ? "" : "Output is at #{job.files.first[:url]}\n")
  else
    msg += "Your backup is incomplete. (job ID #{job.backup_id})\n"
  end
  msg += "Job status: #{job.message}"
end

#send!Object

Attempts to send email through local ESMTP port 25. Raises an Exception on failure.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rds_backup_service/model/email.rb', line 19

def send!
  raise "job #{job.backup_id} has no email option" unless job.options[:email]
  # define local variables for closure over Mail.new
  from_address  = settings['email_from']
  to_address    = job.options[:email]
  subject_text  = "Backup of RDS #{job.rds_id} (job ID #{job.backup_id})"
  body_text     = body
  mail = Mail.new do
    from    from_address
    to      to_address
    subject subject_text
    body    "#{body_text}\n"
  end
  mail.deliver!
end