class Griddler::Mandrill::Adapter

Attributes

params[R]

Public Class Methods

new(params) click to toggle source
# File lib/griddler/mandrill/adapter.rb, line 4
def initialize(params)
  @params = params
end
normalize_params(params) click to toggle source
# File lib/griddler/mandrill/adapter.rb, line 8
def self.normalize_params(params)
  adapter = new(params)
  adapter.normalize_params
end

Public Instance Methods

normalize_params() click to toggle source
# File lib/griddler/mandrill/adapter.rb, line 13
def normalize_params
  events.select do |event|
    event[:spf].present? && (event[:spf][:result] == 'pass' || event[:spf][:result] == 'neutral')
  end.map do |event|
    {
      to: recipients(:to, event),
      cc: recipients(:cc, event),
      bcc: resolve_bcc(event),
      headers: event[:headers],
      from: full_email([ event[:from_email], event[:from_name] ]),
      subject: event[:subject],
      text: event[:text] || '',
      html: event[:html] || '',
      raw_body: event[:raw_msg],
      attachments: attachment_files(event),
      email: event[:email] # the email address where Mandrill received the message
    }
  end
end

Private Instance Methods

attachment_files(event) click to toggle source
# File lib/griddler/mandrill/adapter.rb, line 62
def attachment_files(event)
  files(event, :attachments) + files(event, :images)
end
create_tempfile(attachment) click to toggle source
# File lib/griddler/mandrill/adapter.rb, line 80
def create_tempfile(attachment)
  filename = attachment[:name].gsub(/\/|\\/, '_')
  tempfile = Tempfile.new(filename, Dir::tmpdir, encoding: 'ascii-8bit')
  content = attachment[:content]
  content = Base64.decode64(content) if attachment[:base64]
  tempfile.write(content)
  tempfile.rewind
  tempfile
end
events() click to toggle source
# File lib/griddler/mandrill/adapter.rb, line 37
def events
  @events ||= ActiveSupport::JSON.decode(params[:mandrill_events]).map { |event|
    event['msg'].with_indifferent_access if event['event'] == 'inbound'
  }.compact
end
files(event, key) click to toggle source
# File lib/griddler/mandrill/adapter.rb, line 66
def files(event, key)
  files = event[key] || Hash.new

  files.map do |key, file|
    file[:base64] = true if !file.has_key?(:base64)

    ActionDispatch::Http::UploadedFile.new({
      filename: file[:name],
      type: file[:type],
      tempfile: create_tempfile(file)
    })
  end
end
full_email(contact_info) click to toggle source
# File lib/griddler/mandrill/adapter.rb, line 53
def full_email(contact_info)
  email = contact_info[0]
  if contact_info[1]
    "#{contact_info[1]} <#{email}>"
  else
    email
  end
end
recipients(field, event) click to toggle source
# File lib/griddler/mandrill/adapter.rb, line 43
def recipients(field, event)
  Array.wrap(event[field]).map { |recipient| full_email(recipient) }
end
resolve_bcc(event) click to toggle source
# File lib/griddler/mandrill/adapter.rb, line 47
def resolve_bcc(event)
  email = event[:email]
  to_and_cc = (event[:to].to_a + event[:cc].to_a).compact.map(&:first)
  to_and_cc.include?(email) ? [] : [full_email([email, email.split("@")[0]])]
end