Class: MandrillMailer::ArgFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/mandrill_mailer/arg_formatter.rb

Constant Summary collapse

ACCEPTED_MERGE_LANGUAGES =
['mailchimp', 'handlebars'].freeze

Class Method Summary collapse

Class Method Details

.attachment_args(args) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/mandrill_mailer/arg_formatter.rb', line 7

def self.attachment_args(args)
  return unless args
  args.map do |attachment|
    attachment.symbolize_keys!
    type = attachment[:mimetype] || attachment[:type]
    name = attachment[:filename] || attachment[:name]
    file = attachment[:file] || attachment[:content]
    encoded_file = attachment[:encoded_file] || attachment[:encoded_content]
    next {"type" => type, "name" => name, "content" => encoded_file} if encoded_file
    next {"type" => type, "name" => name, "content" => Base64.encode64(file)}
  end
end

.boolean(arg) ⇒ Object

ensure only true or false is returned given arg



50
51
52
# File 'lib/mandrill_mailer/arg_formatter.rb', line 50

def self.boolean(arg)
  !!arg
end

.format_messages_api_message_data(args, defaults) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/mandrill_mailer/arg_formatter.rb', line 74

def self.format_messages_api_message_data(args, defaults)
  # If a merge_language attribute is given and it's not one of the accepted
  # languages Raise an error
  if args[:merge_language] && !ACCEPTED_MERGE_LANGUAGES.include?(args[:merge_language])
    raise MandrillMailer::CoreMailer::InvalidMergeLanguageError.new("The :merge_language value `#{args[:merge_language]}`is invalid, value must be one of: #{ACCEPTED_MERGE_LANGUAGES.join(', ')}.")
  end

  {
    "html" => args[:html],
    "text" => args[:text],
    "subject" => args[:subject],
    "from_email" => args[:from] || defaults[:from],
    "from_name" => args[:from_name] || defaults[:from_name] || defaults[:from],
    "to" => params(args[:to]),
    "headers" => args[:headers],
    "important" => boolean(args[:important]),
    "track_opens" => args.fetch(:track_opens, true),
    "track_clicks" => boolean(args.fetch(:track_clicks, true)),
    "auto_text" => boolean(args.fetch(:auto_text, true)),
    "auto_html" => boolean(args[:auto_html]),
    "inline_css" => boolean(args[:inline_css]),
    "url_strip_qs" => boolean(args.fetch(:url_strip_qs, true)),
    "preserve_recipients" => boolean(args[:preserve_recipients]),
    "view_content_link" => boolean(args[:view_content_link] || defaults[:view_content_link]),
    "bcc_address" => args[:bcc],
    "tracking_domain" => args[:tracking_domain],
    "signing_domain" => args[:signing_domain],
    "return_path_domain" => args[:return_path_domain],
    "merge" => boolean(args[:merge]),
    "merge_language" => args[:merge_language],
    "global_merge_vars" => mandrill_args(args[:vars] || args[:global_merge_vars] || defaults[:merge_vars]),
    "merge_vars" => merge_vars(args[:recipient_vars] || args[:merge_vars]),
    "tags" => args[:tags],
    "subaccount" => args[:subaccount],
    "google_analytics_domains" => args[:google_analytics_domains],
    "google_analytics_campaign" => args[:google_analytics_campaign],
    "metadata" => args[:metadata],
    "recipient_metadata" => args[:recipient_metadata],
    "attachments" => attachment_args(args[:attachments]),
    "images" => images_args(args[:images])
  }
end

.images_args(args) ⇒ Object



20
21
22
23
# File 'lib/mandrill_mailer/arg_formatter.rb', line 20

def self.images_args(args)
  return unless args
  attachment_args(args)
end

.mandrill_args(args) ⇒ Object

convert a normal hash into the format mandrill needs



26
27
28
29
30
31
# File 'lib/mandrill_mailer/arg_formatter.rb', line 26

def self.mandrill_args(args)
  return [] unless args
  args.stringify_keys.map do |k,v|
    {'name' => k, 'content' => v}
  end
end

.merge_vars(args) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/mandrill_mailer/arg_formatter.rb', line 33

def self.merge_vars(args)
  return [] unless args
  args.map do |item|
    rcpt = item.keys[0]
    {'rcpt' => rcpt, 'vars' => mandrill_args(item.fetch(rcpt))}
  end
end

.params(to_params) ⇒ Object

handle if to params is an array of either hashes or strings or the single string



55
56
57
58
59
60
61
62
63
# File 'lib/mandrill_mailer/arg_formatter.rb', line 55

def self.params(to_params)
  if to_params.kind_of? Array
    to_params.map do |p|
      params_item(p)
    end
  else
    [params_item(to_params)]
  end
end

.params_item(item) ⇒ Object

single to params item



66
67
68
69
70
71
72
# File 'lib/mandrill_mailer/arg_formatter.rb', line 66

def self.params_item(item)
  if item.kind_of? Hash
    item
  else
    {"email" => item, "name" => item}
  end
end

.rcpt_metadata(args) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/mandrill_mailer/arg_formatter.rb', line 41

def self.(args)
  return [] unless args
  args.map do |item|
    rcpt = item.keys[0]
    {'rcpt' => rcpt, 'values' => item.fetch(rcpt)}
  end
end