class Locomotive::Steam::EmailService

Public Instance Methods

logger() click to toggle source
# File lib/locomotive/steam/services/email_service.rb, line 22
def logger
  Locomotive::Common::Logger
end
send_email(options, context) click to toggle source
# File lib/locomotive/steam/services/email_service.rb, line 10
def send_email(options, context)
  prepare_options(options, context)

  log(options, simulation)

  !simulation ? send_email!(options) : nil
end
send_email!(options) click to toggle source
# File lib/locomotive/steam/services/email_service.rb, line 18
def send_email!(options)
  Pony.mail(options)
end

Private Instance Methods

_read_http_attachment(url) click to toggle source
# File lib/locomotive/steam/services/email_service.rb, line 76
def _read_http_attachment(url)
  begin
    uri = URI(url)
    Net::HTTP.get(uri)
  rescue Exception => e
    logger.error "[SendEmail] Unable to read the '#{url}' url, error: #{e.message}"
    nil
  end
end
build_body(options, context, html = true) click to toggle source
# File lib/locomotive/steam/services/email_service.rb, line 37
def build_body(options, context, html = true)
  key = html || html.nil? ? :html_body : :body

  document = (if handle = options.delete(:page_handle)
    parse_page(handle)
  elsif body = options.delete(:body)
    liquid_parser.parse_string(body)
  else
    raise "[EmailService] the body or page_handle options are missing."
  end)

  options[key] = document.render(context)
end
extract_attachment(options) click to toggle source
# File lib/locomotive/steam/services/email_service.rb, line 59
def extract_attachment(options)
  (options[:attachments] || {}).each do |filename, value|
    options[:attachments][filename] = read_attachment(value)
  end
end
log(options, simulation) click to toggle source
# File lib/locomotive/steam/services/email_service.rb, line 86
def log(options, simulation)
  message = ["[#{simulation ? 'Test' : 'Live'}] Sent email via #{options[:via]} (#{options[:via_options].inspect}):"]
  message << "From:     #{options[:from]}"
  message << "To:       #{options[:to]}"
  message << "Subject:  #{options[:subject]}"
  message << "Attachments:  #{options[:attachments]}"
  message << "-----------"
  message << (options[:body] || options[:html_body]).gsub("\n", "\n\t")
  message << "-----------"

  logger.info message.join("\n") + "\n\n"
end
parse_page(handle) click to toggle source
# File lib/locomotive/steam/services/email_service.rb, line 51
def parse_page(handle)
  if page = page_finder_service.by_handle(handle, false)
    liquid_parser.parse(page) # the liquid parser decorates the page (i18n)
  else
    raise "[EmailService] No page found with the following handle: #{handle}"
  end
end
prepare_options(options, context) click to toggle source
# File lib/locomotive/steam/services/email_service.rb, line 28
def prepare_options(options, context)
  build_body(options.symbolize_keys!, context, options.delete(:html))

  extract_attachment(options)

  options[:via] ||= :smtp
  options[:via_options] ||= options.delete(:smtp).try(:symbolize_keys)
end
read_attachment(value) click to toggle source
# File lib/locomotive/steam/services/email_service.rb, line 65
def read_attachment(value)
  url = case value
  when /^https?:\/\// then value
  when /^\//          then asset_host.compute(value, false)
  else
    nil
  end

  url ? _read_http_attachment(url) : value
end