class MandrillBatchMailer::BaseMailer

Attributes

caller_method_name[RW]

Public Class Methods

method_missing(method, *args) click to toggle source

do it just as ActionMailer

Calls superclass method
# File lib/mandrill_batch_mailer/base_mailer.rb, line 40
def method_missing(method, *args)
  mailer = new method
  if mailer.respond_to? method
    mailer.public_send(method, *args)
  else
    super method, *args
  end
end
new(method) click to toggle source
# File lib/mandrill_batch_mailer/base_mailer.rb, line 14
def initialize(method)
  self.caller_method_name = method
end

Public Instance Methods

from_email() click to toggle source

feel free to override

# File lib/mandrill_batch_mailer/base_mailer.rb, line 24
def from_email
  MandrillBatchMailer.from_email
end
from_name() click to toggle source

feel free to override

# File lib/mandrill_batch_mailer/base_mailer.rb, line 29
def from_name
  MandrillBatchMailer.from_name || ''
end
mail(to: nil) click to toggle source
# File lib/mandrill_batch_mailer/base_mailer.rb, line 18
def mail(to: nil)
  @tos = tos_from(to)
  send_template mandrill_parameters
end
tags() click to toggle source

feel free to override

# File lib/mandrill_batch_mailer/base_mailer.rb, line 34
def tags
  ["#{template_name}"]
end

Protected Instance Methods

scope() click to toggle source
# File lib/mandrill_batch_mailer/base_mailer.rb, line 52
def scope
  "#{class_name.underscore.gsub('/', '.')}.#{caller_method_name}"
end

Private Instance Methods

_default() click to toggle source

rubocop:enable MethodLength

# File lib/mandrill_batch_mailer/base_mailer.rb, line 89
def _default
  given_defaults = (respond_to?(:default, true) && default) || {}
  if MandrillBatchMailer.intercept_recipients
    given_defaults[:message].try(:delete, :to)
  end
  given_defaults
end
class_name() click to toggle source
# File lib/mandrill_batch_mailer/base_mailer.rb, line 177
def class_name
  self.class.to_s
end
fallback_translations(scope) click to toggle source
# File lib/mandrill_batch_mailer/base_mailer.rb, line 165
def fallback_translations(scope)
  I18n.fallbacks[I18n.locale].map do |fallback_locale|
    I18n.t scope, locale: fallback_locale
  end.reduce do |orig_translations, fallback_translations|
    if fallback_translations.is_a? Hash
      fallback_translations.deep_merge orig_translations
    else
      orig_translations
    end
  end
end
global_merge_vars() click to toggle source
# File lib/mandrill_batch_mailer/base_mailer.rb, line 117
def global_merge_vars
  merge_vars_from(translations.merge(shared_translations))
end
intercepted_merge_vars(to_email, vars, i) click to toggle source
# File lib/mandrill_batch_mailer/base_mailer.rb, line 132
def intercepted_merge_vars(to_email, vars, i)
  subject = vars[:subject] || translations[:subject]
  {
    rcpt: MandrillBatchMailer.interception_base_mail.sub('@', "+#{i}@"),
    vars: merge_vars_from(vars.merge(subject: "#{to_email} #{subject}"))
  }
end
log_sending(params) click to toggle source
# File lib/mandrill_batch_mailer/base_mailer.rb, line 207
def log_sending(params)
  params_inspect =
    if defined? AwesomePrint
      params.ai
    else
      params.inspect
    end
  MandrillBatchMailer.logger
    .info "Sending Mandrill Mail: #{params_inspect}"
end
mandrill_parameters() click to toggle source

rubocop:disable MethodLength

# File lib/mandrill_batch_mailer/base_mailer.rb, line 59
def mandrill_parameters
  {
    key: MandrillBatchMailer.api_key,
    template_name: template_name,
    template_content: [],
    message: {
      subject: subject,
      from_email: from_email,
      from_name: from_name,
      to: to,
      important: false,
      track_opens: nil,
      track_clicks: nil,
      inline_css: true,
      url_strip_qs: nil,
      preserve_recipients: false,
      view_content_link: nil,
      tracking_domain: nil,
      signing_domain: nil,
      return_path_domain: nil,
      merge: true,
      global_merge_vars: global_merge_vars,
      merge_vars: merge_vars,
      tags: tags
    },
    async: true
  }.deep_merge(_default)
end
merge_vars() click to toggle source
# File lib/mandrill_batch_mailer/base_mailer.rb, line 121
def merge_vars
  @tos.each_with_index.map do |to, i|
    to_email, vars = to.to_a
    if MandrillBatchMailer.intercept_recipients
      intercepted_merge_vars(to_email, vars, i)
    else
      { rcpt: to_email,  vars: merge_vars_from(vars) }
    end
  end
end
merge_vars_from(translations) click to toggle source
# File lib/mandrill_batch_mailer/base_mailer.rb, line 191
def merge_vars_from(translations)
  translations.map do |key, translation|
    { name: key, content: translation }
  end
end
rest_client_send(params) click to toggle source
# File lib/mandrill_batch_mailer/base_mailer.rb, line 218
def rest_client_send(params)
  RestClient::Request.execute(
    method: :post,
    url: MandrillBatchMailer::ENDPOINT,
    payload: params.to_json,
    read_timeout: MandrillBatchMailer.read_timeout,
    open_timeout: MandrillBatchMailer.open_timeout)
end
send_template(params) click to toggle source
# File lib/mandrill_batch_mailer/base_mailer.rb, line 197
def send_template(params)
  if MandrillBatchMailer.perform_deliveries
    rest_client_send params
    params
  else
    log_sending(params)
    params
  end
end
shared_translations() click to toggle source
# File lib/mandrill_batch_mailer/base_mailer.rb, line 181
def shared_translations
  shared_scope =
    "#{class_name.deconstantize.underscore}.shared_translations"
  if I18n.methods.include? :fallbacks
    fallback_translations shared_scope
  else
    I18n.t shared_scope
  end
end
subject() click to toggle source
# File lib/mandrill_batch_mailer/base_mailer.rb, line 102
def subject
  '*|subject|*'
end
template_name() click to toggle source
# File lib/mandrill_batch_mailer/base_mailer.rb, line 97
def template_name
  "#{class_name.underscore}_#{caller_method_name}".split('/').last
    .gsub '_', '-'
end
to() click to toggle source
# File lib/mandrill_batch_mailer/base_mailer.rb, line 106
def to
  if MandrillBatchMailer.intercept_recipients
    @tos.keys.size.times.map do |i|
      { email: MandrillBatchMailer
          .interception_base_mail.sub('@', "+#{i}@") }
    end
  else
    @tos.keys.map { |to_email| { email: to_email } }
  end
end
tos_from(to) click to toggle source

@return [Hash]

p.e. { 'some@mail.ch' => { a_variable: 'Hello' } }
# File lib/mandrill_batch_mailer/base_mailer.rb, line 144
def tos_from(to)
  case to
  when String
    { to => {} }
  when Array
    to.map { |single_to| [single_to, {}] }.to_h
  when Hash
    to
  else
    to.to_h
  end
end
translations() click to toggle source
# File lib/mandrill_batch_mailer/base_mailer.rb, line 157
def translations
  if I18n.methods.include? :fallbacks
    fallback_translations scope
  else
    I18n.t scope
  end
end