Class: Backup::Mail::Base
- Inherits:
-
Object
- Object
- Backup::Mail::Base
- Defined in:
- lib/backup/mail/base.rb
Class Method Summary collapse
- .gsub_content(lines) ⇒ Object
-
.notify!(backup) ⇒ Object
Delivers the backup details by email to the recipient Requires the Backup Object.
- .parse_body ⇒ Object
-
.setup(config) ⇒ Object
Sets up the Mail Configuration for the Backup::Mail::Base class.
-
.setup? ⇒ Boolean
Returns true if the “to” and “from” attributes are set.
-
.smtp_configuration ⇒ Object
Retrieves SMTP configuration.
Class Method Details
.gsub_content(lines) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/backup/mail/base.rb', line 66 def self.gsub_content(lines) bucket = @backup.procedure.get_storage_configuration.attributes['bucket'] path = @backup.procedure.get_storage_configuration.attributes['path'] ip = @backup.procedure.get_storage_configuration.attributes['ip'] lines.each do |line| line.gsub!(':trigger', @backup.trigger) line.gsub!(':day', Time.now.strftime("%A (%d)")) line.gsub!(':month', Time.now.strftime("%B")) line.gsub!(':year', Time.now.strftime("%Y")) line.gsub!(':time', Time.now.strftime("%r")) line.gsub!(':adapter', @backup.procedure.adapter_name.to_s) line.gsub!(':location', bucket || path) line.gsub!(':backup', @backup.final_file) case @backup.procedure.storage_name.to_sym when :s3 then line.gsub!(':remote', "on Amazon S3") when :local then line.gsub!(':remote', "on the local server") when :scp, :sftp, :ftp then line.gsub!(':remote', "on the remote server (#{ip})") end @content ||= String.new @content << line end end |
.notify!(backup) ⇒ Object
Delivers the backup details by email to the recipient Requires the Backup Object
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/backup/mail/base.rb', line 33 def self.notify!(backup) if self.setup? and backup.procedure.attributes['notify'].eql?(true) @backup = backup self.parse_body Pony.mail({ :subject => "Backup for \"#{@backup.trigger}\" was successfully created!", :body => @content }.merge(self.smtp_configuration)) puts "Sending notification to #{self.to}." end end |
.parse_body ⇒ Object
60 61 62 63 64 |
# File 'lib/backup/mail/base.rb', line 60 def self.parse_body File.open(File.join(File.dirname(__FILE__), 'mail.txt'), 'r') do |file| self.gsub_content(file.readlines) end end |
.setup(config) ⇒ Object
Sets up the Mail Configuration for the Backup::Mail::Base class. This must be set in order to send emails It will dynamically add class methods (configuration) for each email that will be sent
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/backup/mail/base.rb', line 8 def self.setup(config) if config (class << self; self; end).instance_eval do config.attributes.each do |method, value| define_method method do value end end config.get_smtp_configuration.attributes.each do |method, value| define_method method do value end end end end end |
.setup? ⇒ Boolean
Returns true if the “to” and “from” attributes are set
26 27 28 29 |
# File 'lib/backup/mail/base.rb', line 26 def self.setup? return true if defined?(from) and defined?(to) false end |
.smtp_configuration ⇒ Object
Retrieves SMTP configuration
46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/backup/mail/base.rb', line 46 def self.smtp_configuration = { :to => self.to, :from => self.from, :via => :smtp, :smtp => { :host => self.host, :port => self.port, :domain => self.domain, }} [:smtp].merge(:auth => self.authentication, :user => self.username, :password => self.password) if self.responds_to? :authentication end |