Class: Herbes::Email

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/herbes/email.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template_params, options = {}) ⇒ Email

Returns a new instance of Email.



21
22
23
24
25
# 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

Class Method Details

.render(params, options = {}) ⇒ Object



49
50
51
52
53
54
55
56
57
# 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

Instance Method Details

#render_html(template) ⇒ Object



27
28
29
# File 'lib/herbes/email.rb', line 27

def render_html(template)
  ERB.new(template).result(binding)
end

#render_inline_template(path = nil, premailer_options = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# 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) ⇒ Object



9
10
11
# File 'lib/herbes/email.rb', line 9

def render_style(path = Constants::DEFAULT_EMAIL_STYLE_PATH)
  File.read(path)
end

#render_template(path = nil) ⇒ Object



31
32
33
34
# 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) ⇒ Object



13
14
15
16
17
18
19
# 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