Module: DMCourier::Services::MessageHelper

Included in:
Mandrill, Sendgrid, Sparkpost
Defined in:
lib/dm_courier/message_helper.rb

Instance Method Summary collapse

Instance Method Details

#attachments(filter = {}) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/dm_courier/message_helper.rb', line 71

def attachments(filter = {})
  Enumerator.new do |y|
    attachments = mail.attachments
    attachments = if filter[:inline]
                    attachments.select(&:inline?)
                  else
                    attachments.reject(&:inline?)
                  end if filter.key?(:inline)

    attachments.map do |attachment|
      y.yield(name: attachment.inline? ? attachment.cid : attachment.filename,
              type: attachment.mime_type,
              content: Base64.encode64(attachment.body.decoded),
              inline: attachment.inline?)
    end
  end
end

#attachments?(filter = {}) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
92
93
94
95
96
97
# File 'lib/dm_courier/message_helper.rb', line 89

def attachments?(filter = {})
  found = mail.attachments && !mail.attachments.empty?

  if found && filter.key?(:inline)
    found &&= mail.attachments.any? { |a| a.inline? == filter[:inline] }
  end

  found
end

#extract_params(params) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/dm_courier/message_helper.rb', line 7

def extract_params(params)
  json = {}

  json
    .merge!(Hash[
      params[:nil_true_false].map { |name, key| [name.to_sym, nil_true_false?(key.to_sym)] }
    ]) if params.key?(:nil_true_false)

  json
    .merge!(Hash[
      params[:string].map { |name, key| [name.to_sym, return_string_value(key.to_sym)] }
    ]) if params.key?(:string)

  json
end

#fallback_options(field) ⇒ Object



67
68
69
# File 'lib/dm_courier/message_helper.rb', line 67

def fallback_options(field)
  mail[field] || options[field]
end

#fromObject



35
36
37
# File 'lib/dm_courier/message_helper.rb', line 35

def from
  Mail::Address.new(from_address)
end

#from_addressObject



39
40
41
# File 'lib/dm_courier/message_helper.rb', line 39

def from_address
  mail[:from] ? mail[:from].formatted.first : options[:from]
end

#from_emailObject



23
24
25
# File 'lib/dm_courier/message_helper.rb', line 23

def from_email
  from.address if from_address
end

#from_nameObject



31
32
33
# File 'lib/dm_courier/message_helper.rb', line 31

def from_name
  from.display_name if from_address
end

#html_partObject



48
49
50
# File 'lib/dm_courier/message_helper.rb', line 48

def html_part
  mail.html_part ? mail.html_part.body.decoded : mail.body.decoded
end

#metadataObject



27
28
29
# File 'lib/dm_courier/message_helper.rb', line 27

def 
  JSON.parse(mail[:metadata].value) if mail[:metadata]
end

#nil_true_false?(field) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
64
65
# File 'lib/dm_courier/message_helper.rb', line 61

def nil_true_false?(field)
  value = fallback_options(field)
  return nil if value.nil?
  value.to_s == "true" ? true : false
end

#return_string_value(field) ⇒ Object



56
57
58
59
# File 'lib/dm_courier/message_helper.rb', line 56

def return_string_value(field)
  value = fallback_options(field)
  value ? value.to_s : nil
end

#subjectObject



52
53
54
# File 'lib/dm_courier/message_helper.rb', line 52

def subject
  mail.subject
end

#text_partObject



43
44
45
46
# File 'lib/dm_courier/message_helper.rb', line 43

def text_part
  return mail.text_part.body.decoded if mail.multipart? && mail.text_part
  nil
end