Class: Snails::Mailer

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

Instance Method Summary collapse

Methods included from SimpleFormat

#simple_format, #tag, #tag_attributes

Constructor Details

#initialize(opts) ⇒ Mailer

Returns a new instance of Mailer.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/snails/mailer.rb', line 15

def initialize(opts)
  mail_config = (opts[:smtp] || opts[:mail]) or raise ":smtp options missing"

  if key = mail_config.dig(:dkim, :private_key) and File.exist?(key)
    mail_config[:dkim][:private_key] = IO.read(key)
  elsif mail_config[:dkim]
    puts "Private key for DKIM not found! Disabling..."
    mail_config.delete(:dkim)
  end

  Tuktuk.options = mail_config
  @debug         = mail_config[:debug]
  @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')
end

Instance Method Details

#email(name, &block) ⇒ Object



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

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

#helpers(&block) ⇒ Object



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

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

#perform(method, *args) ⇒ Object



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

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

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

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



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

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



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

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