mail_interceptor 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +23 -0
- data/lib/mail_interceptor.rb +14 -8
- data/lib/mail_interceptor/version.rb +1 -1
- data/test/mail_interceptor_test.rb +32 -24
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 01f80432a77850310f4d4ae32027ead3bdc951b5
|
4
|
+
data.tar.gz: 50e4552dd58951fbae975339350e75d31e0d13fc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 606b17ea4496526fe9b9cf0f1f17710a5184218083749770cdc8f1e588995ca0edd03404c4453b8438d95c5a228c2ae3a5fa224fd6cf6144e31d2f9eaf65af84
|
7
|
+
data.tar.gz: 461fb9d049dd049ffb7492a643063e9fa1d4a7ead07fe39f72f4eb036c9aa99d29d61054977eea39cd92c7f6acd342c1b43b69632c725164f03d7ae22e9e49ab
|
data/README.md
CHANGED
@@ -87,6 +87,29 @@ MailInterceptor::Interceptor.new({ forward_emails_to: ['intercepted_emails@bigbi
|
|
87
87
|
'qa@bigbinary.com' })
|
88
88
|
```
|
89
89
|
|
90
|
+
### Custom environment
|
91
|
+
|
92
|
+
If your staging environment is using the same Rails environment as
|
93
|
+
production, you can pass in an object with the name of the environment
|
94
|
+
and whether to intercept mail as an option. The default is to use
|
95
|
+
`Rails.env` and intercept mail in all environments except production.
|
96
|
+
|
97
|
+
```ruby
|
98
|
+
class MyEnv
|
99
|
+
def name
|
100
|
+
ENV["INSTANCE_NAME"]
|
101
|
+
end
|
102
|
+
|
103
|
+
def intercept?
|
104
|
+
ENV["INTERCEPT_MAIL"] == '1'
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
MailInterceptor::Interceptor.new({ env: MyEnv.new,
|
109
|
+
forward_emails_to: ['intercepted_emails@bigbinary.com',
|
110
|
+
'qa@bigbinary.com' })
|
111
|
+
```
|
112
|
+
|
90
113
|
#### Brought to you by
|
91
114
|
|
92
115
|
[](http://BigBinary.com)
|
data/lib/mail_interceptor.rb
CHANGED
@@ -4,12 +4,13 @@ require "mail_interceptor/version"
|
|
4
4
|
|
5
5
|
module MailInterceptor
|
6
6
|
class Interceptor
|
7
|
-
attr_accessor :deliver_emails_to, :forward_emails_to, :subject_prefix
|
7
|
+
attr_accessor :deliver_emails_to, :forward_emails_to, :subject_prefix, :env
|
8
8
|
|
9
9
|
def initialize options = {}
|
10
10
|
@deliver_emails_to = Array.wrap options[:deliver_emails_to]
|
11
11
|
@subject_prefix = options[:subject_prefix] || ''
|
12
12
|
@forward_emails_to = options.fetch :forward_emails_to
|
13
|
+
@env = options.fetch :env, InterceptorEnv.new
|
13
14
|
|
14
15
|
add_env_info_to_subject_prefix
|
15
16
|
sanitize_forward_emails_to
|
@@ -23,7 +24,7 @@ module MailInterceptor
|
|
23
24
|
private
|
24
25
|
|
25
26
|
def normalize_recipients recipients
|
26
|
-
return Array.wrap(recipients)
|
27
|
+
return Array.wrap(recipients) unless env.intercept?
|
27
28
|
|
28
29
|
return forward_emails_to if deliver_emails_to.empty?
|
29
30
|
|
@@ -38,6 +39,7 @@ module MailInterceptor
|
|
38
39
|
|
39
40
|
def add_subject_prefix message
|
40
41
|
return if subject_prefix.blank?
|
42
|
+
return if !env.intercept?
|
41
43
|
|
42
44
|
message.subject = "#{subject_prefix} #{message.subject}"
|
43
45
|
end
|
@@ -45,7 +47,7 @@ module MailInterceptor
|
|
45
47
|
def sanitize_forward_emails_to
|
46
48
|
self.forward_emails_to = Array.wrap forward_emails_to
|
47
49
|
|
48
|
-
if forward_emails_to_empty? &&
|
50
|
+
if forward_emails_to_empty? && env.intercept?
|
49
51
|
raise "forward_emails_to should not be empty"
|
50
52
|
end
|
51
53
|
end
|
@@ -53,20 +55,24 @@ module MailInterceptor
|
|
53
55
|
def add_env_info_to_subject_prefix
|
54
56
|
return if subject_prefix.blank?
|
55
57
|
|
56
|
-
_prefix =
|
58
|
+
_prefix = env.intercept? ? "#{subject_prefix} #{env.name}" : subject_prefix
|
57
59
|
self.subject_prefix = "[#{_prefix}]"
|
58
60
|
end
|
59
61
|
|
60
62
|
def forward_emails_to_empty?
|
61
63
|
Array.wrap(forward_emails_to).reject(&:blank?).empty?
|
62
64
|
end
|
65
|
+
end
|
63
66
|
|
64
|
-
|
65
|
-
|
67
|
+
class InterceptorEnv
|
68
|
+
def name
|
69
|
+
Rails.env.upcase
|
66
70
|
end
|
67
71
|
|
68
|
-
def
|
69
|
-
Rails.env
|
72
|
+
def intercept?
|
73
|
+
return false if Rails.env.test?
|
74
|
+
|
75
|
+
!Rails.env.production?
|
70
76
|
end
|
71
77
|
end
|
72
78
|
end
|
@@ -9,33 +9,37 @@ class MailInterceptorTest < Minitest::Test
|
|
9
9
|
|
10
10
|
def setup
|
11
11
|
@message = OpenStruct.new
|
12
|
-
stub_env_methods('test')
|
13
12
|
end
|
14
13
|
|
15
14
|
def test_normalized_deliver_emails_to
|
16
|
-
@interceptor = ::MailInterceptor::Interceptor.new
|
15
|
+
@interceptor = ::MailInterceptor::Interceptor.new env: env,
|
16
|
+
forward_emails_to: 'test@example.com'
|
17
17
|
assert_equal [], @interceptor.deliver_emails_to
|
18
18
|
|
19
|
-
@interceptor = ::MailInterceptor::Interceptor.new
|
20
|
-
|
19
|
+
@interceptor = ::MailInterceptor::Interceptor.new env: env,
|
20
|
+
forward_emails_to: 'test@example.com',
|
21
|
+
deliver_emails_to: '@wheel.com'
|
21
22
|
assert_equal ["@wheel.com"], @interceptor.deliver_emails_to
|
22
23
|
|
23
|
-
@interceptor = ::MailInterceptor::Interceptor.new
|
24
|
-
|
24
|
+
@interceptor = ::MailInterceptor::Interceptor.new env: env,
|
25
|
+
forward_emails_to: 'test@example.com',
|
26
|
+
deliver_emails_to: ['@wheel.com', '@pump.com']
|
25
27
|
assert_equal ["@wheel.com", "@pump.com"], @interceptor.deliver_emails_to
|
26
28
|
end
|
27
29
|
|
28
30
|
def test_invocation_of_regular_expression
|
29
|
-
interceptor = ::MailInterceptor::Interceptor.new
|
30
|
-
|
31
|
+
interceptor = ::MailInterceptor::Interceptor.new env: env,
|
32
|
+
forward_emails_to: 'test@example.com',
|
33
|
+
deliver_emails_to: ['@wheel.com', '@pump.com', 'john@gmail.com']
|
31
34
|
@message.to = [ 'a@wheel.com', 'b@wheel.com', 'c@pump.com', 'd@club.com', 'e@gmail.com', 'john@gmail.com', 'sam@gmail.com']
|
32
35
|
interceptor.delivering_email @message
|
33
36
|
assert_equal ["a@wheel.com", "b@wheel.com", "c@pump.com", "test@example.com", "john@gmail.com"], @message.to
|
34
37
|
end
|
35
38
|
|
36
39
|
def test_no_subject_prefix_in_test
|
37
|
-
interceptor = ::MailInterceptor::Interceptor.new
|
38
|
-
|
40
|
+
interceptor = ::MailInterceptor::Interceptor.new env: env,
|
41
|
+
forward_emails_to: 'test@example.com',
|
42
|
+
subject_prefix: nil
|
39
43
|
@message.subject = 'Forgot password'
|
40
44
|
|
41
45
|
interceptor.delivering_email @message
|
@@ -43,8 +47,9 @@ class MailInterceptorTest < Minitest::Test
|
|
43
47
|
end
|
44
48
|
|
45
49
|
def test_subject_prefix_in_test
|
46
|
-
interceptor = ::MailInterceptor::Interceptor.new
|
47
|
-
|
50
|
+
interceptor = ::MailInterceptor::Interceptor.new env: env,
|
51
|
+
forward_emails_to: 'test@example.com',
|
52
|
+
subject_prefix: 'wheel'
|
48
53
|
@message.subject = 'Forgot password'
|
49
54
|
|
50
55
|
interceptor.delivering_email @message
|
@@ -56,9 +61,9 @@ class MailInterceptorTest < Minitest::Test
|
|
56
61
|
end
|
57
62
|
|
58
63
|
def test_subject_prefix_in_production
|
59
|
-
|
60
|
-
|
61
|
-
|
64
|
+
interceptor = ::MailInterceptor::Interceptor.new env: env('production'),
|
65
|
+
forward_emails_to: 'test@example.com',
|
66
|
+
subject_prefix: 'wheel'
|
62
67
|
@message.subject = 'Forgot password'
|
63
68
|
|
64
69
|
interceptor.delivering_email @message
|
@@ -69,22 +74,25 @@ class MailInterceptorTest < Minitest::Test
|
|
69
74
|
message = "forward_emails_to should not be empty"
|
70
75
|
|
71
76
|
exception = assert_raises(RuntimeError) do
|
72
|
-
::MailInterceptor::Interceptor.new
|
73
|
-
|
77
|
+
::MailInterceptor::Interceptor.new env: env,
|
78
|
+
forward_emails_to: '',
|
79
|
+
subject_prefix: 'wheel'
|
74
80
|
end
|
75
81
|
|
76
82
|
assert_equal message, exception.message
|
77
83
|
|
78
84
|
exception = assert_raises(RuntimeError) do
|
79
|
-
::MailInterceptor::Interceptor.new
|
80
|
-
|
85
|
+
::MailInterceptor::Interceptor.new env: env,
|
86
|
+
forward_emails_to: [],
|
87
|
+
subject_prefix: 'wheel'
|
81
88
|
end
|
82
89
|
|
83
90
|
assert_equal message, exception.message
|
84
91
|
|
85
92
|
exception = assert_raises(RuntimeError) do
|
86
|
-
::MailInterceptor::Interceptor.new
|
87
|
-
|
93
|
+
::MailInterceptor::Interceptor.new env: env,
|
94
|
+
forward_emails_to: [''],
|
95
|
+
subject_prefix: 'wheel'
|
88
96
|
end
|
89
97
|
|
90
98
|
assert_equal message, exception.message
|
@@ -92,8 +100,8 @@ class MailInterceptorTest < Minitest::Test
|
|
92
100
|
|
93
101
|
private
|
94
102
|
|
95
|
-
def
|
96
|
-
|
97
|
-
|
103
|
+
def env(environment = 'test')
|
104
|
+
OpenStruct.new :name => environment.upcase,
|
105
|
+
:intercept? => environment != 'production'
|
98
106
|
end
|
99
107
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mail_interceptor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Neeraj Singh
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-03-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|