snails 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/snails/mailer.rb +117 -0
  3. data/snails.gemspec +1 -1
  4. metadata +2 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4400d3577ffe665951a185e806b60dfc17d4df1f
4
- data.tar.gz: 7c3b08235ab641c057492e1b947ea00b6c8e2ea4
3
+ metadata.gz: ee6183d74fa011a7f2cc62352a468cfa5ad948a3
4
+ data.tar.gz: bdff8341f0737d0b5889a9eaf34f8702cbb8b2ac
5
5
  SHA512:
6
- metadata.gz: 427ca357316bc92b3bb1ecdfbeda822e872e0e5c958496c8bcd431c43fe10071c8fb5f080fed5e34a013a6da62f7b2daa5cb6294279a505f81c3f8ebe92c1e9b
7
- data.tar.gz: 803ee94a83c7c8740c0788742f85389ef522a5b6596542e4c26124b9a4fd8d04e67a4c27bd623c8481137800bf3c64cd75f830c23a9b93bb468b01821c3f2b13
6
+ metadata.gz: 356d0950976977e63f2bdbd7e17c938299ba2fa4307ad167b88865a937589e4e68d73ee249479eb8999f70a59ad4d35dd25b6967276b9e251a7d8ca77472b5d7
7
+ data.tar.gz: 414f2cad747f8733b480588ae7988f0a18c39d5e0bb5b9a7320324f4ead771a3501fd8aad8eb7369e94eb70eb512755812f307064498a6c9826c14e748ff1749
@@ -0,0 +1,117 @@
1
+ require 'erb'
2
+ require 'tilt/erb'
3
+ require 'tuktuk'
4
+
5
+ module Snails
6
+
7
+ class Mailer
8
+
9
+ @queue = :emails
10
+
11
+ def initialize(opts)
12
+ cwd = Pathname.new(Dir.pwd)
13
+ mail_config = (opts[:smtp] || opts[:mail]) or raise ":smtp options missing"
14
+
15
+ if key = mail_config.dig(:dkim, :private_key) and File.exist?(key)
16
+ mail_config[:dkim][:private_key] = IO.read(key)
17
+ elsif mail_config[:dkim]
18
+ puts "Private key for DKIM not found! Disabling..."
19
+ mail_config.delete(:dkim)
20
+ end
21
+
22
+ Tuktuk.options = mail_config
23
+
24
+ @from_email = opts[:from] or raise ":from required"
25
+ @base_subject = opts[:base_subject] || ''
26
+ @views = opts[:views] || cwd.join('lib', 'views')
27
+ @logfile = opts[:logfile] || cwd.join('log', 'mailer.log')
28
+ end
29
+
30
+ def email(name, &block)
31
+ define_singleton_method(name) do |*args|
32
+ instance_exec(*args, &block)
33
+ end
34
+ end
35
+
36
+ def perform(method, *args)
37
+ send(method, *args)
38
+ end
39
+
40
+ # e.g. Notifier.queue(:some_notification, @project, "arg1")
41
+ def queue(method, obj, *args)
42
+ return unless Snails.env.production?
43
+ Resque.enqueue(self, method, obj.id, *args)
44
+ end
45
+
46
+ def send_error(to: @from_email, err:, env:, params: {}})
47
+ @exception, @env, @params = err, env, params
48
+ @url = "#{env['REQUEST_METHOD']} #{env['REQUEST_URI']}"
49
+
50
+ subject = "#{@url} :: (#{@exception.class}) \"#{@exception.message}\""
51
+ content = %{
52
+ A <%= @exception.class %> occurred in <%= @url %>:
53
+
54
+ -----------------
55
+
56
+ <%= @exception.message %>
57
+
58
+ <%= @exception.backtrace.join("\n") %>
59
+
60
+ -----------------
61
+
62
+ - Request : <%= @url %>
63
+ - Parameters : <%= @params.inspect %>
64
+ - IP address : <%= @env['REMOTE_ADDR'] %>
65
+ - User agent : <%= @env['HTTP_USER_AGENT'] %>
66
+ - Process : <%= $$ %>
67
+ - Server : <%= `hostname -s`.chomp %>
68
+
69
+ -----------------
70
+ }.strip
71
+
72
+ send_email(to: to, subject: subject, body: content)
73
+ end
74
+
75
+ private
76
+
77
+ def render(view)
78
+ template = File.read(File.join(@views, "#{view}.erb"))
79
+ ERB.new(template).result(binding)
80
+ end
81
+
82
+ def partial(name)
83
+ partial_name = name.to_s["/"] ? name.to_s.reverse.sub("/", "_/").reverse : "_#{name}"
84
+ render partial_name
85
+ end
86
+
87
+ def logger
88
+ @logger ||= Logger.new(@logfile)
89
+ end
90
+
91
+ def send_email(to:, subject:, body:, from: nil, html: nil)
92
+ raise "No recipient given for mail: #{subject}!" if to.blank?
93
+
94
+ message = {
95
+ to: to,
96
+ from: from || @from_email,
97
+ subject: @base_subject + subject,
98
+ body: body.is_a?(Symbol) ? render(body) : body
99
+ }
100
+
101
+ if html
102
+ message[:html_body] = html.is_a?(Symbol) ? render(html) : html
103
+ end
104
+
105
+ logger.info "[#{to}] Delivering: #{subject}"
106
+ resp, email = Tuktuk.deliver(message) if Snails.env.production?
107
+
108
+ if resp.is_a?(Tuktuk::Bounce)
109
+ logger.info "[#{to}] Email bounced! [#{resp.code}] #{resp.message}"
110
+ end
111
+
112
+ return resp, email
113
+ end
114
+
115
+ end
116
+
117
+ end
data/snails.gemspec CHANGED
@@ -3,7 +3,7 @@
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "snails"
6
- s.version = '0.0.6'
6
+ s.version = '0.0.7'
7
7
  s.platform = Gem::Platform::RUBY
8
8
  s.authors = ['Tomás Pollak']
9
9
  s.email = ['tomas@forkhq.com']
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: snails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomás Pollak
@@ -127,6 +127,7 @@ files:
127
127
  - example/Rakefile
128
128
  - example/config.ru
129
129
  - lib/snails.rb
130
+ - lib/snails/mailer.rb
130
131
  - lib/snails/tasks.rb
131
132
  - snails.gemspec
132
133
  homepage: