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
- #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.
18 19 20 21 22 23 24 25 |
# File 'lib/apostle/mail.rb', line 18 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
38 39 40 41 |
# File 'lib/apostle/mail.rb', line 38 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.
8 9 10 |
# File 'lib/apostle/mail.rb', line 8 def _exception @_exception end |
#data ⇒ Object
Returns the value of attribute data.
8 9 10 |
# File 'lib/apostle/mail.rb', line 8 def data @data end |
#email ⇒ Object
Returns the value of attribute email.
8 9 10 |
# File 'lib/apostle/mail.rb', line 8 def email @email end |
#from ⇒ Object
Returns the value of attribute from.
8 9 10 |
# File 'lib/apostle/mail.rb', line 8 def from @from end |
#headers ⇒ Object
Returns the value of attribute headers.
8 9 10 |
# File 'lib/apostle/mail.rb', line 8 def headers @headers end |
#layout_id ⇒ Object
Returns the value of attribute layout_id.
8 9 10 |
# File 'lib/apostle/mail.rb', line 8 def layout_id @layout_id end |
#name ⇒ Object
Returns the value of attribute name.
8 9 10 |
# File 'lib/apostle/mail.rb', line 8 def name @name end |
#reply_to ⇒ Object
Returns the value of attribute reply_to.
8 9 10 |
# File 'lib/apostle/mail.rb', line 8 def reply_to @reply_to end |
#template_id ⇒ Object
Returns the value of attribute template_id.
8 9 10 |
# File 'lib/apostle/mail.rb', line 8 def template_id @template_id end |
Instance Method Details
#deliver ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/apostle/mail.rb', line 43 def deliver begin deliver! true rescue DeliveryError => e false end end |
#deliver! ⇒ Object
Shortcut method to deliver a single message
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/apostle/mail.rb', line 53 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
28 29 30 31 32 33 34 |
# File 'lib/apostle/mail.rb', line 28 def header(name, value = nil) if value (@headers ||= {})[name] = value else (@headers || {})[name] end end |
#to_h ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/apostle/mail.rb', line 73 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, "template_id" => template_id.to_s }.delete_if { |k, v| !v || v == '' } } end |
#to_json ⇒ Object
87 88 89 |
# File 'lib/apostle/mail.rb', line 87 def to_json JSON.generate(to_h) end |