Class: Dreamcatcher::Mailer

Inherits:
Object
  • Object
show all
Defined in:
lib/dreamcatcher/mailer.rb

Defined Under Namespace

Classes: EmailContext

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ Mailer

Returns a new instance of Mailer.



9
10
11
# File 'lib/dreamcatcher/mailer.rb', line 9

def initialize(configuration)
  @configuration = configuration
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



7
8
9
# File 'lib/dreamcatcher/mailer.rb', line 7

def configuration
  @configuration
end

Instance Method Details

#build_email_options(context) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/dreamcatcher/mailer.rb', line 22

def build_email_options(context)

  options = {
    :to          => configuration.evaluate(:to, context),
    :from        => configuration.evaluate(:from, context),
    :subject     => configuration.evaluate(:subject, context),
    :via         => configuration.evaluate(:via, context),
    :via_options => configuration.evaluate(:via_options, context)
  }

  options[:body]      = render_body(:text, context, options)
  options[:html_body] = render_body(:html, context, options)

  options.delete_if { |k, v| v.nil? }
  raise "E-mail has no to recipient!" unless options[:to]
  raise "E-mail has no body!" unless options[:body] || options[:html_body]

  options
end

#erb_template_file(dir, name, type) ⇒ Object



66
67
68
# File 'lib/dreamcatcher/mailer.rb', line 66

def erb_template_file(dir, name, type)
  File.join(dir, "#{name}.#{type}.erb")
end

#handle_exception(context) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/dreamcatcher/mailer.rb', line 13

def handle_exception(context)
  email_options = build_email_options(context)
  if configuration.evaluate(:deliver, context)
    Pony.mail(email_options)
  else
    # TODO: print / log email somewhere
  end
end

#render_body(type, context, options) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/dreamcatcher/mailer.rb', line 42

def render_body(type, context, options)
  template_dir  = configuration.evaluate(:template_dir, context)
  template      = configuration.evaluate(:template, context)
  template_file = erb_template_file(template_dir, template, type)
  email_context = EmailContext.new(context, options)

  if File.exists?(template_file)
    layout_file = erb_template_file(template_dir, 'layout', type)
    if File.exists?(layout_file)
      render_erb_template(layout_file, email_context) do
        render_erb_template(template_file, email_context) 
      end
    else
      render_erb_template(template_file, email_context) 
    end
  else
    nil
  end
end

#render_erb_template(template_file, context, &block) ⇒ Object



62
63
64
# File 'lib/dreamcatcher/mailer.rb', line 62

def render_erb_template(template_file, context, &block)
  ERB.new(File.read(template_file), nil, '<>').result(context.get_binding(&block))
end