Skip to content

Commit 6d62baf

Browse files
committed
Make environment name and intercept mail customizable
1 parent c669001 commit 6d62baf

3 files changed

Lines changed: 41 additions & 8 deletions

File tree

README.md

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

90+
### Custom environment name
91+
92+
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.
96+
97+
```ruby
98+
MailInterceptor::Interceptor.new({ env_name: ENV["INSTANCE_NAME"],
99+
intercept_mail?: ENV["INTERCEPT_MAIL"] == '1',
100+
forward_emails_to: ['intercepted_emails@bigbinary.com',
101+
'qa@bigbinary.com' })
102+
```
103+
90104
#### Brought to you by
91105

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

lib/mail_interceptor.rb

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ 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?
1315

1416
add_env_info_to_subject_prefix
1517
sanitize_forward_emails_to
@@ -22,8 +24,13 @@ def delivering_email message
2224

2325
private
2426

27+
attr_reader :env_name
28+
def intercept_mail?
29+
@intercept_mail
30+
end
31+
2532
def normalize_recipients recipients
26-
return Array.wrap(recipients) if production?
33+
return Array.wrap(recipients) unless intercept_mail?
2734

2835
return forward_emails_to if deliver_emails_to.empty?
2936

@@ -45,27 +52,27 @@ def add_subject_prefix message
4552
def sanitize_forward_emails_to
4653
self.forward_emails_to = Array.wrap forward_emails_to
4754

48-
if forward_emails_to_empty? && !production?
55+
if forward_emails_to_empty? && intercept_mail?
4956
raise "forward_emails_to should not be empty"
5057
end
5158
end
5259

5360
def add_env_info_to_subject_prefix
5461
return if subject_prefix.blank?
5562

56-
_prefix = production? ? subject_prefix : "#{subject_prefix} #{env.upcase}"
63+
_prefix = intercept_mail? ? "#{subject_prefix} #{env_name.upcase}" : subject_prefix
5764
self.subject_prefix = "[#{_prefix}]"
5865
end
5966

6067
def forward_emails_to_empty?
6168
Array.wrap(forward_emails_to).reject(&:blank?).empty?
6269
end
6370

64-
def production?
65-
env.production?
71+
def default_intercept_mail?
72+
!Rails.env.production?
6673
end
6774

68-
def env
75+
def default_env_name
6976
Rails.env
7077
end
7178
end

test/mail_interceptor_test.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,18 @@ def test_subject_prefix_in_test
5555
assert_equal "[wheel TEST] Another Forgot password", @message.subject
5656
end
5757

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,
62+
forward_emails_to: 'test@example.com',
63+
subject_prefix: 'wheel'
64+
@message.subject = 'Forgot password'
65+
66+
interceptor.delivering_email @message
67+
assert_equal "[wheel STAGING] Forgot password", @message.subject
68+
end
69+
5870
def test_subject_prefix_in_production
5971
stub_env_methods('production')
6072
interceptor = ::MailInterceptor::Interceptor.new forward_emails_to: 'test@example.com',
@@ -93,7 +105,7 @@ def test_error_if_forward_emails_to_is_empty
93105
private
94106

95107
def stub_env_methods(env)
96-
::MailInterceptor::Interceptor.any_instance.stubs(:env).returns(env)
97-
::MailInterceptor::Interceptor.any_instance.stubs(:production?).returns(env == 'production')
108+
::MailInterceptor::Interceptor.any_instance.stubs(:default_env_name).returns(env)
109+
::MailInterceptor::Interceptor.any_instance.stubs(:default_intercept_mail?).returns(env != 'production')
98110
end
99111
end

0 commit comments

Comments
 (0)