mail_interceptor 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1038b93a850773fa998a6d17a784b0c9d2c6a49b
4
+ data.tar.gz: 42edb3a479327add4a8b4f7242567f813263a6c2
5
+ SHA512:
6
+ metadata.gz: 0f72941dc324c3e7c4f2a0a3e22146843e7ae33be4a5926933871d47aa14a98da88f1bc2ab7bc15c167494c2c4d005b681d0806dfa38faa057405d8f59567b20
7
+ data.tar.gz: 669c21d19894c6c1b6a541389b6853c39e791a111157041c01ab466e58d94287cd31ed1407ea85ab483fe924c35310c41ba652524611c9600978594c1e513285
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mail_interceptor.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Neeraj Singh
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # MailInterceptor
2
+
3
+ Intercepts and forwards emails in non production environment.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```
10
+ gem 'mail_interceptor'
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```
16
+ # config/initializer/mail_interceptor.rb
17
+
18
+ interceptor = MailInterceptor::Interceptor.new({ forward_emails_to: 'forward@domain.com',
19
+ deliver_emails_to: ["@wheel.com"],
20
+ subject_prefix: 'WHEEL' })
21
+ ActionMailer::Base.register_interceptor(interceptor)
22
+ ```
23
+
24
+ #### deliver_emails_to
25
+
26
+ Passing __deliver_emails_to__ is optional. If no "deliver_emails_to"
27
+ is passed then all emails will be forwarded.
28
+
29
+ Let's say that you want to actually deliver all emails having the pattern
30
+ "@BigBinary.com" then pass a regular expression like this. Now emails
31
+ like `john@BigBinary.com` will not be intercepted and John will actually
32
+ get an email in non-production environment.
33
+
34
+ ```
35
+ deliver_emails_to: ["@BigBinary.com"]
36
+ ```
37
+
38
+ The regular expression is matched without case sensitive. So you can mix lowercase
39
+ and uppercase and it won't matter.
40
+
41
+ #### subject_prefix
42
+
43
+ __subject_prefix__ is optional. If it is supplied then it is added to
44
+ the front of the subject in non-production environment.
45
+
46
+ ```
47
+ [WHEEL] Forgot password
48
+ [WHEEL STAGING] Forogt password
49
+ ```
50
+
51
+ #### forward_emails_to
52
+
53
+ This is a required field.
54
+
55
+ It can take a single email as string or it can take an array of emails
56
+ in which case emails are forwarded to each of those emails in the array.
57
+
58
+
59
+ #### Brought to you by
60
+
61
+
62
+ ![BigBinary](http://bigbinary.com/assets/common/logo.png)
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rake/testtask'
4
+ require 'rubygems/package_task'
5
+
6
+ task :default => :test
7
+
8
+ Rake::TestTask.new('test') do |t|
9
+ t.pattern = 'test/**/*_test.rb'
10
+ t.warning = true
11
+ t.verbose = true
12
+ end
@@ -0,0 +1,66 @@
1
+ require "mail_interceptor/version"
2
+
3
+ module MailInterceptor
4
+ class Interceptor
5
+ attr_accessor :deliver_emails_to, :forward_emails_to, :subject_prefix
6
+
7
+ def initialize options = {}
8
+ @deliver_emails_to = Array.wrap options[:deliver_emails_to]
9
+ @subject_prefix = options[:subject_prefix] || ''
10
+ @forward_emails_to = options.fetch :forward_emails_to
11
+
12
+ sanitize_forward_emails_to
13
+ end
14
+
15
+ def delivering_email message
16
+ add_env_info_to_subject_prefix
17
+ add_subject_prefix message
18
+ message.to = normalize_recipients(message.to).flatten.uniq
19
+ end
20
+
21
+ private
22
+
23
+ def normalize_recipients recipients
24
+ return forward_emails_to if deliver_emails_to.empty?
25
+
26
+ recipients.map do |recipient|
27
+ if deliver_emails_to.find { |regex| Regexp.new(regex, Regexp::IGNORECASE).match(recipient) }
28
+ recipient
29
+ else
30
+ forward_emails_to
31
+ end
32
+ end
33
+ end
34
+
35
+ def add_subject_prefix message
36
+ return if subject_prefix.blank?
37
+
38
+ message.subject = "#{subject_prefix} #{message.subject}"
39
+ end
40
+
41
+ def sanitize_forward_emails_to
42
+ self.forward_emails_to = Array.wrap forward_emails_to
43
+
44
+ raise "forward_emails_to should not be empty" if forward_emails_to_empty?
45
+ end
46
+
47
+ def add_env_info_to_subject_prefix
48
+ return if subject_prefix.blank?
49
+
50
+ _prefix = production? ? subject_prefix : "#{subject_prefix} #{env.upcase}"
51
+ self.subject_prefix = "[#{_prefix}]"
52
+ end
53
+
54
+ def forward_emails_to_empty?
55
+ Array.wrap(forward_emails_to).reject(&:blank?).empty?
56
+ end
57
+
58
+ def production?
59
+ env.production?
60
+ end
61
+
62
+ def env
63
+ Rails.env
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,3 @@
1
+ module MailInterceptor
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mail_interceptor/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mail_interceptor"
8
+ spec.version = MailInterceptor::VERSION
9
+ spec.authors = ["Neeraj Singh"]
10
+ spec.email = ["neeraj@BigBinary.com"]
11
+ spec.summary = %q{Intercepts and forwards emails in non production environment}
12
+ spec.homepage = "http://github.com/bigbinary/mail_interceptor"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler"
21
+ spec.add_development_dependency "rake"
22
+ spec.add_development_dependency "minitest"
23
+ spec.add_development_dependency "activesupport"
24
+ spec.add_development_dependency "mocha"
25
+ end
@@ -0,0 +1,97 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'minitest/autorun'
4
+ require 'active_support/all'
5
+ require 'ostruct'
6
+ require 'mocha/mini_test'
7
+ require_relative './../lib/mail_interceptor'
8
+
9
+ class MailInterceptorTest < Minitest::Test
10
+
11
+ def setup
12
+ @message = OpenStruct.new
13
+ end
14
+
15
+ def test_normalized_deliver_emails_to
16
+ @interceptor = ::MailInterceptor::Interceptor.new forward_emails_to: 'test@example.com'
17
+ assert_equal [], @interceptor.deliver_emails_to
18
+
19
+ @interceptor = ::MailInterceptor::Interceptor.new forward_emails_to: 'test@example.com',
20
+ deliver_emails_to: '@wheel.com'
21
+ assert_equal ["@wheel.com"], @interceptor.deliver_emails_to
22
+
23
+ @interceptor = ::MailInterceptor::Interceptor.new forward_emails_to: 'test@example.com',
24
+ deliver_emails_to: ['@wheel.com', '@pump.com']
25
+ assert_equal ["@wheel.com", "@pump.com"], @interceptor.deliver_emails_to
26
+ end
27
+
28
+ 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
+ @message.to = [ 'a@wheel.com', 'b@wheel.com', 'c@pump.com', 'd@club.com', 'e@gmail.com', 'john@gmail.com', 'sam@gmail.com']
32
+ interceptor.delivering_email @message
33
+ assert_equal ["a@wheel.com", "b@wheel.com", "c@pump.com", "test@example.com", "john@gmail.com"], @message.to
34
+ end
35
+
36
+ def test_no_subject_prefix_in_test
37
+ interceptor = ::MailInterceptor::Interceptor.new forward_emails_to: 'test@example.com',
38
+ subject_prefix: nil
39
+ @message.subject = 'Forgot password'
40
+ stub_env_methods(interceptor, 'test')
41
+
42
+ interceptor.delivering_email @message
43
+ assert_equal "Forgot password", @message.subject
44
+ end
45
+
46
+ def test_subject_prefix_in_test
47
+ interceptor = ::MailInterceptor::Interceptor.new forward_emails_to: 'test@example.com',
48
+ subject_prefix: 'wheel'
49
+ @message.subject = 'Forgot password'
50
+ stub_env_methods(interceptor, 'test')
51
+
52
+ interceptor.delivering_email @message
53
+ assert_equal "[wheel TEST] Forgot password", @message.subject
54
+ end
55
+
56
+ def test_subject_prefix_in_production
57
+ interceptor = ::MailInterceptor::Interceptor.new forward_emails_to: 'test@example.com',
58
+ subject_prefix: 'wheel'
59
+ @message.subject = 'Forgot password'
60
+ stub_env_methods(interceptor, 'production')
61
+
62
+ interceptor.delivering_email @message
63
+ assert_equal "[wheel] Forgot password", @message.subject
64
+ end
65
+
66
+ def test_error_if_forward_emails_to_is_empty
67
+ message = "forward_emails_to should not be empty"
68
+
69
+ exception = assert_raises(RuntimeError) do
70
+ ::MailInterceptor::Interceptor.new forward_emails_to: '',
71
+ subject_prefix: 'wheel'
72
+ end
73
+
74
+ assert_equal message, exception.message
75
+
76
+ exception = assert_raises(RuntimeError) do
77
+ ::MailInterceptor::Interceptor.new forward_emails_to: [],
78
+ subject_prefix: 'wheel'
79
+ end
80
+
81
+ assert_equal message, exception.message
82
+
83
+ exception = assert_raises(RuntimeError) do
84
+ ::MailInterceptor::Interceptor.new forward_emails_to: [''],
85
+ subject_prefix: 'wheel'
86
+ end
87
+
88
+ assert_equal message, exception.message
89
+ end
90
+
91
+ private
92
+
93
+ def stub_env_methods(interceptor, env)
94
+ interceptor.stubs(:env).returns(env)
95
+ interceptor.stubs(:production?).returns(env == 'production')
96
+ end
97
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mail_interceptor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Neeraj Singh
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activesupport
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: mocha
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description:
84
+ email:
85
+ - neeraj@BigBinary.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - lib/mail_interceptor.rb
96
+ - lib/mail_interceptor/version.rb
97
+ - mail_interceptor.gemspec
98
+ - test/mail_interceptor_test.rb
99
+ homepage: http://github.com/bigbinary/mail_interceptor
100
+ licenses:
101
+ - MIT
102
+ metadata: {}
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ requirements: []
118
+ rubyforge_project:
119
+ rubygems_version: 2.2.2
120
+ signing_key:
121
+ specification_version: 4
122
+ summary: Intercepts and forwards emails in non production environment
123
+ test_files:
124
+ - test/mail_interceptor_test.rb