class SailthruMailer::Base

Constants

CONFIGURATION_METHODS
DEPRECATED_CONFIGURATION_METHODS
VALID_CONFIGURATION_METHODS

Attributes

template[RW]

we have accessors for

Public Class Methods

new(name, *args) click to toggle source

private initializer

# File lib/sailthru_mailer/base.rb, line 13
def initialize(name, *args)
  self.template = name
  self.send(name, *args)
end

Protected Class Methods

action_defined?(m) click to toggle source
# File lib/sailthru_mailer/base.rb, line 151
def action_defined?(m)
  self.public_instance_methods.map(&:to_sym).include?(m.to_sym)
end
connection(reload = false) click to toggle source
# File lib/sailthru_mailer/base.rb, line 111
def connection(reload = false)
  @connection = nil if reload
  @connection ||= SailthruMailer::Connection.new
end
defaults(vals = nil, &block) click to toggle source

just here to give us nicer syntax

# File lib/sailthru_mailer/base.rb, line 138
def defaults(vals = nil, &block)
  return instance_exec(&block) if block_given?
  vals.each_pair{|k,v| self.send(k, v)}
end
inherited(klass) click to toggle source

called when someone inherits from us

# File lib/sailthru_mailer/base.rb, line 105
def inherited(klass)
  VALID_CONFIGURATION_METHODS.each do |m|
    klass.send(m, self.send(m))
  end
end
method_missing(m, *args, &block) click to toggle source

if this is a valid public instance method, we proceed

Calls superclass method
# File lib/sailthru_mailer/base.rb, line 143
def method_missing(m, *args, &block)
  if self.action_defined?(m)
    return new(m, *args)
  else
    return super
  end
end

Public Instance Methods

body(val = nil)

we want to actually call it body for backwards compatibility

Alias for: vars
body=(val)
Alias for: vars=
deliver() click to toggle source

send the mail

# File lib/sailthru_mailer/base.rb, line 54
def deliver
  # handle test mode
  return SailthruMailer.deliveries << self if SailthruMailer.test
  # response = sailthru.send(template_name, email, vars, options, schedule_time)
  self.class.connection.deliver(
    self.template, 
    self.all_recipients,
    self.formatted_vars,
    self.formatted_options,
    (self.date || Time.now).utc.to_s
  )
end
formatted_vars() click to toggle source

formatted variable hash, ready for JSON encoding

# File lib/sailthru_mailer/base.rb, line 67
def formatted_vars
  {}.tap do |ret|
    self.vars.each_pair do |k,v|
      ret[k] = self.prep_for_json(v)
    end
  end
end
vars(val = nil) click to toggle source

variables for the template

# File lib/sailthru_mailer/base.rb, line 42
def vars(val = nil)
  @vars = val unless val.nil?
  @vars ||= {}
end
Also aliased as: body
vars=(val) click to toggle source
# File lib/sailthru_mailer/base.rb, line 46
def vars=(val)
  vars(val)
end
Also aliased as: body=

Protected Instance Methods

all_recipients() click to toggle source

list of all email addresses emails can be specified as follows

to ["email1@test.com", "email2@test.com"]
to "email1@test.com, email2@test.com; email3@test.com"
to ["email1@test.com; email2@test.com"]
# File lib/sailthru_mailer/base.rb, line 99
def all_recipients
  [:to, :cc, :bcc].collect{|type| Array.wrap(self.send(type)).collect{|email| email.split(/;,/)}}.flatten.uniq.join(", ")
end
formatted_options() click to toggle source

get the options for this send

# File lib/sailthru_mailer/base.rb, line 88
def formatted_options
  {}.tap do |ret|
    ret[:replyto] = self.reply_to if self.reply_to.present?
    ret[:behalf_email] = self.from if self.from.present?
  end
end
prep_for_json(val) click to toggle source

prepare a value to be converted to JSON

# File lib/sailthru_mailer/base.rb, line 76
def prep_for_json(val)
  val = val.collect{|v| self.prep_for_json(v)} if val.is_a?(Array)
  # recursive call for hashes
  if val.is_a?(Hash)
    val.each_pair{|k,v| val[k] = self.prep_for_json(v)} 
  # otherwise try to convert to a hash (e.g. ActiveModel)
  elsif val.respond_to?(:to_hash)
    val = self.prep_for_json(val.to_hash)
  end
  val
end