class Herbes::Email

Public Class Methods

new(template_params, options = {}) click to toggle source
Calls superclass method
# File lib/herbes/email.rb, line 21
def initialize(template_params, options = {})
  style = resolve_style(options)
  params = template_params.transform_keys(&:to_sym)
  super(params.merge(style: style))
end
render(params, options = {}) click to toggle source
# File lib/herbes/email.rb, line 49
def render(params, options = {})
  path = options[:template_path]
  premailer = options[:premailer] || {}

  # remove unicode space as it fails aws cognito regex
  new(params, options)
    .render_inline_template(path, premailer)
    .gsub("\u00A0", '')
end

Public Instance Methods

render_html(template) click to toggle source
# File lib/herbes/email.rb, line 27
def render_html(template)
  ERB.new(template).result(binding)
end
render_inline_template(path = nil, premailer_options = {}) click to toggle source
# File lib/herbes/email.rb, line 36
def render_inline_template(path = nil, premailer_options = {})
  path ||= Constants::DEFAULT_EMAIL_TEMPLATE_PATH
  Premailer.new(
    render_template(path),
    [
      { warn_level: Premailer::Warnings::SAFE },
      premailer_options,
      { css_string: style, with_html_string: true }
    ].reduce(&:merge)
  ).to_inline_css
end
render_style(path = Constants::DEFAULT_EMAIL_STYLE_PATH) click to toggle source
# File lib/herbes/email.rb, line 9
def render_style(path = Constants::DEFAULT_EMAIL_STYLE_PATH)
  File.read(path)
end
render_template(path = nil) click to toggle source
# File lib/herbes/email.rb, line 31
def render_template(path = nil)
  path ||= Constants::DEFAULT_EMAIL_TEMPLATE_PATH
  render_html(File.read(path))
end
resolve_style(params) click to toggle source
# File lib/herbes/email.rb, line 13
def resolve_style(params)
  s = params[:style_string] ||
      (params.key?(:style_path) ? render_style(params[:style_path]) : nil)
  return s if s && params[:extend_default_style] != true

  [render_style, s].join("\n")
end