class Griddler::Sendgrid::Adapter

Attributes

params[R]

Public Class Methods

new(params) click to toggle source
# File lib/griddler/sendgrid/adapter.rb, line 4
def initialize(params)
  @params = params
end
normalize_params(params) click to toggle source
# File lib/griddler/sendgrid/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/sendgrid/adapter.rb, line 13
def normalize_params
  params.merge(
    to: recipients(:to).map(&:format),
    cc: recipients(:cc).map(&:format),
    bcc: get_bcc,
    attachments: attachment_files,
    charsets: charsets,
    spam_report: {
      report: params[:spam_report],
      score: params[:spam_score],
    }

  )
end

Private Instance Methods

attachment_count() click to toggle source
# File lib/griddler/sendgrid/adapter.rb, line 64
def attachment_count
  params[:attachments].to_i
end
attachment_filename(index) click to toggle source
# File lib/griddler/sendgrid/adapter.rb, line 78
def attachment_filename(index)
  attachment_info.fetch("attachment#{index + 1}", {})["filename"]
end
attachment_files() click to toggle source
# File lib/griddler/sendgrid/adapter.rb, line 58
def attachment_files
  attachment_count.times.map do |index|
    extract_file_at(index)
  end
end
attachment_info() click to toggle source
# File lib/griddler/sendgrid/adapter.rb, line 82
def attachment_info
  @attachment_info ||= JSON.parse(params.delete("attachment-info") || "{}")
end
bcc_from_envelope() click to toggle source
# File lib/griddler/sendgrid/adapter.rb, line 46
def bcc_from_envelope
  JSON.parse(params[:envelope])["to"] if params[:envelope].present?
end
charsets() click to toggle source
# File lib/griddler/sendgrid/adapter.rb, line 50
def charsets
  return {} unless params[:charsets].present?
  JSON.parse(params[:charsets]).symbolize_keys
rescue JSON::ParserError
  {}
end
extract_file_at(index) click to toggle source
# File lib/griddler/sendgrid/adapter.rb, line 68
def extract_file_at(index)
  filename = attachment_filename(index)

  params.delete("attachment#{index + 1}".to_sym).tap do |file|
    if filename.present?
      file.original_filename = filename
    end
  end
end
get_bcc() click to toggle source
# File lib/griddler/sendgrid/adapter.rb, line 38
def get_bcc
  if bcc = bcc_from_envelope
    bcc - recipients(:to).map(&:address) - recipients(:cc).map(&:address)
  else
    []
  end
end
recipients(key) click to toggle source
# File lib/griddler/sendgrid/adapter.rb, line 32
def recipients(key)
  Mail::AddressList.new(params[key] || '').addresses
rescue Mail::Field::IncompleteParseError
  []
end