class MandrillDm::Message

Attributes

mail[R]

Public Class Methods

new(mail) click to toggle source
# File lib/mandrill_dm/message.rb, line 7
def initialize(mail)
  @mail = mail
end

Public Instance Methods

attachments() click to toggle source

Returns a Mandrill API compatible attachment hash

# File lib/mandrill_dm/message.rb, line 12
def attachments
  regular_attachments = mail.attachments.reject(&:inline?)
  regular_attachments.collect do |attachment|
    {
      name: attachment.filename,
      type: attachment.mime_type,
      content: Base64.encode64(attachment.body.decoded)
    }
  end
end
auto_html() click to toggle source
# File lib/mandrill_dm/message.rb, line 39
def auto_html
  nil_true_false?(:auto_html)
end
auto_text() click to toggle source
# File lib/mandrill_dm/message.rb, line 35
def auto_text
  nil_true_false?(:auto_text)
end
bcc_address() click to toggle source
# File lib/mandrill_dm/message.rb, line 43
def bcc_address
  return_string_value(:bcc_address)
end
from_email() click to toggle source
# File lib/mandrill_dm/message.rb, line 47
def from_email
  from.address
end
from_name() click to toggle source
# File lib/mandrill_dm/message.rb, line 51
def from_name
  from.display_name
end
global_merge_vars() click to toggle source
# File lib/mandrill_dm/message.rb, line 55
def global_merge_vars
  get_value(:global_merge_vars)
end
headers() click to toggle source
# File lib/mandrill_dm/message.rb, line 59
def headers
  mail.header_fields.reduce({}) do |acc, field|
    acc.merge(field.name => field.value)
  end
end
html() click to toggle source
# File lib/mandrill_dm/message.rb, line 65
def html
  return mail.html_part.body.decoded if mail.html_part
  return_decoded_body('text/html')
end
images() click to toggle source

Mandrill uses a different hash for inlined image attachments

# File lib/mandrill_dm/message.rb, line 24
def images
  inline_attachments = mail.attachments.select(&:inline?)
  inline_attachments.collect do |attachment|
    {
      name: attachment.cid,
      type: attachment.mime_type,
      content: Base64.encode64(attachment.body.decoded)
    }
  end
end
important() click to toggle source
# File lib/mandrill_dm/message.rb, line 78
def important
  mail[:important].to_s == 'true'
end
inline_css() click to toggle source
# File lib/mandrill_dm/message.rb, line 82
def inline_css
  nil_true_false?(:inline_css)
end
ip_pool() click to toggle source
# File lib/mandrill_dm/message.rb, line 86
def ip_pool
  return_string_value(:ip_pool)
end
merge() click to toggle source
# File lib/mandrill_dm/message.rb, line 90
def merge
  nil_true_false?(:merge)
end
merge_language() click to toggle source
# File lib/mandrill_dm/message.rb, line 94
def merge_language
  return_string_value(:merge_language)
end
merge_vars() click to toggle source
# File lib/mandrill_dm/message.rb, line 98
def merge_vars
  get_value(:merge_vars)
end
metadata() click to toggle source
# File lib/mandrill_dm/message.rb, line 102
def metadata
  get_value(:metadata)
end
preserve_recipients() click to toggle source
# File lib/mandrill_dm/message.rb, line 106
def preserve_recipients
  nil_true_false?(:preserve_recipients)
end
return_path_domain() click to toggle source
# File lib/mandrill_dm/message.rb, line 110
def return_path_domain
  return_string_value(:return_path_domain)
end
send_at() click to toggle source
# File lib/mandrill_dm/message.rb, line 114
def send_at
  value = get_value(:send_at)
  value ? send_at_formatted_string(value) : nil
end
send_at_formatted_string(obj) click to toggle source

mandrill expects `send_at` in UTC as `YYYY-MM-DD HH:MM:SS`

# File lib/mandrill_dm/message.rb, line 120
def send_at_formatted_string(obj)
  return obj if obj.is_a?(String)

  obj = obj.to_time if obj.is_a?(DateTime)
  return obj.utc.strftime('%Y-%m-%d %H:%M:%S') if obj.is_a?(Time)

  raise ArgumentError, 'send_at should be Time/DateTime or String'
end
signing_domain() click to toggle source
# File lib/mandrill_dm/message.rb, line 129
def signing_domain
  return_string_value(:signing_domain)
end
subaccount() click to toggle source
# File lib/mandrill_dm/message.rb, line 133
def subaccount
  return_string_value(:subaccount)
end
subject() click to toggle source
# File lib/mandrill_dm/message.rb, line 137
def subject
  mail.subject
end
tags() click to toggle source
# File lib/mandrill_dm/message.rb, line 141
def tags
  collect_tags
end
template() click to toggle source
# File lib/mandrill_dm/message.rb, line 70
def template
  return_string_value(:template)
end
template_content() click to toggle source
# File lib/mandrill_dm/message.rb, line 74
def template_content
  get_value(:template_content)
end
text() click to toggle source
# File lib/mandrill_dm/message.rb, line 145
def text
  return mail.text_part.body.decoded if mail.text_part
  return_decoded_body('text/plain')
end
to() click to toggle source
# File lib/mandrill_dm/message.rb, line 150
def to
  combine_address_fields.reject(&:nil?).flatten
end
to_json() click to toggle source
# File lib/mandrill_dm/message.rb, line 174
def to_json # rubocop:disable MethodLength, AbcSize
  json_hash = {
    auto_html: auto_html,
    auto_text: auto_text,
    bcc_address: bcc_address,
    from_email: from_email,
    from_name: from_name,
    global_merge_vars: global_merge_vars,
    headers: headers,
    html: html,
    important: important,
    inline_css: inline_css,
    merge: merge,
    merge_language: merge_language,
    merge_vars: merge_vars,
    metadata: metadata,
    preserve_recipients: preserve_recipients,
    return_path_domain: return_path_domain,
    signing_domain: signing_domain,
    subaccount: subaccount,
    subject: subject,
    tags: tags,
    text: text,
    to: to,
    track_clicks: track_clicks,
    track_opens: track_opens,
    tracking_domain: tracking_domain,
    url_strip_qs: url_strip_qs,
    view_content_link: view_content_link
  }

  json_hash[:attachments] = attachments if attachments?
  json_hash[:images] = images if inline_attachments?
  json_hash
end
track_clicks() click to toggle source
# File lib/mandrill_dm/message.rb, line 154
def track_clicks
  nil_true_false?(:track_clicks)
end
track_opens() click to toggle source
# File lib/mandrill_dm/message.rb, line 158
def track_opens
  nil_true_false?(:track_opens)
end
tracking_domain() click to toggle source
# File lib/mandrill_dm/message.rb, line 162
def tracking_domain
  return_string_value(:tracking_domain)
end
url_strip_qs() click to toggle source
# File lib/mandrill_dm/message.rb, line 166
def url_strip_qs
  nil_true_false?(:url_strip_qs)
end

Private Instance Methods

attachments?() click to toggle source
# File lib/mandrill_dm/message.rb, line 256
def attachments?
  mail.attachments.any? { |a| !a.inline? }
end
collect_tags() click to toggle source

Returns an array of tags

# File lib/mandrill_dm/message.rb, line 213
def collect_tags
  mail[:tags].to_s.split(', ').map { |tag| tag }
end
combine_address_fields() click to toggle source

Returns a single, flattened hash with all to, cc, and bcc addresses

# File lib/mandrill_dm/message.rb, line 218
def combine_address_fields
  %w[to cc bcc].map do |field|
    hash_addresses(mail[field])
  end
end
from() click to toggle source

Returns a Mail::Address object using the from field

# File lib/mandrill_dm/message.rb, line 225
def from
  address = mail[:from].formatted
  Mail::Address.new(address.first)
end
get_value(field) click to toggle source

rubocop:disable Metrics/AbcSize

# File lib/mandrill_dm/message.rb, line 231
def get_value(field)
  if mail[field].respond_to?(:unparsed_value)                     # `mail` gem > 2.7.0
    mail[field].unparsed_value
  elsif mail[field].instance_variable_defined?('@unparsed_value') # `mail` gem = 2.7.0
    mail[field].instance_variable_get('@unparsed_value')
  elsif mail[field].instance_variable_defined?('@value')          # `mail` gem < 2.7.0
    mail[field].instance_variable_get('@value')
  end
end
hash_addresses(address_field) click to toggle source

Returns a Mandrill API compatible email address hash

# File lib/mandrill_dm/message.rb, line 243
def hash_addresses(address_field)
  return nil unless address_field

  address_field.formatted.map do |address|
    address_obj = Mail::Address.new(address)
    {
      email: address_obj.address,
      name: address_obj.display_name,
      type: address_field.name.downcase
    }
  end
end
inline_attachments?() click to toggle source
# File lib/mandrill_dm/message.rb, line 260
def inline_attachments?
  mail.attachments.any?(&:inline?)
end
nil_true_false?(field) click to toggle source
# File lib/mandrill_dm/message.rb, line 272
def nil_true_false?(field)
  return nil if mail[field].nil?
  mail[field].to_s == 'true'
end
return_decoded_body(mime_type) click to toggle source
# File lib/mandrill_dm/message.rb, line 264
def return_decoded_body(mime_type)
  mail.mime_type == mime_type ? mail.body.decoded : nil
end
return_string_value(field) click to toggle source
# File lib/mandrill_dm/message.rb, line 268
def return_string_value(field)
  mail[field] ? mail[field].to_s : nil
end