Class: Mandrails::MessageBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/mandrails/message_builder.rb

Overview

The MessageBuilder is used to convert a Mail::Message into a JSON object consumable by the Mandrill API.

Constant Summary collapse

RESTRICTED_KEYS =

Setting keys which are not allowed to by set by the message

%w{key async}
MANDRILL_SETTINGS =

Known mandrill settings

[:track_opens, :track_clicks, :auto_text,
:url_strip_qs, :preserve_recipients, :bcc_address]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mail, defaults = {}) ⇒ MessageBuilder

Public:



22
23
24
25
# File 'lib/mandrails/message_builder.rb', line 22

def initialize(mail, defaults = {})
  @mail = mail
  @defaults = defaults.reject { |key, value| RESTRICTED_KEYS.include?(key.to_s) }
end

Instance Attribute Details

#defaultsObject (readonly)

Access to mail and defaults



18
19
20
# File 'lib/mandrails/message_builder.rb', line 18

def defaults
  @defaults
end

#mailObject (readonly)

Access to mail and defaults



18
19
20
# File 'lib/mandrails/message_builder.rb', line 18

def mail
  @mail
end

Instance Method Details

#as_jsonObject



96
97
98
# File 'lib/mandrails/message_builder.rb', line 96

def as_json
  message
end

#attachmentsObject

Internal: Extract attachments.

Returns Array of Hash.



79
80
81
82
83
84
# File 'lib/mandrails/message_builder.rb', line 79

def attachments
  return unless mail.attachments.length > 0
  mail.attachments.map do |part|
    { type: part.mime_type, name: part.filename, content: Base64.encode64(part.body.raw_source).strip }
  end
end

#body(format) ⇒ Object

Internal: Extract body of specified format, if any.

Returns String or nil.



59
60
61
62
63
# File 'lib/mandrails/message_builder.rb', line 59

def body(format)
  content = mail.send("#{format}_part").presence
  content ||= mail if mail.mime_type =~ %r{\Atext/#{format}} || format == :text && text?
  content.body.raw_source if content.present?
end

#from_emailObject

Internal: Extract from email.

Returns String



52
53
54
# File 'lib/mandrails/message_builder.rb', line 52

def from_email
  mail.from && mail.from.first.presence || defaults[:from_email]
end

#from_nameObject

Internal: Extract from name from either the header or defaults.

Returns String.



45
46
47
# File 'lib/mandrails/message_builder.rb', line 45

def from_name
  mail.header['from-name'].to_s.presence || defaults[:from_name]
end

#headersObject

Internal: Extract Reply-To header field. FIXME: extract all X-* headers as well!

Returns Hash.



90
91
92
93
94
# File 'lib/mandrails/message_builder.rb', line 90

def headers
  headers = {}
  headers['Reply-To'] = mail.reply_to.first.to_s if mail.reply_to.present?
  headers
end

#messageObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/mandrails/message_builder.rb', line 27

def message
  @message ||= defaults.merge(
    # E-Mail stuff
    html: body(:html),
    text: body(:text),
    subject: mail.subject,
    from_email: from_email,
    from_name: from_name,
    to: recipients,

    # Additional headers
    attachments: attachments,
    headers: headers)
end

#recipientsObject

Internal: Build recipients list.

Returns Array of Hash with ‘:name`, `:email`.



72
73
74
# File 'lib/mandrails/message_builder.rb', line 72

def recipients
  [mail.to, mail.cc].compact.flatten.map { |email| { email: email, name: email } }
end

#text?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/mandrails/message_builder.rb', line 65

def text?
  mail.mime_type =~ %r{\Atext/plain} || !mail.mime_type
end