mail_interceptor 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 519692ac3fb68840234a78cc4868c0305c129bf0
4
- data.tar.gz: 28be53d6876eae2aa09301022ee1162a1c036a88
3
+ metadata.gz: 01f80432a77850310f4d4ae32027ead3bdc951b5
4
+ data.tar.gz: 50e4552dd58951fbae975339350e75d31e0d13fc
5
5
  SHA512:
6
- metadata.gz: 898f3f4f3240815ab68ae7e28d126f4280516fca853acf00107fd112a8ca6aded5eeaf09a06e4811f61895fba3b7b0b47aff2e8cc38063bdf3f742bd2e86b672
7
- data.tar.gz: 24afd80b88edccd7fc1becca837c5248bdd2cf919794f5d51ce897ffe274416ccb4fd49c84ad7e94d15603ba60c5fd69e0609749fdbdedcacce52a3c6c61a942
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
  [![BigBinary logo](http://bigbinary.com/assets/common/logo.png)](http://BigBinary.com)
@@ -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) if production?
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? && !production?
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 = production? ? subject_prefix : "#{subject_prefix} #{env.upcase}"
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
- def production?
65
- env.production?
67
+ class InterceptorEnv
68
+ def name
69
+ Rails.env.upcase
66
70
  end
67
71
 
68
- def env
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
@@ -1,3 +1,3 @@
1
1
  module MailInterceptor
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  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 forward_emails_to: 'test@example.com'
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 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'
21
22
  assert_equal ["@wheel.com"], @interceptor.deliver_emails_to
22
23
 
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']
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 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']
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 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
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 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'
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
- 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'
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 forward_emails_to: '',
73
- subject_prefix: 'wheel'
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 forward_emails_to: [],
80
- subject_prefix: 'wheel'
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 forward_emails_to: [''],
87
- subject_prefix: 'wheel'
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 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'
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.3
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-01-26 00:00:00.000000000 Z
11
+ date: 2015-03-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport