Class: Snails::Mailer

Inherits:
Object
  • Object
show all
Includes:
SimpleFormat
Defined in:
lib/snails/mailer.rb

Defined Under Namespace

Classes: Backend, MailgunBackend, TestBackend, TuktukBackend

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SimpleFormat

#simple_format, #tag, #tag_attributes

Constructor Details

#initialize(opts) ⇒ Mailer

Returns a new instance of Mailer.



32
33
34
35
36
37
38
39
# File 'lib/snails/mailer.rb', line 32

def initialize(opts)
  @from_email    = opts[:from] or raise ":from required"
  @base_subject  = opts[:base_subject] || ''
  @views         = opts[:views] || Snails.root.join('lib', 'views')
  @logfile       = opts[:logfile] # || Snails.root.join('log', 'mailer.log')

  @backend       = self.class.init_backend(opts[:backend_name], opts[:backend_options])
end

Class Method Details

.backendsObject

class Bounce < StandardError; end class SoftBounce < Bounce; end class HardBounce < Bounce; end



19
20
21
22
23
24
25
# File 'lib/snails/mailer.rb', line 19

def self.backends
  {
    'test_backend' => 'Snails::Mailer::TestBackend',
    'smtp' => 'Snails::Mailer::TuktukBackend',    # requires tuktuk gem
    'mailgun' => 'Snails::Mailer::MailgunBackend' # requires rest-client gem
  }
end

.init_backend(backend_name, opts = {}) ⇒ Object



27
28
29
30
# File 'lib/snails/mailer.rb', line 27

def self.init_backend(backend_name, opts = {})
  backend_name = backend_name.presence || 'test_backend'
  backends[backend_name].constantize.new(opts)
end

Instance Method Details

#email(name, &block) ⇒ Object



41
42
43
44
45
# File 'lib/snails/mailer.rb', line 41

def email(name, &block)
  define_singleton_method(name) do |*args|
    instance_exec(*args, &block)
  end
end

#helpers(&block) ⇒ Object



47
48
49
# File 'lib/snails/mailer.rb', line 47

def helpers(&block)
  instance_eval(&block)
end

#perform(method, *args) ⇒ Object



51
52
53
# File 'lib/snails/mailer.rb', line 51

def perform(method, *args)
  send(method, *args)
end

#queue(method, obj, *args) ⇒ Object

e.g. Notifier.queue(:some_notification, @project, “arg1”)



56
57
58
59
# File 'lib/snails/mailer.rb', line 56

def queue(method, obj, *args)
  return unless Snails.env.production?
  Resque.enqueue(self, method, obj.id, *args)
end

#send_error(to: @from_email, err:, env:, params: {}) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/snails/mailer.rb', line 61

def send_error(to: @from_email, err:, env:, params: {})
  @exception, @env, @params = err, env, params
  @url = "#{env['REQUEST_METHOD']} #{env['REQUEST_URI']}"

  subject = "#{@url} :: (#{@exception.class}) \"#{@exception.message}\""
  content = %{
A <%= @exception.class %> occurred in <%= @url %>:

  -----------------

<%= @exception.message %>

<%= @exception.backtrace.join("\n") %>

  -----------------

- Request    : <%= @url %>
- Parameters : <%= @params.inspect %>
- IP address : <%= @env['REMOTE_ADDR'] %>
- User agent : <%= @env['HTTP_USER_AGENT'] %>
- Process    : <%= $$ %>
- Server     : <%= `hostname -s`.chomp %>

  -----------------
  }.strip

  send_email(to: to, subject: subject, body: content)
end