Skip to content

Commit 5b60812

Browse files
Merge pull request #13 from monkbroc/master
Make environment name and intercept mail customizable
2 parents c669001 + 69335cd commit 5b60812

3 files changed

Lines changed: 66 additions & 32 deletions

File tree

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,29 @@ MailInterceptor::Interceptor.new({ forward_emails_to: ['intercepted_emails@bigbi
8787
'qa@bigbinary.com' })
8888
```
8989

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+
90113
#### Brought to you by
91114

92115
[![BigBinary logo](http://bigbinary.com/assets/common/logo.png)](http://BigBinary.com)

lib/mail_interceptor.rb

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44

55
module MailInterceptor
66
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
88

99
def initialize options = {}
1010
@deliver_emails_to = Array.wrap options[:deliver_emails_to]
1111
@subject_prefix = options[:subject_prefix] || ''
1212
@forward_emails_to = options.fetch :forward_emails_to
13+
@env = options.fetch :env, InterceptorEnv.new
1314

1415
add_env_info_to_subject_prefix
1516
sanitize_forward_emails_to
@@ -23,7 +24,7 @@ def delivering_email message
2324
private
2425

2526
def normalize_recipients recipients
26-
return Array.wrap(recipients) if production?
27+
return Array.wrap(recipients) unless env.intercept?
2728

2829
return forward_emails_to if deliver_emails_to.empty?
2930

@@ -45,28 +46,30 @@ def add_subject_prefix message
4546
def sanitize_forward_emails_to
4647
self.forward_emails_to = Array.wrap forward_emails_to
4748

48-
if forward_emails_to_empty? && !production?
49+
if forward_emails_to_empty? && env.intercept?
4950
raise "forward_emails_to should not be empty"
5051
end
5152
end
5253

5354
def add_env_info_to_subject_prefix
5455
return if subject_prefix.blank?
5556

56-
_prefix = production? ? subject_prefix : "#{subject_prefix} #{env.upcase}"
57+
_prefix = env.intercept? ? "#{subject_prefix} #{env.name}" : subject_prefix
5758
self.subject_prefix = "[#{_prefix}]"
5859
end
5960

6061
def forward_emails_to_empty?
6162
Array.wrap(forward_emails_to).reject(&:blank?).empty?
6263
end
64+
end
6365

64-
def production?
65-
env.production?
66+
class InterceptorEnv
67+
def name
68+
Rails.env.upcase
6669
end
6770

68-
def env
69-
Rails.env
71+
def intercept?
72+
!Rails.env.production?
7073
end
7174
end
7275
end

test/mail_interceptor_test.rb

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,42 +9,47 @@ class MailInterceptorTest < Minitest::Test
99

1010
def setup
1111
@message = OpenStruct.new
12-
stub_env_methods('test')
1312
end
1413

1514
def test_normalized_deliver_emails_to
16-
@interceptor = ::MailInterceptor::Interceptor.new forward_emails_to: 'test@example.com'
15+
@interceptor = ::MailInterceptor::Interceptor.new env: env,
16+
forward_emails_to: 'test@example.com'
1717
assert_equal [], @interceptor.deliver_emails_to
1818

19-
@interceptor = ::MailInterceptor::Interceptor.new forward_emails_to: 'test@example.com',
20-
deliver_emails_to: '@wheel.com'
19+
@interceptor = ::MailInterceptor::Interceptor.new env: env,
20+
forward_emails_to: 'test@example.com',
21+
deliver_emails_to: '@wheel.com'
2122
assert_equal ["@wheel.com"], @interceptor.deliver_emails_to
2223

23-
@interceptor = ::MailInterceptor::Interceptor.new forward_emails_to: 'test@example.com',
24-
deliver_emails_to: ['@wheel.com', '@pump.com']
24+
@interceptor = ::MailInterceptor::Interceptor.new env: env,
25+
forward_emails_to: 'test@example.com',
26+
deliver_emails_to: ['@wheel.com', '@pump.com']
2527
assert_equal ["@wheel.com", "@pump.com"], @interceptor.deliver_emails_to
2628
end
2729

2830
def test_invocation_of_regular_expression
29-
interceptor = ::MailInterceptor::Interceptor.new forward_emails_to: 'test@example.com',
30-
deliver_emails_to: ['@wheel.com', '@pump.com', 'john@gmail.com']
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']
3134
@message.to = [ 'a@wheel.com', 'b@wheel.com', 'c@pump.com', 'd@club.com', 'e@gmail.com', 'john@gmail.com', 'sam@gmail.com']
3235
interceptor.delivering_email @message
3336
assert_equal ["a@wheel.com", "b@wheel.com", "c@pump.com", "test@example.com", "john@gmail.com"], @message.to
3437
end
3538

3639
def test_no_subject_prefix_in_test
37-
interceptor = ::MailInterceptor::Interceptor.new forward_emails_to: 'test@example.com',
38-
subject_prefix: nil
40+
interceptor = ::MailInterceptor::Interceptor.new env: env,
41+
forward_emails_to: 'test@example.com',
42+
subject_prefix: nil
3943
@message.subject = 'Forgot password'
4044

4145
interceptor.delivering_email @message
4246
assert_equal "Forgot password", @message.subject
4347
end
4448

4549
def test_subject_prefix_in_test
46-
interceptor = ::MailInterceptor::Interceptor.new forward_emails_to: 'test@example.com',
47-
subject_prefix: 'wheel'
50+
interceptor = ::MailInterceptor::Interceptor.new env: env,
51+
forward_emails_to: 'test@example.com',
52+
subject_prefix: 'wheel'
4853
@message.subject = 'Forgot password'
4954

5055
interceptor.delivering_email @message
@@ -56,9 +61,9 @@ def test_subject_prefix_in_test
5661
end
5762

5863
def test_subject_prefix_in_production
59-
stub_env_methods('production')
60-
interceptor = ::MailInterceptor::Interceptor.new forward_emails_to: 'test@example.com',
61-
subject_prefix: 'wheel'
64+
interceptor = ::MailInterceptor::Interceptor.new env: env('production'),
65+
forward_emails_to: 'test@example.com',
66+
subject_prefix: 'wheel'
6267
@message.subject = 'Forgot password'
6368

6469
interceptor.delivering_email @message
@@ -69,31 +74,34 @@ def test_error_if_forward_emails_to_is_empty
6974
message = "forward_emails_to should not be empty"
7075

7176
exception = assert_raises(RuntimeError) do
72-
::MailInterceptor::Interceptor.new forward_emails_to: '',
73-
subject_prefix: 'wheel'
77+
::MailInterceptor::Interceptor.new env: env,
78+
forward_emails_to: '',
79+
subject_prefix: 'wheel'
7480
end
7581

7682
assert_equal message, exception.message
7783

7884
exception = assert_raises(RuntimeError) do
79-
::MailInterceptor::Interceptor.new forward_emails_to: [],
80-
subject_prefix: 'wheel'
85+
::MailInterceptor::Interceptor.new env: env,
86+
forward_emails_to: [],
87+
subject_prefix: 'wheel'
8188
end
8289

8390
assert_equal message, exception.message
8491

8592
exception = assert_raises(RuntimeError) do
86-
::MailInterceptor::Interceptor.new forward_emails_to: [''],
87-
subject_prefix: 'wheel'
93+
::MailInterceptor::Interceptor.new env: env,
94+
forward_emails_to: [''],
95+
subject_prefix: 'wheel'
8896
end
8997

9098
assert_equal message, exception.message
9199
end
92100

93101
private
94102

95-
def stub_env_methods(env)
96-
::MailInterceptor::Interceptor.any_instance.stubs(:env).returns(env)
97-
::MailInterceptor::Interceptor.any_instance.stubs(:production?).returns(env == 'production')
103+
def env(environment = 'test')
104+
OpenStruct.new :name => environment.upcase,
105+
:intercept? => environment != 'production'
98106
end
99107
end

0 commit comments

Comments
 (0)