email_interceptor 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +2 -0
- data/.rspec +3 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +35 -0
- data/LICENSE.txt +22 -0
- data/README.md +46 -0
- data/Rakefile +9 -0
- data/email_interceptor.gemspec +25 -0
- data/lib/email_interceptor.rb +40 -0
- data/lib/email_interceptor/configuration.rb +0 -0
- data/lib/email_interceptor/version.rb +3 -0
- data/spec/lib/email_interceptor_spec.rb +56 -0
- data/spec/spec_helper.rb +14 -0
- metadata +120 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3e335c082ec97b10d48fdfcfac1da05eef48b874
|
4
|
+
data.tar.gz: aea7158992b7728e53d1377619eb132463c49c2d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 78d4bc47ba53ce612fdc37203b6c0414e9e79f4ddbe7600631ef2e7e0beddc10a846bcc614fb00d042f4006416ebc119e0cf7980dfa4d20b8772d7188b203a2e
|
7
|
+
data.tar.gz: 616fc4bc6491f358de34f6a75dc3b0434632be11ff737bae101681e5cb7ad9efef54be21210cb7912d8327e546788f735736cdc0e9cb87d147685af026bfa984
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
email_interceptor (0.0.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.2.5)
|
10
|
+
docile (1.1.3)
|
11
|
+
multi_json (1.8.4)
|
12
|
+
rake (10.1.1)
|
13
|
+
rspec (2.14.1)
|
14
|
+
rspec-core (~> 2.14.0)
|
15
|
+
rspec-expectations (~> 2.14.0)
|
16
|
+
rspec-mocks (~> 2.14.0)
|
17
|
+
rspec-core (2.14.7)
|
18
|
+
rspec-expectations (2.14.5)
|
19
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
20
|
+
rspec-mocks (2.14.5)
|
21
|
+
simplecov (0.8.2)
|
22
|
+
docile (~> 1.1.0)
|
23
|
+
multi_json
|
24
|
+
simplecov-html (~> 0.8.0)
|
25
|
+
simplecov-html (0.8.0)
|
26
|
+
|
27
|
+
PLATFORMS
|
28
|
+
ruby
|
29
|
+
|
30
|
+
DEPENDENCIES
|
31
|
+
bundler (~> 1.3)
|
32
|
+
email_interceptor!
|
33
|
+
rake
|
34
|
+
rspec
|
35
|
+
simplecov
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Your
|
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,46 @@
|
|
1
|
+
# EmailInterceptor
|
2
|
+
|
3
|
+
This is a very simple gem that works as an ActiveMailer email
|
4
|
+
interceptor. It allows you to selectively send some emails (called
|
5
|
+
:internal) while re-writing all other emails with a test account or
|
6
|
+
other email address.
|
7
|
+
|
8
|
+
The intention of this is to prevent real users from being emailed from
|
9
|
+
non-production environments, such as staging.
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
gem 'email_interceptor'
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install email_interceptor
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
To use this in Rails, one can pass it to
|
28
|
+
`ActionMailer.register_interceptor` in an initializer, i.e.
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
email_interceptor = EmailInterceptor.new(:internal_only, {
|
32
|
+
:fake_email_address => 'testdummy@example.com',
|
33
|
+
:internal_recipient_matcher => /@this.can.be.a.regex.com$/,
|
34
|
+
:logger_file => Rails.root.join('/log/mailer.log')
|
35
|
+
})
|
36
|
+
|
37
|
+
ActionMailer::Base.register_interceptor(email_interceptor)
|
38
|
+
```
|
39
|
+
|
40
|
+
## Contributing
|
41
|
+
|
42
|
+
1. Fork it
|
43
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
44
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
45
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
46
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -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 'email_interceptor/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "email_interceptor"
|
8
|
+
spec.version = EmailInterceptor::VERSION
|
9
|
+
spec.authors = ["Renewable Funding"]
|
10
|
+
spec.email = ["dave.miller@renewfund.com", "maher@renewfund.com", "laurie@renewfund.com", "ravi@renewfund.com"]
|
11
|
+
spec.description = %q{A small utility for shunting email to a an email address, depending on settings}
|
12
|
+
spec.summary = %q{Using this, you can rewrite recipient lists to a test email account for all or only external users}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
spec.add_development_dependency "simplecov"
|
25
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'socket'
|
2
|
+
require 'email_interceptor/version'
|
3
|
+
|
4
|
+
class EmailInterceptor
|
5
|
+
def initialize(strategy, opts={})
|
6
|
+
strategies = [:live, :internal_only, :fake]
|
7
|
+
@strategy = strategies.include?(strategy) ? strategy : :fake
|
8
|
+
@fake_email_address = opts[:fake_email_address] || 'fake@example.com'
|
9
|
+
@internal_recipient_matcher = opts[:internal_recipient_matcher]
|
10
|
+
@logger_file = opts[:logger_file]
|
11
|
+
end
|
12
|
+
|
13
|
+
def delivering_email(message)
|
14
|
+
override_mail_to(message)
|
15
|
+
log_delivery(message)
|
16
|
+
end
|
17
|
+
|
18
|
+
def override_mail_to(message)
|
19
|
+
unless @strategy == :live
|
20
|
+
message.to = message.to.map { |t| override_single_recipient(t) }
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def log_delivery(message)
|
25
|
+
return unless @logger_file
|
26
|
+
File.open(@logger_file, 'a') do |f|
|
27
|
+
f.puts "#{Time.now}\n#{message}"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
def override_single_recipient(recipient)
|
33
|
+
case @strategy
|
34
|
+
when :internal_only
|
35
|
+
recipient =~ @internal_recipient_matcher ? recipient : @fake_email_address
|
36
|
+
else
|
37
|
+
@fake_email_address
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
File without changes
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'stringio'
|
2
|
+
require 'ostruct'
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe EmailInterceptor do
|
6
|
+
let(:opts) { {:fake_email_address => 'foo@bar.com', :internal_recipient_matcher => /@renewfund.com$/, :logger_file => '/foo/bar/mail.log' } }
|
7
|
+
subject(:fake_interceptor) { described_class.new(:fake, opts) }
|
8
|
+
subject(:live_interceptor) { described_class.new(:live, opts) }
|
9
|
+
subject(:internal_interceptor) { described_class.new(:internal_only, opts) }
|
10
|
+
subject(:bogus_interceptor) { described_class.new(:bogus, opts) }
|
11
|
+
|
12
|
+
describe '#delivering_email' do
|
13
|
+
it 'overrides mail to and logs delivery' do
|
14
|
+
subject.should_receive(:override_mail_to).with(:message)
|
15
|
+
subject.should_receive(:log_delivery).with(:message)
|
16
|
+
subject.delivering_email(:message)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe '#override_mail_to' do
|
21
|
+
it 'always fake mail recipients when strategy is fake' do
|
22
|
+
message = OpenStruct.new(:to => ['real@email.address', 'real@renewfund.com'])
|
23
|
+
fake_interceptor.override_mail_to(message)
|
24
|
+
expect(message.to).to eq ['foo@bar.com', 'foo@bar.com']
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'only fakes external mail recipients when strategy is internal_only' do
|
28
|
+
message = OpenStruct.new(:to => ['real@email.address', 'real@renewfund.com'])
|
29
|
+
internal_interceptor.override_mail_to(message)
|
30
|
+
expect(message.to).to eq ['foo@bar.com', 'real@renewfund.com']
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'does not modify mail to address if strategy is live' do
|
34
|
+
message = OpenStruct.new(:to => ['real@email.address', 'real@renewfund.com'])
|
35
|
+
live_interceptor.override_mail_to(message)
|
36
|
+
expect(message.to).to eq ['real@email.address', 'real@renewfund.com']
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'fakes the recipient if the strategy is not live, internal_only, or fake' do
|
40
|
+
message = OpenStruct.new(:to => ['real@email.address', 'real@renewfund.com'])
|
41
|
+
bogus_interceptor.override_mail_to(message)
|
42
|
+
expect(message.to).to eq ['foo@bar.com', 'foo@bar.com']
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe '#log_delivery' do
|
47
|
+
it 'adds the message to the mailer log' do
|
48
|
+
io = StringIO.new(dummy_file = '')
|
49
|
+
File.stub(:open).with('/foo/bar/mail.log', 'a').and_yield(io)
|
50
|
+
Time.stub(:now => 'alabaster')
|
51
|
+
subject.log_delivery('nergo nergo')
|
52
|
+
dummy_file.split("\n").should == ["alabaster", "nergo nergo"]
|
53
|
+
Time.unstub(:now)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'simplecov'
|
2
|
+
SimpleCov.start
|
3
|
+
|
4
|
+
# Requires supporting ruby files with custom matchers and macros, etc,
|
5
|
+
# in spec/support/ and its subdirectories.
|
6
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
7
|
+
|
8
|
+
require './lib/email_interceptor'
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
12
|
+
config.run_all_when_everything_filtered = true
|
13
|
+
config.order = 'random'
|
14
|
+
end
|
metadata
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: email_interceptor
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Renewable Funding
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-03-21 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: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
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: rspec
|
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: simplecov
|
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
|
+
description: A small utility for shunting email to a an email address, depending on
|
70
|
+
settings
|
71
|
+
email:
|
72
|
+
- dave.miller@renewfund.com
|
73
|
+
- maher@renewfund.com
|
74
|
+
- laurie@renewfund.com
|
75
|
+
- ravi@renewfund.com
|
76
|
+
executables: []
|
77
|
+
extensions: []
|
78
|
+
extra_rdoc_files: []
|
79
|
+
files:
|
80
|
+
- ".gitignore"
|
81
|
+
- ".rspec"
|
82
|
+
- Gemfile
|
83
|
+
- Gemfile.lock
|
84
|
+
- LICENSE.txt
|
85
|
+
- README.md
|
86
|
+
- Rakefile
|
87
|
+
- email_interceptor.gemspec
|
88
|
+
- lib/email_interceptor.rb
|
89
|
+
- lib/email_interceptor/configuration.rb
|
90
|
+
- lib/email_interceptor/version.rb
|
91
|
+
- spec/lib/email_interceptor_spec.rb
|
92
|
+
- spec/spec_helper.rb
|
93
|
+
homepage: ''
|
94
|
+
licenses:
|
95
|
+
- MIT
|
96
|
+
metadata: {}
|
97
|
+
post_install_message:
|
98
|
+
rdoc_options: []
|
99
|
+
require_paths:
|
100
|
+
- lib
|
101
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
requirements: []
|
112
|
+
rubyforge_project:
|
113
|
+
rubygems_version: 2.2.2
|
114
|
+
signing_key:
|
115
|
+
specification_version: 4
|
116
|
+
summary: Using this, you can rewrite recipient lists to a test email account for all
|
117
|
+
or only external users
|
118
|
+
test_files:
|
119
|
+
- spec/lib/email_interceptor_spec.rb
|
120
|
+
- spec/spec_helper.rb
|