Class: Mandrails::MessageBuilder
- Inherits:
-
Object
- Object
- Mandrails::MessageBuilder
- 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
-
#defaults ⇒ Object
readonly
Access to mail and defaults.
-
#mail ⇒ Object
readonly
Access to mail and defaults.
Instance Method Summary collapse
- #as_json ⇒ Object
-
#attachments ⇒ Object
Internal: Extract attachments.
-
#body(format) ⇒ Object
Internal: Extract body of specified format, if any.
-
#from_email ⇒ Object
Internal: Extract from email.
-
#from_name ⇒ Object
Internal: Extract from name from either the header or defaults.
-
#headers ⇒ Object
Internal: Extract Reply-To header field.
-
#initialize(mail, defaults = {}) ⇒ MessageBuilder
constructor
Public:.
- #message ⇒ Object
-
#recipients ⇒ Object
Internal: Build recipients list.
- #text? ⇒ Boolean
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
#defaults ⇒ Object (readonly)
Access to mail and defaults
18 19 20 |
# File 'lib/mandrails/message_builder.rb', line 18 def defaults @defaults end |
#mail ⇒ Object (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_json ⇒ Object
96 97 98 |
# File 'lib/mandrails/message_builder.rb', line 96 def as_json end |
#attachments ⇒ Object
Internal: Extract attachments.
Returns Array of Hash.
79 80 81 82 83 84 |
# File 'lib/mandrails/message_builder.rb', line 79 def return unless mail..length > 0 mail..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_email ⇒ Object
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_name ⇒ Object
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 |
#headers ⇒ Object
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 |
#message ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/mandrails/message_builder.rb', line 27 def @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: , headers: headers) end |
#recipients ⇒ Object
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
65 66 67 |
# File 'lib/mandrails/message_builder.rb', line 65 def text? mail.mime_type =~ %r{\Atext/plain} || !mail.mime_type end |