Class: MandrillDm::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/mandrill_dm/message.rb

Overview

rubocop:disable ClassLength

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mail) ⇒ Message

Returns a new instance of Message.



7
8
9
# File 'lib/mandrill_dm/message.rb', line 7

def initialize(mail)
  @mail = mail
end

Instance Attribute Details

#mailObject (readonly)

Returns the value of attribute mail.



5
6
7
# File 'lib/mandrill_dm/message.rb', line 5

def mail
  @mail
end

Instance Method Details

#attachmentsObject

Returns a Mandrill API compatible attachment hash



12
13
14
15
16
17
18
19
20
21
# 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_htmlObject



39
40
41
# File 'lib/mandrill_dm/message.rb', line 39

def auto_html
  nil_true_false?(:auto_html)
end

#auto_textObject



35
36
37
# File 'lib/mandrill_dm/message.rb', line 35

def auto_text
  nil_true_false?(:auto_text)
end

#bcc_addressObject



43
44
45
# File 'lib/mandrill_dm/message.rb', line 43

def bcc_address
  return_string_value(:bcc_address)
end

#from_emailObject



47
48
49
# File 'lib/mandrill_dm/message.rb', line 47

def from_email
  from.address
end

#from_nameObject



51
52
53
# File 'lib/mandrill_dm/message.rb', line 51

def from_name
  from.display_name
end

#global_merge_varsObject



55
56
57
# File 'lib/mandrill_dm/message.rb', line 55

def global_merge_vars
  get_value(:global_merge_vars)
end

#headersObject



59
60
61
62
63
# 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

#htmlObject



65
66
67
68
# 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

#imagesObject

Mandrill uses a different hash for inlined image attachments



24
25
26
27
28
29
30
31
32
33
# 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

#importantObject



78
79
80
# File 'lib/mandrill_dm/message.rb', line 78

def important
  mail[:important].to_s == 'true'
end

#inline_cssObject



82
83
84
# File 'lib/mandrill_dm/message.rb', line 82

def inline_css
  nil_true_false?(:inline_css)
end

#ip_poolObject



86
87
88
# File 'lib/mandrill_dm/message.rb', line 86

def ip_pool
  return_string_value(:ip_pool)
end

#mergeObject



90
91
92
# File 'lib/mandrill_dm/message.rb', line 90

def merge
  nil_true_false?(:merge)
end

#merge_languageObject



94
95
96
# File 'lib/mandrill_dm/message.rb', line 94

def merge_language
  return_string_value(:merge_language)
end

#merge_varsObject



98
99
100
# File 'lib/mandrill_dm/message.rb', line 98

def merge_vars
  get_value(:merge_vars)
end

#metadataObject



102
103
104
# File 'lib/mandrill_dm/message.rb', line 102

def 
  get_value(:metadata)
end

#preserve_recipientsObject



106
107
108
# File 'lib/mandrill_dm/message.rb', line 106

def preserve_recipients
  nil_true_false?(:preserve_recipients)
end

#return_path_domainObject



110
111
112
# File 'lib/mandrill_dm/message.rb', line 110

def return_path_domain
  return_string_value(:return_path_domain)
end

#send_atObject



114
115
116
117
# 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) ⇒ Object

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

Raises:

  • (ArgumentError)


120
121
122
123
124
125
126
127
# 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_domainObject



129
130
131
# File 'lib/mandrill_dm/message.rb', line 129

def signing_domain
  return_string_value(:signing_domain)
end

#subaccountObject



133
134
135
# File 'lib/mandrill_dm/message.rb', line 133

def subaccount
  return_string_value(:subaccount)
end

#subjectObject



137
138
139
# File 'lib/mandrill_dm/message.rb', line 137

def subject
  mail.subject
end

#tagsObject



141
142
143
# File 'lib/mandrill_dm/message.rb', line 141

def tags
  collect_tags
end

#templateObject



70
71
72
# File 'lib/mandrill_dm/message.rb', line 70

def template
  return_string_value(:template)
end

#template_contentObject



74
75
76
# File 'lib/mandrill_dm/message.rb', line 74

def template_content
  get_value(:template_content)
end

#textObject



145
146
147
148
# 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

#toObject



150
151
152
# File 'lib/mandrill_dm/message.rb', line 150

def to
  combine_address_fields.reject(&:nil?).flatten
end

#to_jsonObject

rubocop:disable MethodLength, AbcSize



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# 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: ,
    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_clicksObject



154
155
156
# File 'lib/mandrill_dm/message.rb', line 154

def track_clicks
  nil_true_false?(:track_clicks)
end

#track_opensObject



158
159
160
# File 'lib/mandrill_dm/message.rb', line 158

def track_opens
  nil_true_false?(:track_opens)
end

#tracking_domainObject



162
163
164
# File 'lib/mandrill_dm/message.rb', line 162

def tracking_domain
  return_string_value(:tracking_domain)
end

#url_strip_qsObject



166
167
168
# File 'lib/mandrill_dm/message.rb', line 166

def url_strip_qs
  nil_true_false?(:url_strip_qs)
end


170
171
172
# File 'lib/mandrill_dm/message.rb', line 170

def view_content_link
  nil_true_false?(:view_content_link)
end