Skip to content

Commit 69335cd

Browse files
committed
Use an object to pass in the external environment dependencies
1 parent 6d62baf commit 69335cd

3 files changed

Lines changed: 57 additions & 56 deletions

File tree

README.md

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

90-
### Custom environment name
90+
### Custom environment
9191

9292
If your staging environment is using the same Rails environment as
93-
production, you can pass in the the name of the environment and whether
94-
to intercept mail as options. The default is to use `Rails.env` and
95-
intercept mail in all environments except production.
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.
9696

9797
```ruby
98-
MailInterceptor::Interceptor.new({ env_name: ENV["INSTANCE_NAME"],
99-
intercept_mail?: ENV["INTERCEPT_MAIL"] == '1',
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,
100109
forward_emails_to: ['intercepted_emails@bigbinary.com',
101110
'qa@bigbinary.com' })
102111
```

lib/mail_interceptor.rb

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +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_name = options.fetch :env_name, default_env_name
14-
@intercept_mail = options.fetch :intercept_mail?, default_intercept_mail?
13+
@env = options.fetch :env, InterceptorEnv.new
1514

1615
add_env_info_to_subject_prefix
1716
sanitize_forward_emails_to
@@ -24,13 +23,8 @@ def delivering_email message
2423

2524
private
2625

27-
attr_reader :env_name
28-
def intercept_mail?
29-
@intercept_mail
30-
end
31-
3226
def normalize_recipients recipients
33-
return Array.wrap(recipients) unless intercept_mail?
27+
return Array.wrap(recipients) unless env.intercept?
3428

3529
return forward_emails_to if deliver_emails_to.empty?
3630

@@ -52,28 +46,30 @@ def add_subject_prefix message
5246
def sanitize_forward_emails_to
5347
self.forward_emails_to = Array.wrap forward_emails_to
5448

55-
if forward_emails_to_empty? && intercept_mail?
49+
if forward_emails_to_empty? && env.intercept?
5650
raise "forward_emails_to should not be empty"
5751
end
5852
end
5953

6054
def add_env_info_to_subject_prefix
6155
return if subject_prefix.blank?
6256

63-
_prefix = intercept_mail? ? "#{subject_prefix} #{env_name.upcase}" : subject_prefix
57+
_prefix = env.intercept? ? "#{subject_prefix} #{env.name}" : subject_prefix
6458
self.subject_prefix = "[#{_prefix}]"
6559
end
6660

6761
def forward_emails_to_empty?
6862
Array.wrap(forward_emails_to).reject(&:blank?).empty?
6963
end
64+
end
7065

71-
def default_intercept_mail?
72-
!Rails.env.production?
66+
class InterceptorEnv
67+
def name
68+
Rails.env.upcase
7369
end
7470

75-
def default_env_name
76-
Rails.env
71+
def intercept?
72+
!Rails.env.production?
7773
end
7874
end
7975
end

test/mail_interceptor_test.rb

Lines changed: 31 additions & 35 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
@@ -55,24 +60,12 @@ def test_subject_prefix_in_test
5560
assert_equal "[wheel TEST] Another Forgot password", @message.subject
5661
end
5762

58-
def test_subject_prefix_in_custom_staging
59-
stub_env_methods('production')
60-
interceptor = ::MailInterceptor::Interceptor.new env_name: 'staging',
61-
intercept_mail?: true,
63+
def test_subject_prefix_in_production
64+
interceptor = ::MailInterceptor::Interceptor.new env: env('production'),
6265
forward_emails_to: 'test@example.com',
6366
subject_prefix: 'wheel'
6467
@message.subject = 'Forgot password'
6568

66-
interceptor.delivering_email @message
67-
assert_equal "[wheel STAGING] Forgot password", @message.subject
68-
end
69-
70-
def test_subject_prefix_in_production
71-
stub_env_methods('production')
72-
interceptor = ::MailInterceptor::Interceptor.new forward_emails_to: 'test@example.com',
73-
subject_prefix: 'wheel'
74-
@message.subject = 'Forgot password'
75-
7669
interceptor.delivering_email @message
7770
assert_equal "[wheel] Forgot password", @message.subject
7871
end
@@ -81,31 +74,34 @@ def test_error_if_forward_emails_to_is_empty
8174
message = "forward_emails_to should not be empty"
8275

8376
exception = assert_raises(RuntimeError) do
84-
::MailInterceptor::Interceptor.new forward_emails_to: '',
85-
subject_prefix: 'wheel'
77+
::MailInterceptor::Interceptor.new env: env,
78+
forward_emails_to: '',
79+
subject_prefix: 'wheel'
8680
end
8781

8882
assert_equal message, exception.message
8983

9084
exception = assert_raises(RuntimeError) do
91-
::MailInterceptor::Interceptor.new forward_emails_to: [],
92-
subject_prefix: 'wheel'
85+
::MailInterceptor::Interceptor.new env: env,
86+
forward_emails_to: [],
87+
subject_prefix: 'wheel'
9388
end
9489

9590
assert_equal message, exception.message
9691

9792
exception = assert_raises(RuntimeError) do
98-
::MailInterceptor::Interceptor.new forward_emails_to: [''],
99-
subject_prefix: 'wheel'
93+
::MailInterceptor::Interceptor.new env: env,
94+
forward_emails_to: [''],
95+
subject_prefix: 'wheel'
10096
end
10197

10298
assert_equal message, exception.message
10399
end
104100

105101
private
106102

107-
def stub_env_methods(env)
108-
::MailInterceptor::Interceptor.any_instance.stubs(:default_env_name).returns(env)
109-
::MailInterceptor::Interceptor.any_instance.stubs(:default_intercept_mail?).returns(env != 'production')
103+
def env(environment = 'test')
104+
OpenStruct.new :name => environment.upcase,
105+
:intercept? => environment != 'production'
110106
end
111107
end

0 commit comments

Comments
 (0)