-
Notifications
You must be signed in to change notification settings - Fork 17
/
adapter.rb
88 lines (76 loc) · 2.31 KB
/
adapter.rb
1
2
3
4
5
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
require 'active_support/core_ext/string/strip'
require_relative 'attachment'
module Griddler
module Postmark
class Adapter
def initialize(params)
@params = params
end
def self.normalize_params(params)
adapter = new(params)
adapter.normalize_params
end
def normalize_params
{
to: extract_recipients(:ToFull),
cc: extract_recipients(:CcFull),
bcc: extract_recipients(:BccFull),
original_recipient: params[:OriginalRecipient],
reply_to: params[:ReplyTo],
from: full_email(params[:FromFull]),
subject: params[:Subject],
text: params[:TextBody],
html: params[:HtmlBody],
attachments: attachment_files,
headers: headers,
vendor_specific: {
mailbox_hash: params[:MailboxHash],
message_id: params[:MessageID],
stripped_text_reply: params[:StrippedTextReply]
}
}
end
private
attr_reader :params
def headers
Array(@params[:Headers]).inject({}) do |hash, header|
hash[header[:Name]] = header[:Value]
hash
end
end
def extract_recipients(key)
params[key].to_a.map { |recipient| full_email(recipient) }
end
def full_email(contact_info)
email = contact_info[:Email]
if contact_info[:Name].present?
"#{contact_info[:Name]} <#{email}>"
else
email
end
end
def attachment_files
attachments = Array(params[:Attachments])
attachments.map do |attachment|
Griddler::Postmark::Attachment.new({
filename: attachment[:Name],
type: attachment[:ContentType],
tempfile: create_tempfile(attachment),
content_id: attachment[:ContentID]
})
end
end
def create_tempfile(attachment)
filename = attachment[:Name].gsub(/\/|\\/, '_')[0..150]
tempfile = Tempfile.new(filename, Dir::tmpdir, encoding: 'ascii-8bit')
tempfile.write(content(attachment))
tempfile.rewind
tempfile
end
def content(attachment)
content = attachment[:Content] || attachment[:Data]
Base64.decode64(content) if content
end
end
end
end