class Hulaki::Mailer

Constants

TEMPLATE_PATH

Attributes

config[R]
params[R]

Public Class Methods

new(params={}) click to toggle source
# File lib/hulaki/email_handler/mailer.rb, line 8
def initialize(params={})
  @params     = params
  @config     = Hulaki::Config["email"]
end

Public Instance Methods

deliver() click to toggle source
# File lib/hulaki/email_handler/mailer.rb, line 13
def deliver
  # Fixme: validation has no purpose for the time being
  validate(@reciever, config["from"], params[:message])
  configure_defaults
  prepare_email
  @mail.deliver
  "sent"
end

Private Instance Methods

configure_defaults() click to toggle source
# File lib/hulaki/email_handler/mailer.rb, line 49
def configure_defaults
  # instance variables are not available inside the block below; so
  # localizing the variable
  env = @config
  delivery_mode = ENV['mode'] == 'test' ? 'test' : :smtp
  Mail.defaults do
    delivery_method delivery_mode,
                    {
                        :address              => env["address"],
                        :port                 => env["port"],
                        :domain               => env["domain"],
                        :user_name            => env["user_name"],
                        :password             => env["password"],
                        :authentication       => env["authentication"],
                        :enable_starttls_auto => true
                    }
  end
end
prepare_email() click to toggle source
# File lib/hulaki/email_handler/mailer.rb, line 23
def prepare_email
  email_body = params[:message]

  # sender set from command-line will take precedence over that in `config.yml`
  from       = params[:from] || config["from"]
  to         = params[:to]
  subject    = params[:subject]

  if config['use_template'] == true
    content = ERB.new(File.read(File.expand_path(TEMPLATE_PATH))).result(binding)
  else
    content = email_body
  end

  @mail = Mail.new do
    to        to
    from      from
    subject   subject

    html_part do
      content_type 'text/html; charset=UTF-8'
      body          content
    end
  end
end
validate(reciever, sender, message) click to toggle source

returns [Hash] Error

# File lib/hulaki/email_handler/mailer.rb, line 69
def validate(reciever, sender, message)
  validator = Hulaki::EmailValidator.new(from: reciever, to: sender, message: message)
  validator.validates_format && validator.validates_presence
end