Class: Apostle::Mail

Inherits:
Object
  • Object
show all
Defined in:
lib/apostle/mail.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options = {})
  @template_id = template_id
  @data = {}

  options.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

#_exceptionObject

Returns the value of attribute _exception.



9
10
11
# File 'lib/apostle/mail.rb', line 9

def _exception
  @_exception
end

#dataObject

Returns the value of attribute data.



9
10
11
# File 'lib/apostle/mail.rb', line 9

def data
  @data
end

#emailObject

Returns the value of attribute email.



9
10
11
# File 'lib/apostle/mail.rb', line 9

def email
  @email
end

#fromObject

Returns the value of attribute from.



9
10
11
# File 'lib/apostle/mail.rb', line 9

def from
  @from
end

#headersObject

Returns the value of attribute headers.



9
10
11
# File 'lib/apostle/mail.rb', line 9

def headers
  @headers
end

#layout_idObject

Returns the value of attribute layout_id.



9
10
11
# File 'lib/apostle/mail.rb', line 9

def layout_id
  @layout_id
end

#nameObject

Returns the value of attribute name.



9
10
11
# File 'lib/apostle/mail.rb', line 9

def name
  @name
end

#reply_toObject

Returns the value of attribute reply_to.



9
10
11
# File 'lib/apostle/mail.rb', line 9

def reply_to
  @reply_to
end

#template_idObject

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

#attachmentsObject



101
102
103
# File 'lib/apostle/mail.rb', line 101

def attachments
  @_attachments ||= {}
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

#deliverObject



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_hObject



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" => encoded_attachments,
      "template_id" => template_id.to_s
    }.delete_if { |k, v| !v || v == '' }
  }
end

#to_jsonObject



97
98
99
# File 'lib/apostle/mail.rb', line 97

def to_json
  JSON.generate(to_h)
end