validates_email_whitelist_of 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :gemcutter
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,30 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ validates_email_whitelist_of (1.0.0)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ activerecord (2.3.11)
10
+ activesupport (= 2.3.11)
11
+ activesupport (2.3.11)
12
+ diff-lcs (1.1.2)
13
+ rspec (2.4.0)
14
+ rspec-core (~> 2.4.0)
15
+ rspec-expectations (~> 2.4.0)
16
+ rspec-mocks (~> 2.4.0)
17
+ rspec-core (2.4.0)
18
+ rspec-expectations (2.4.0)
19
+ diff-lcs (~> 1.1.2)
20
+ rspec-mocks (2.4.0)
21
+ sqlite3 (1.3.3)
22
+
23
+ PLATFORMS
24
+ ruby
25
+
26
+ DEPENDENCIES
27
+ activerecord (~> 2.3.11)
28
+ rspec (~> 2.4.0)
29
+ sqlite3
30
+ validates_email_whitelist_of!
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 [name of plugin creator]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,41 @@
1
+ validates_email_whitelist_of
2
+ ============================
3
+
4
+ Plugin for ActiveRecord for validating email format and whitelisting the domain.
5
+
6
+ Works on Rails 2+, Rails 3+ and Ruby 1.8+.
7
+
8
+ Usage
9
+ =====
10
+
11
+ Add the gem to your Rails Gemfile
12
+
13
+ gem 'validates_email_whitelist_of'
14
+
15
+ class User < ActiveRecord::Base
16
+ validates_email_whitelist_of :email, :whitelist => ['example.com', 'google.com']
17
+ end
18
+
19
+ On ActiveRecord 3.0+ you can do
20
+
21
+ class User < ActiveRecord::Base
22
+ validates :email, :whitelist => true
23
+ end
24
+
25
+ If you want to use I18n, make sure you add the scope
26
+
27
+ `activerecord.errors.messages.invalid_email`
28
+ `activerecord.errors.messages.invalid_whitelist`
29
+
30
+ or
31
+
32
+ `errors.messages.invalid_email`
33
+ `errors.messages.invalid_whitelist`
34
+
35
+ The following locales are builtin:
36
+
37
+ * pt
38
+ * pt-BR
39
+ * en
40
+
41
+ Copyright (c) 2011 Ruben Fonseca, released under the MIT license
@@ -0,0 +1,10 @@
1
+ en:
2
+ activerecord:
3
+ errors:
4
+ messages:
5
+ invalid_email: "is not a valid address"
6
+ invalid_whitelist: "is not an allowed email"
7
+ errors:
8
+ messages:
9
+ invalid_email: "is not a valid e-mail"
10
+ invalid_whitelist: "is not an allowed email"
@@ -0,0 +1,16 @@
1
+ pt: &pt
2
+ activerecord:
3
+ errors:
4
+ messages:
5
+ invalid_email: "não parece ser um e-mail válido"
6
+ invalid_whitelist: "não é um e-mail permitido"
7
+ errors:
8
+ messages:
9
+ invalid_email: "não parece ser um e-mail válido"
10
+ invalid_whitelist: "não é um e-mail permitido"
11
+
12
+ pt-BR:
13
+ <<: *pt
14
+
15
+ pt-PT:
16
+ <<: *pt
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require "validates_email_whitelist_of"
@@ -0,0 +1,28 @@
1
+ module SimplesIdeias
2
+ module ValidatesEmailWhitelistOf
3
+ def validates_email_whitelist_of(*attr_names)
4
+ options = {
5
+ :on => :save,
6
+ :allow_nil => false,
7
+ :allow_blank => false,
8
+ :whitelist => []
9
+ }
10
+
11
+ options.update(attr_names.pop) if attr_names.last.kind_of?(Hash)
12
+
13
+ validates_each(attr_names, options) do |record, attr_name, value|
14
+ if value.to_s =~ EMAIL_FORMAT
15
+ message = ::I18n.t(:invalid_whitelist, :scope => 'activerecord.errors.messages')
16
+ record.errors.add(attr_name, message) unless options[:whitelist].include?($2)
17
+ else
18
+ message = ::I18n.translate(options[:message], :default => [:"activerecord.errors.messages.invalid_email", "is not a valid address"])
19
+ record.errors.add(attr_name, message)
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ class ActiveRecord::Base
27
+ extend SimplesIdeias::ValidatesEmailWhitelistOf
28
+ end
@@ -0,0 +1,21 @@
1
+ module ActiveModel
2
+ module Validations
3
+ class EmailWhitelistValidator < EachValidator
4
+ def validate_each(record, attribute, value)
5
+ if value.to_s !~ SimplesIdeias::ValidatesEmailWhitelistOf::EMAIL_FORMAT
6
+ record.errors.add(attribute, :invalid_email, :default => options[:message], :value => value)
7
+ else
8
+ unless (options[:whitelist] || []).include?($2)
9
+ record.errors.add(attribute, :invalid_whitelist, :default => options[:message], :value => value)
10
+ end
11
+ end
12
+ end
13
+ end
14
+
15
+ module ClassMethods
16
+ def validates_email_whitelist_of(*attr_names)
17
+ validates_with EmailWhitelistValidator, _merge_attributes(attr_names)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,13 @@
1
+ module SimplesIdeias
2
+ module ValidatesEmailWhitelistOf
3
+ EMAIL_FORMAT = /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
4
+ end
5
+ end
6
+
7
+ I18n.load_path += Dir[File.dirname(__FILE__) + "/../config/locales/*.yml"]
8
+
9
+ if ActiveRecord::VERSION::STRING >= "3.0"
10
+ require "rails3/validates_email_whitelist_of"
11
+ else
12
+ require "rails2/validates_email_whitelist_of"
13
+ end
@@ -0,0 +1,89 @@
1
+ $LOAD_PATH.unshift File.dirname(__FILE__) + "/.."
2
+ $LOAD_PATH.unshift File.dirname(__FILE__) + "/../lib"
3
+
4
+ require "rubygems"
5
+ require "bundler/setup"
6
+
7
+ require "active_record"
8
+ require "active_record/migration"
9
+ require "init"
10
+
11
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
12
+
13
+ ActiveRecord::Schema.define(:version => 0) do
14
+ create_table :users do |t|
15
+ t.string :email, :corporate_email
16
+ end
17
+ end
18
+
19
+ ::I18n.backend.__send__ :init_translations
20
+
21
+ INVALID_EMAILS = [
22
+ 'invalid@example-com',
23
+ # period can not start local part
24
+ '.invalid@example.com',
25
+ # period can not end local part
26
+ 'invalid.@example.com',
27
+ # period can not appear twice consecutively in local part
28
+ 'invali..d@example.com',
29
+ # should not allow underscores in domain names
30
+ 'invalid@ex_mple.com',
31
+ 'invalid@example.com.',
32
+ 'invalid@example.com_',
33
+ 'invalid@example.com-',
34
+ 'invalid-example.com',
35
+ 'invalid@example.b#r.com',
36
+ 'invalid@example.c',
37
+ 'invali d@example.com',
38
+ 'invalidexample.com',
39
+ 'invalid@example.',
40
+ # from http://tools.ietf.org/html/rfc3696, page 5
41
+ # corrected in http://www.rfc-editor.org/errata_search.php?rfc=3696
42
+ 'Fred\ Bloggs_@example.com',
43
+ 'Abc\@def+@example.com',
44
+ 'Joe.\\Blow@example.com'
45
+ ]
46
+
47
+ VALID_EMAILS = [
48
+ 'valid@example.com',
49
+ 'Valid@test.example.com',
50
+ 'valid+valid123@test.example.com',
51
+ 'valid_valid123@test.example.com',
52
+ 'valid-valid+123@test.example.co.uk',
53
+ 'valid-valid+1.23@test.example.com.au',
54
+ 'valid@example.co.uk',
55
+ 'v@example.com',
56
+ 'valid@example.ca',
57
+ 'valid_@example.com',
58
+ 'valid123.456@example.org',
59
+ 'valid123.456@example.travel',
60
+ 'valid123.456@example.museum',
61
+ 'valid@example.mobi',
62
+ 'valid@example.info',
63
+ 'valid-@example.com',
64
+ # from RFC 3696, page 6
65
+ 'customer/department=shipping@example.com',
66
+ '$A12345@example.com',
67
+ '!def!xyz%abc@example.com',
68
+ '_somename@example.com',
69
+ # apostrophes
70
+ "test'test@example.com",
71
+ # '"Abc\@def"@example.com',
72
+ # from http://www.rfc-editor.org/errata_search.php?rfc=3696
73
+ # '"Fred\ Bloggs"@example.com',
74
+ '"Joe.\\Blow"@example.com'
75
+ ]
76
+
77
+ WHITELIST = [
78
+ 'example.com',
79
+ 'test.example.com',
80
+ 'test.example.co.uk',
81
+ 'test.example.com.au',
82
+ 'example.co.uk',
83
+ 'example.info',
84
+ 'example.ca',
85
+ 'example.org',
86
+ 'example.travel',
87
+ 'example.museum',
88
+ 'example.mobi'
89
+ ]
@@ -0,0 +1,115 @@
1
+ # encoding: utf-8
2
+ require File.dirname(__FILE__) + "/spec_helper"
3
+
4
+ describe "validates email format of" do
5
+ class User < ActiveRecord::Base
6
+ validates_email_whitelist_of :email, :corporate_email, :allow_blank => false, :whitelist => WHITELIST
7
+ end
8
+
9
+ class Buyer < ActiveRecord::Base
10
+ set_table_name :users
11
+ validates_email_whitelist_of :email, :message => "is not a valid e-mail", :whitelist => WHITELIST
12
+ end
13
+
14
+ before do
15
+ I18n.locale = :en
16
+ end
17
+
18
+ VALID_EMAILS.each do |email|
19
+ it "should accept #{email.inspect} as a valid email" do
20
+ user = User.new(:email => email, :corporate_email => email)
21
+ user.should be_valid
22
+ end
23
+ end
24
+
25
+ INVALID_EMAILS.each do |email|
26
+ it "should reject #{email.inspect} as a valid email" do
27
+ user = User.new(:email => "invalid", :corporate_email => "invalid")
28
+ user.should_not be_valid
29
+ end
30
+ end
31
+
32
+ it "should reject a not whitelisted email" do
33
+ user = User.new(:email => "foo@notwhitelisted.com")
34
+ user.should_not be_valid
35
+ errors_for(user, :email).should == ["is not an allowed email"]
36
+ end
37
+
38
+ it "should use default error message" do
39
+ user = User.new(:email => "invalid")
40
+ user.should_not be_valid
41
+ errors_for(user, :email).should == ["is not a valid address"]
42
+ end
43
+
44
+ it "should reject nil value" do
45
+ user = User.new(:email => nil)
46
+ user.should_not be_valid
47
+ errors_for(user, :email).should_not be_empty
48
+ end
49
+
50
+ it "should reject empty value" do
51
+ user = User.new(:email => "")
52
+ user.should_not be_valid
53
+ errors_for(user, :email).should_not be_empty
54
+ end
55
+
56
+ it "should validate multiple attributes" do
57
+ user = User.new(:corporate_email => "invalid")
58
+ user.should_not be_valid
59
+ errors_for(user, :corporate_email).should == ["is not a valid address"]
60
+ end
61
+
62
+ it "should use custom error message as :message options" do
63
+ buyer = Buyer.new(:email => "invalid")
64
+ buyer.should_not be_valid
65
+ errors_for(buyer, :email).should == ["is not a valid address"]
66
+ end
67
+
68
+ it "should use I18n string as error message [pt]" do
69
+ I18n.locale = :pt
70
+ user = User.new(:email => "invalid")
71
+ user.should_not be_valid
72
+ errors_for(user, :email).should == ["não parece ser um e-mail válido"]
73
+ end
74
+
75
+ it "should use I18n string as error message [pt-BR]" do
76
+ I18n.locale = :'pt-BR'
77
+ user = User.new(:email => "invalid")
78
+ user.should_not be_valid
79
+ errors_for(user, :email).should == ["não parece ser um e-mail válido"]
80
+ end
81
+
82
+ it "should use I18n string as error message [pt-PT]" do
83
+ I18n.locale = :'pt-PT'
84
+ user = User.new(:email => "invalid")
85
+ user.should_not be_valid
86
+ errors_for(user, :email).should == ["não parece ser um e-mail válido"]
87
+ end
88
+
89
+ if ActiveRecord::VERSION::STRING >= "3.0"
90
+ context "ActiveRecord 3.0+" do
91
+ class Person < ActiveRecord::Base
92
+ set_table_name :users
93
+ validates :email, :email => true
94
+ end
95
+
96
+ VALID_EMAILS.each do |email|
97
+ it "should accept #{email.inspect} as a valid email" do
98
+ user = Person.new(:email => email)
99
+ user.should be_valid
100
+ end
101
+ end
102
+
103
+ INVALID_EMAILS.each do |email|
104
+ it "should reject #{email.inspect} as a valid email" do
105
+ user = Person.new(:email => "invalid")
106
+ user.should_not be_valid
107
+ end
108
+ end
109
+ end
110
+ end
111
+
112
+ def errors_for(record, attr_name)
113
+ [record.errors[attr_name]].flatten.compact
114
+ end
115
+ end
@@ -0,0 +1,24 @@
1
+ spec = Gem::Specification.new do |s|
2
+ s.name = 'validates_email_whitelist_of'
3
+
4
+ s.version = '1.0.1'
5
+ s.summary = 'Validates e-mail address format (RFC2822 and RFC3696) and whitelist the domain part.'
6
+ s.description = "Rails 2 and Rails 3 plugin. #{s.summary}"
7
+ s.homepage = 'https://github.com/rubenfonseca/validates_email_whitelist_of'
8
+ s.extra_rdoc_files = ['README.markdown', 'MIT-LICENSE']
9
+
10
+ s.files = `git ls-files`.split("\n")
11
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
12
+
13
+ s.require_path = 'lib'
14
+ s.rdoc_options << '--title' << 'validates_email_whitelist_of'
15
+
16
+ s.authors = ['Ruben Fonseca']
17
+ s.email = 'fonseka@gmail.com'
18
+
19
+ s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version=
20
+
21
+ s.add_development_dependency "rspec", "~> 2.4.0"
22
+ s.add_development_dependency "activerecord", "~> 2.3.11"
23
+ s.add_development_dependency "sqlite3"
24
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: validates_email_whitelist_of
3
+ version: !ruby/object:Gem::Version
4
+ hash: 21
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 1
10
+ version: 1.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Ruben Fonseca
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-05-03 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ hash: 31
29
+ segments:
30
+ - 2
31
+ - 4
32
+ - 0
33
+ version: 2.4.0
34
+ type: :development
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: activerecord
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ hash: 21
45
+ segments:
46
+ - 2
47
+ - 3
48
+ - 11
49
+ version: 2.3.11
50
+ type: :development
51
+ version_requirements: *id002
52
+ - !ruby/object:Gem::Dependency
53
+ name: sqlite3
54
+ prerelease: false
55
+ requirement: &id003 !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ hash: 3
61
+ segments:
62
+ - 0
63
+ version: "0"
64
+ type: :development
65
+ version_requirements: *id003
66
+ description: Rails 2 and Rails 3 plugin. Validates e-mail address format (RFC2822 and RFC3696) and whitelist the domain part.
67
+ email: fonseka@gmail.com
68
+ executables: []
69
+
70
+ extensions: []
71
+
72
+ extra_rdoc_files:
73
+ - README.markdown
74
+ - MIT-LICENSE
75
+ files:
76
+ - Gemfile
77
+ - Gemfile.lock
78
+ - MIT-LICENSE
79
+ - README.markdown
80
+ - config/locales/en.yml
81
+ - config/locales/pt.yml
82
+ - init.rb
83
+ - lib/rails2/validates_email_whitelist_of.rb
84
+ - lib/rails3/validates_email_whitelist_of.rb
85
+ - lib/validates_email_whitelist_of.rb
86
+ - spec/spec_helper.rb
87
+ - spec/validates_email_format_of_spec.rb
88
+ - validates_email_whitelist_of.gemspec
89
+ homepage: https://github.com/rubenfonseca/validates_email_whitelist_of
90
+ licenses: []
91
+
92
+ post_install_message:
93
+ rdoc_options:
94
+ - --title
95
+ - validates_email_whitelist_of
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ hash: 3
104
+ segments:
105
+ - 0
106
+ version: "0"
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ">"
111
+ - !ruby/object:Gem::Version
112
+ hash: 25
113
+ segments:
114
+ - 1
115
+ - 3
116
+ - 1
117
+ version: 1.3.1
118
+ requirements: []
119
+
120
+ rubyforge_project:
121
+ rubygems_version: 1.7.2
122
+ signing_key:
123
+ specification_version: 3
124
+ summary: Validates e-mail address format (RFC2822 and RFC3696) and whitelist the domain part.
125
+ test_files:
126
+ - spec/spec_helper.rb
127
+ - spec/validates_email_format_of_spec.rb