email_sanitizer 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1 @@
1
+ MIT
@@ -0,0 +1 @@
1
+ TODO
@@ -0,0 +1,36 @@
1
+ module EmailSanitizer
2
+ class << self
3
+ attr_accessor :base_email
4
+ def base_email
5
+ @base_email ||= "default@example.com"
6
+ end
7
+
8
+ def base_local
9
+ base_email.split('@').first
10
+ end
11
+
12
+ def base_domain
13
+ base_email.split('@').last
14
+ end
15
+
16
+ def sanitize(email_addresses)
17
+ email_addresses = normalize_email_addresses(email_addresses)
18
+ email_addresses.inject([]) { |arry, address| arry << do_sanitize(address) }
19
+ end
20
+
21
+ private
22
+
23
+ def normalize_email_addresses(email_addresses)
24
+ return email_addresses.split(/[,;\s]+/) if email_addresses.class == String
25
+ email_addresses
26
+ end
27
+
28
+ def do_sanitize(email_address)
29
+ local, domain = email_address.split('@')
30
+ "#{base_local}+#{local}_at_#{domain}@#{base_domain}"
31
+ end
32
+ end
33
+ end
34
+
35
+ require 'email_sanitizer/interceptor'
36
+ Mail.register_interceptor(EmailSanitizer::Interceptor)
@@ -0,0 +1,7 @@
1
+ module EmailSanitizer
2
+ class Interceptor
3
+ def self.delivering_email(email)
4
+ email.to = EmailSanitizer.sanitize(email.to)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,69 @@
1
+ require 'spec_helper'
2
+
3
+ describe "When required" do
4
+ it "should register itself through Mail.register_interceptor" do
5
+ require 'email_sanitizer'
6
+ Mail.class_variable_get(:@@delivery_interceptors).should include(EmailSanitizer::Interceptor)
7
+ end
8
+ end
9
+
10
+ require 'email_sanitizer'
11
+
12
+ describe EmailSanitizer do
13
+
14
+ describe "#base_email" do
15
+ it "should default to default@example.com" do
16
+ EmailSanitizer.base_email.should == "default@example.com"
17
+ end
18
+
19
+ it "should be changeable" do
20
+ EmailSanitizer.base_email = "test@example.com"
21
+ EmailSanitizer.base_email.should == "test@example.com"
22
+ end
23
+ end
24
+
25
+ describe '#sanitize' do
26
+ it 'should modify a email with correct format' do
27
+ EmailSanitizer.base_email = 'default@example.com'
28
+ email = "foo@bar.com"
29
+ EmailSanitizer.sanitize(email).should == ['default+foo_at_bar.com@example.com']
30
+ end
31
+
32
+ it 'should be able to take a string of email addresses' do
33
+ EmailSanitizer.base_email = 'default@example.com'
34
+ [ "foo@bar.com, test@bar.com, abc@example.com", "foo@bar.com test@bar.com abc@example.com", "foo@bar.com; test@bar.com; abc@example.com"].each do |s|
35
+ EmailSanitizer.sanitize(s).should == ["default+foo_at_bar.com@example.com", "default+test_at_bar.com@example.com", "default+abc_at_example.com@example.com"]
36
+ end
37
+ end
38
+
39
+ it "should be able to take an array of email addresses" do
40
+ EmailSanitizer.base_email = 'default@example.com'
41
+ EmailSanitizer.sanitize(["foo@bar.com", "abc@example.com"]).should == ["default+foo_at_bar.com@example.com", 'default+abc_at_example.com@example.com']
42
+ end
43
+ end
44
+
45
+ describe "usage" do
46
+ before(:each) do
47
+ ActionMailer::Base.delivery_method = :test
48
+ EmailSanitizer.base_email = "default@example.com"
49
+ @email = TestMailer.notification
50
+ @email.to.should == ["foo@bar.com"]
51
+ end
52
+
53
+ it "should work as advertised when calling .deliver!" do
54
+ @email.deliver
55
+ @email.to.should == ["default+foo_at_bar.com@example.com"]
56
+ end
57
+
58
+ # This will be fixed in Mail gem soon
59
+ # it "should work as advertised when calling through .delvier!" do
60
+ # @email.deliver!
61
+ # @email.to.should == ["default+foo_at_bar.com@example.com"]
62
+ # end
63
+ end
64
+
65
+
66
+
67
+
68
+ end
69
+
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+ require 'email_sanitizer'
3
+
4
+ describe EmailSanitizer::Interceptor do
5
+ describe "#delivering_email" do
6
+ it "should sanitize 'to' field on email" do
7
+ email_address = "foo@bar.com"
8
+ email = Mail.new(:to => email_address)
9
+ EmailSanitizer::Interceptor.delivering_email(email)
10
+ email.to.should == EmailSanitizer.sanitize(email_address)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,17 @@
1
+ require 'rubygems'
2
+ require 'awesome_print'
3
+ require 'action_mailer'
4
+
5
+ # This file was generated by the `rspec --init` command. Conventionally, all
6
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
7
+ # Require this file using `require "spec_helper.rb"` to ensure that it is only
8
+ # loaded once.
9
+ #
10
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
11
+ RSpec.configure do |config|
12
+ config.treat_symbols_as_metadata_keys_with_true_values = true
13
+ config.run_all_when_everything_filtered = true
14
+ config.filter_run :focus
15
+ end
16
+
17
+ Dir[ File.dirname(__FILE__) + '/support/**/*.rb'].each { |file| require file }
@@ -0,0 +1,5 @@
1
+ class TestMailer < ActionMailer::Base
2
+ def notification
3
+ mail(:to => "foo@bar.com", :from => "test@example.com")
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: email_sanitizer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Eric Zou
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-30 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec-rails
16
+ requirement: &70243766892720 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 2.0.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70243766892720
25
+ - !ruby/object:Gem::Dependency
26
+ name: autotest
27
+ requirement: &70243766892200 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 4.4.6
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70243766892200
36
+ - !ruby/object:Gem::Dependency
37
+ name: actionmailer
38
+ requirement: &70243766891740 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: 3.0.0
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *70243766891740
47
+ description: Email Sanitizer lets you avoid accidentally sending out emails to your
48
+ customers from staging, demo or other non-production environments
49
+ email: hello@ericzou.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - LICENSE
55
+ - README.md
56
+ - lib/email_sanitizer/interceptor.rb
57
+ - lib/email_sanitizer.rb
58
+ - spec/email_sanitizer_spec.rb
59
+ - spec/interceptor_spec.rb
60
+ - spec/spec_helper.rb
61
+ - spec/support/test_mailer.rb
62
+ homepage: https://github.com/ericzou/email_sanitizer
63
+ licenses: []
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 1.8.10
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: Email Sanitizer lets you avoid accidentally sending out emails to your customers
86
+ from staging, demo or other non-production environments
87
+ test_files: []