class Jobs::Email::Send

Public Instance Methods

perform(params) click to toggle source
# File lib/restpack_email_service/jobs/email/send.rb, line 7
def perform(params)
  params.deep_symbolize_keys!

  template = get_template(params)

  if template
    params[:subject] = template.render_subject(params[:data])
    params[:text_body] = template.render_text(params[:data])
    params[:html_body] = template.render_html(params[:data])

    if template.from
      params[:from] ||= template.from
    end

    send_raw.perform(params)
  else
    #TODO: GJ: logging exception
    raise "Invalid template: #{params['template']}"
  end
end

Private Instance Methods

get_template(params) click to toggle source
# File lib/restpack_email_service/jobs/email/send.rb, line 34
def get_template(params)
  application_id = params[:application_id]
  template = resolve_template(application_id, params[:template])
  inject_header_and_footer(template, application_id)

  template
end
load_default_template(identifier, format='html') click to toggle source
# File lib/restpack_email_service/jobs/email/send.rb, line 65
def load_default_template(identifier, format='html')
  filepath = File.expand_path("#{template_path}/#{identifier}.#{format}.liquid", __FILE__)
  if File.exists? filepath
    return File.read(filepath)
  end
end
resolve_template(application_id, identifier) click to toggle source
# File lib/restpack_email_service/jobs/email/send.rb, line 42
def resolve_template(application_id, identifier)
  template = Models::Email::Template.where(
    application_id: application_id,
    identifier: identifier
  ).take

  template ||= Models::Email::Template.new

  if template.subject_template.blank?
    template.subject_template = load_default_template(identifier, 'subject')
  end

  if template.text_template.blank?
    template.text_template = load_default_template(identifier, 'text')
  end

  if template.html_template.blank?
    template.html_template = load_default_template(identifier, 'html')
  end

  template
end
send_raw() click to toggle source
# File lib/restpack_email_service/jobs/email/send.rb, line 30
def send_raw
  Jobs::Email::SendRaw.new
end
template_path() click to toggle source
# File lib/restpack_email_service/jobs/email/send.rb, line 72
def template_path
  '../../../../../templates'
end