recipient_interceptor 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,38 @@
1
+ require 'mail'
2
+
3
+ class RecipientInterceptor
4
+ def initialize(recipients)
5
+ @recipients = normalize_to_array(recipients)
6
+ end
7
+
8
+ def delivering_email(message)
9
+ add_custom_headers message
10
+ message.to = @recipients
11
+ message.cc = nil
12
+ message.bcc = nil
13
+ end
14
+
15
+ private
16
+
17
+ def normalize_to_array(recipients)
18
+ if recipients.respond_to? :split
19
+ recipients.split ','
20
+ else
21
+ recipients
22
+ end
23
+ end
24
+
25
+ def add_custom_headers(message)
26
+ {
27
+ 'X-Intercepted-To' => message.to,
28
+ 'X-Intercepted-Cc' => message.cc,
29
+ 'X-Intercepted-Bcc' => message.bcc
30
+ }.each do |header, addresses|
31
+ if addresses
32
+ addresses.each do |address|
33
+ message.header = "#{message.header}\n#{header}: #{address}"
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,47 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'recipient_interceptor')
2
+
3
+ describe RecipientInterceptor do
4
+ it 'overrides to/cc/bcc fields, copies original fields to custom headers' do
5
+ Mail.register_interceptor RecipientInterceptor.new('staging@example.com')
6
+
7
+ response = deliver_mail
8
+
9
+ expect(response.to).to eq ['staging@example.com']
10
+ expect(response.cc).to eq []
11
+ expect(response.bcc).to eq []
12
+
13
+ expect(response.header['X-Intercepted-To'].to_s).
14
+ to eq "[original.to@example.com, staging@example.com]"
15
+ expect(response.header['X-Intercepted-Cc'].to_s).
16
+ to eq 'original.cc@example.com'
17
+ expect(response.header['X-Intercepted-Bcc'].to_s).
18
+ to eq 'original.bcc@example.com'
19
+ end
20
+
21
+ it 'accepts an array of recipients' do
22
+ Mail.register_interceptor RecipientInterceptor.new(recipient_array)
23
+
24
+ response = deliver_mail
25
+
26
+ expect(response.to).to eq recipient_array
27
+ end
28
+
29
+ def deliver_mail
30
+ Mail.defaults do
31
+ delivery_method :test
32
+ end
33
+
34
+ mail = Mail.deliver do
35
+ from 'original.from@example.com'
36
+ to 'original.to@example.com'
37
+ cc 'original.cc@example.com'
38
+ bcc 'original.bcc@example.com'
39
+ end
40
+
41
+ mail.deliver!
42
+ end
43
+
44
+ def recipient_array
45
+ ['one@example.com', 'two@example.com']
46
+ end
47
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: recipient_interceptor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Dan Croak
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-28 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: mail
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: ! " Use RecipientInterceptor when you don't want your Ruby program
47
+ to\n accidentally send emails to addresses other than those on a whitelist\n
48
+ \ which you configure. For example, you could use it in your web app's\n staging
49
+ environment.\n"
50
+ email: support@thoughtbot.com
51
+ executables: []
52
+ extensions: []
53
+ extra_rdoc_files: []
54
+ files:
55
+ - lib/recipient_interceptor.rb
56
+ - spec/recipient_interceptor_spec.rb
57
+ homepage: http://github.com/thoughtbot/recipient_interceptor
58
+ licenses: []
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project:
77
+ rubygems_version: 1.8.23
78
+ signing_key:
79
+ specification_version: 3
80
+ summary: Intercept recipients when delivering email with the Mail gem.
81
+ test_files:
82
+ - spec/recipient_interceptor_spec.rb