Class: IronMailer::SendEmail
- Inherits:
-
Object
- Object
- IronMailer::SendEmail
- Defined in:
- lib/iron_mailer/send_email.rb
Instance Attribute Summary collapse
-
#cache ⇒ Object
readonly
Returns the value of attribute cache.
-
#debug ⇒ Object
readonly
Returns the value of attribute debug.
-
#delivery_method ⇒ Object
readonly
Returns the value of attribute delivery_method.
-
#delivery_method_settings ⇒ Object
readonly
Returns the value of attribute delivery_method_settings.
-
#iron_cache_client ⇒ Object
readonly
Returns the value of attribute iron_cache_client.
-
#iron_mq_client ⇒ Object
readonly
Returns the value of attribute iron_mq_client.
-
#queue ⇒ Object
readonly
Returns the value of attribute queue.
Instance Method Summary collapse
- #cache_email(mail) ⇒ Object
- #execute ⇒ Object
- #extract_cache_info(mail) ⇒ Object
-
#initialize(options = {}) ⇒ SendEmail
constructor
A new instance of SendEmail.
- #split_email(source_email) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ SendEmail
Returns a new instance of SendEmail.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/iron_mailer/send_email.rb', line 6 def initialize(={}) @iron_mq_client = IronMQ::Client.new(['iron_mq']) @queue = @iron_mq_client.queue(['iron_mq']['queue_name']) @iron_cache_client = IronCache::Client.new(['iron_cache']) @cache = @iron_cache_client.cache(['iron_cache']['cache_name']) @debug = ['debug'] if ['delivery_method'].is_a?(Hash) delivery_method_hash = ['delivery_method'] @delivery_method=delivery_method_hash.keys.first.to_sym @delivery_method_settings = {} params = delivery_method_hash.values.first.clone params.keys.each do |key| @delivery_method_settings[key.to_sym] = params[key] end else @delivery_method=['delivery_method'].to_sym @delivery_method_settings={} end end |
Instance Attribute Details
#cache ⇒ Object (readonly)
Returns the value of attribute cache.
4 5 6 |
# File 'lib/iron_mailer/send_email.rb', line 4 def cache @cache end |
#debug ⇒ Object (readonly)
Returns the value of attribute debug.
4 5 6 |
# File 'lib/iron_mailer/send_email.rb', line 4 def debug @debug end |
#delivery_method ⇒ Object (readonly)
Returns the value of attribute delivery_method.
4 5 6 |
# File 'lib/iron_mailer/send_email.rb', line 4 def delivery_method @delivery_method end |
#delivery_method_settings ⇒ Object (readonly)
Returns the value of attribute delivery_method_settings.
4 5 6 |
# File 'lib/iron_mailer/send_email.rb', line 4 def delivery_method_settings @delivery_method_settings end |
#iron_cache_client ⇒ Object (readonly)
Returns the value of attribute iron_cache_client.
4 5 6 |
# File 'lib/iron_mailer/send_email.rb', line 4 def iron_cache_client @iron_cache_client end |
#iron_mq_client ⇒ Object (readonly)
Returns the value of attribute iron_mq_client.
4 5 6 |
# File 'lib/iron_mailer/send_email.rb', line 4 def iron_mq_client @iron_mq_client end |
#queue ⇒ Object (readonly)
Returns the value of attribute queue.
4 5 6 |
# File 'lib/iron_mailer/send_email.rb', line 4 def queue @queue end |
Instance Method Details
#cache_email(mail) ⇒ Object
64 65 66 67 68 |
# File 'lib/iron_mailer/send_email.rb', line 64 def cache_email(mail) field = mail.header['X-SMTPAPI'].is_a?(Array) ? mail.header['X-SMTPAPI'].first : mail.header['X-SMTPAPI'] cache_key = JSON.parse(field.value)['unique_args']['cache_key'] self.cache.put(cache_key, self.extract_cache_info(mail).to_json) end |
#execute ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/iron_mailer/send_email.rb', line 70 def execute = self.queue.get(:n=>100) errors = [] .each do || begin source_email = Mail.new(.body) puts "Processing: #{source_email.subject}" if self.debug puts self.delivery_method=>self.delivery_method_settings if self.debug puts source_email if self.debug mails = self.split_email(source_email) mails.each do |mail| mail.delivery_method self.delivery_method, self.delivery_method_settings mail.deliver cache_email(mail) end .delete rescue Exception=>e if self.debug puts e. puts e.backtrace end errors << end end errors.each do || puts "Releasing: #{.id}" if self.debug .release end end |
#extract_cache_info(mail) ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/iron_mailer/send_email.rb', line 55 def extract_cache_info(mail) {'subject'=>mail.subject, 'date'=>mail.date, 'from'=>mail.from, 'to'=>mail.to, 'mail'=>mail.to_s } end |
#split_email(source_email) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/iron_mailer/send_email.rb', line 32 def split_email(source_email) puts "split_email: #{source_email}" tos = source_email.to tos = [tos] if tos.is_a?(String) tos.uniq.map{|destination| duplicate = Mail.new(source_email) duplicate.to = destination if destination==tos.first duplicate.=source_email. duplicate.bcc=source_email.bcc duplicate.cc=source_email.cc else duplicate. = nil duplicate.bcc = nil duplicate.cc = nil end duplicate.header['X-SMTPAPI']={unique_args: {cache_key: UUIDTools::UUID.random_create.to_s}}.to_json duplicate } end |