Class: Apostle::Mail
- Inherits:
-
Object
- Object
- Apostle::Mail
- Defined in:
- lib/apostle/mail.rb
Instance Attribute Summary collapse
-
#_exception ⇒ Object
Returns the value of attribute _exception.
-
#data ⇒ Object
Returns the value of attribute data.
-
#email ⇒ Object
Returns the value of attribute email.
-
#from ⇒ Object
Returns the value of attribute from.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#layout_id ⇒ Object
Returns the value of attribute layout_id.
-
#name ⇒ Object
Returns the value of attribute name.
-
#reply_to ⇒ Object
Returns the value of attribute reply_to.
-
#template_id ⇒ Object
Returns the value of attribute template_id.
Instance Method Summary collapse
- #attachments ⇒ Object
- #bcc=(address) ⇒ Object
- #cc=(address) ⇒ Object
- #deliver ⇒ Object
-
#deliver! ⇒ Object
Shortcut method to deliver a single message.
-
#header(name, value = nil) ⇒ Object
Provide a getter and setters for headers.
-
#initialize(template_id, options = {}) ⇒ Mail
constructor
A new instance of Mail.
-
#method_missing(method, *args) ⇒ Object
Allow convenience setters for the data payload E.G.
- #to_h ⇒ Object
- #to_json ⇒ Object
Constructor Details
#initialize(template_id, options = {}) ⇒ Mail
Returns a new instance of Mail.
19 20 21 22 23 24 25 26 |
# File 'lib/apostle/mail.rb', line 19 def initialize(template_id, = {}) @template_id = template_id @data = {} .each do |k, v| self.send("#{k}=", v) end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
Allow convenience setters for the data payload E.G. mail.potato= “Something” will set @data
47 48 49 50 |
# File 'lib/apostle/mail.rb', line 47 def method_missing(method, *args) return unless method.match /.*=/ @data[method.to_s.gsub(/=$/, '')] = args.first end |
Instance Attribute Details
#_exception ⇒ Object
Returns the value of attribute _exception.
9 10 11 |
# File 'lib/apostle/mail.rb', line 9 def _exception @_exception end |
#data ⇒ Object
Returns the value of attribute data.
9 10 11 |
# File 'lib/apostle/mail.rb', line 9 def data @data end |
#email ⇒ Object
Returns the value of attribute email.
9 10 11 |
# File 'lib/apostle/mail.rb', line 9 def email @email end |
#from ⇒ Object
Returns the value of attribute from.
9 10 11 |
# File 'lib/apostle/mail.rb', line 9 def from @from end |
#headers ⇒ Object
Returns the value of attribute headers.
9 10 11 |
# File 'lib/apostle/mail.rb', line 9 def headers @headers end |
#layout_id ⇒ Object
Returns the value of attribute layout_id.
9 10 11 |
# File 'lib/apostle/mail.rb', line 9 def layout_id @layout_id end |
#name ⇒ Object
Returns the value of attribute name.
9 10 11 |
# File 'lib/apostle/mail.rb', line 9 def name @name end |
#reply_to ⇒ Object
Returns the value of attribute reply_to.
9 10 11 |
# File 'lib/apostle/mail.rb', line 9 def reply_to @reply_to end |
#template_id ⇒ Object
Returns the value of attribute template_id.
9 10 11 |
# File 'lib/apostle/mail.rb', line 9 def template_id @template_id end |
Instance Method Details
#attachments ⇒ Object
101 102 103 |
# File 'lib/apostle/mail.rb', line 101 def ||= {} end |
#bcc=(address) ⇒ Object
41 42 43 |
# File 'lib/apostle/mail.rb', line 41 def bcc=(address) header("bcc", address) end |
#cc=(address) ⇒ Object
37 38 39 |
# File 'lib/apostle/mail.rb', line 37 def cc=(address) header("cc", address) end |
#deliver ⇒ Object
52 53 54 55 56 57 58 59 |
# File 'lib/apostle/mail.rb', line 52 def deliver begin deliver! true rescue DeliveryError => e false end end |
#deliver! ⇒ Object
Shortcut method to deliver a single message
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/apostle/mail.rb', line 62 def deliver! return true unless Apostle.deliver unless template_id && template_id != "" raise DeliveryError, "No email template_id provided" end queue = Apostle::Queue.new queue.add self queue.deliver! # Return true or false depending on successful delivery if queue.results[:valid].include?(self) return true else raise _exception end end |
#header(name, value = nil) ⇒ Object
Provide a getter and setters for headers
29 30 31 32 33 34 35 |
# File 'lib/apostle/mail.rb', line 29 def header(name, value = nil) if value (@headers ||= {})[name] = value else (@headers || {})[name] end end |
#to_h ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/apostle/mail.rb', line 82 def to_h { "#{self.email.to_s}" => { "data" => @data, "from" => from.to_s, "headers" => headers, "layout_id" => layout_id.to_s, "name" => name.to_s, "reply_to" => reply_to.to_s, "attachments" => , "template_id" => template_id.to_s }.delete_if { |k, v| !v || v == '' } } end |
#to_json ⇒ Object
97 98 99 |
# File 'lib/apostle/mail.rb', line 97 def to_json JSON.generate(to_h) end |