Class: Pdm::Mail
Instance Attribute Summary collapse
Attributes inherited from Resource
#options
Instance Method Summary
collapse
Methods inherited from Resource
#api_request, #check_opts, #with_exceptions
Constructor Details
#initialize(mail, options = {}) ⇒ Mail
11
12
13
14
|
# File 'lib/pdm/mail.rb', line 11
def initialize(mail, options = {})
self.mail = mail
super(options)
end
|
Instance Attribute Details
#guid ⇒ Object
Returns the value of attribute guid.
9
10
11
|
# File 'lib/pdm/mail.rb', line 9
def guid
@guid
end
|
#last_api_response ⇒ Object
Returns the value of attribute last_api_response.
9
10
11
|
# File 'lib/pdm/mail.rb', line 9
def last_api_response
@last_api_response
end
|
#mail ⇒ Object
Returns the value of attribute mail.
9
10
11
|
# File 'lib/pdm/mail.rb', line 9
def mail
@mail
end
|
Instance Method Details
#attachment_params(mail) ⇒ Object
65
66
67
68
69
70
|
# File 'lib/pdm/mail.rb', line 65
def attachment_params(mail)
return {} if mail.attachments.empty?
{
"job[attachments]" => attachments_for_rest_client(mail.attachments),
}
end
|
#attachments_for_rest_client(attachments) ⇒ Object
59
60
61
62
63
|
# File 'lib/pdm/mail.rb', line 59
def attachments_for_rest_client(attachments)
attachments.map do |attachment|
Pdm::Attachment.new(attachment)
end
end
|
#basic_params(mail) ⇒ Object
33
34
35
36
37
38
39
40
|
# File 'lib/pdm/mail.rb', line 33
def basic_params(mail)
{
"job[from]" => mail.from.first,
"job[reply_to]" => mail.reply_to,
"job[subject]" => mail.subject,
"job[recipients][][email]" => mail.to.first,
}
end
|
#body_params(mail) ⇒ Object
42
43
44
45
46
47
48
49
50
|
# File 'lib/pdm/mail.rb', line 42
def body_params(mail)
plain_part, html_part = get_body_parts(mail).map do |p|
p && p.body.to_s
end
{
"job[plain]" => plain_part,
"job[html]" => html_part,
}
end
|
#create_and_send_job(mail) ⇒ Object
80
81
82
83
84
85
|
# File 'lib/pdm/mail.rb', line 80
def create_and_send_job(mail)
params = create_and_send_job_params(mail)
with_exceptions do
api_request.post(:jobs, params)
end
end
|
#create_and_send_job_params(mail) ⇒ Object
72
73
74
75
76
77
78
|
# File 'lib/pdm/mail.rb', line 72
def create_and_send_job_params(mail)
params = basic_params(mail)
params.merge!(body_params(mail))
params.merge!(personalization_params(mail)) if mail.respond_to?(:personalization)
params.merge!(attachment_params(mail))
params.reject { |k, v| v.nil? || (v.respond_to?(:empty?) && v.empty?) }
end
|
#deliver ⇒ Object
112
113
114
115
116
117
118
119
120
121
|
# File 'lib/pdm/mail.rb', line 112
def deliver
res = create_and_send_job(mail)
case res.code
when 200
parse_api_response(res)
end
return res
end
|
#get_body_part(parts, content_type) ⇒ Object
17
18
19
20
21
22
|
# File 'lib/pdm/mail.rb', line 17
def get_body_part(parts, content_type)
parts.detect do |p|
p.content_type.include?(content_type) &&
p.[:content_disposition].nil?
end
end
|
#get_body_parts(mail) ⇒ Object
24
25
26
27
28
29
30
31
|
# File 'lib/pdm/mail.rb', line 24
def get_body_parts(mail)
[
"text/plain",
"text/html",
].map do |content_type|
get_body_part(mail.parts, content_type)
end
end
|
#parse_api_response(res) ⇒ Object
87
88
89
90
91
92
93
94
|
# File 'lib/pdm/mail.rb', line 87
def parse_api_response(res)
self.last_api_response = res = Json.decode(res)
if singleton?(res)
parse_singleton_response(res)
else
parse_multiple_response(res)
end
end
|
#parse_multiple_response(res) ⇒ Object
108
109
110
|
# File 'lib/pdm/mail.rb', line 108
def parse_multiple_response(res)
end
|
#parse_singleton_response(res) ⇒ Object
100
101
102
103
104
105
106
|
# File 'lib/pdm/mail.rb', line 100
def parse_singleton_response(res)
root = res["singleton_job"]
root.each do |key, value|
setter = "#{key}="
__send__(setter, value) if respond_to?(setter)
end
end
|
#personalization_params(mail) ⇒ Object
52
53
54
55
56
57
|
# File 'lib/pdm/mail.rb', line 52
def personalization_params(mail)
return {} if mail.personalization.nil?
{
"job[personalization]" => mail.personalization,
}
end
|
#singleton?(res) ⇒ Boolean
96
97
98
|
# File 'lib/pdm/mail.rb', line 96
def singleton?(res)
res.has_key?("singleton_job")
end
|