Class: Snails::Mailer

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

Defined Under Namespace

Classes: Backend, Mailgun, Tuktuk

Instance Method Summary collapse

Methods included from SimpleFormat

#simple_format, #tag, #tag_attributes

Constructor Details

#initialize(opts) ⇒ Mailer

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



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

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_name   = opts[:backend_name] || 'TestBackend'
  @backend       = self.class.backends[backend_name].new(opts[:backend_options])
end

Instance Method Details

#email(name, &block) ⇒ Object



29
30
31
32
33
# File 'lib/snails/mailer.rb', line 29

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

#helpers(&block) ⇒ Object



35
36
37
# File 'lib/snails/mailer.rb', line 35

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

#perform(method, *args) ⇒ Object



39
40
41
# File 'lib/snails/mailer.rb', line 39

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

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

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



44
45
46
47
# File 'lib/snails/mailer.rb', line 44

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



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/snails/mailer.rb', line 49

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