spectator-validates_email 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## PROJECT::GENERAL
9
+ pkg
10
+ rdoc
11
+ *.gem
12
+
13
+ ## PROJECT::SPECIFIC
14
+ test/debug.log
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 [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.rdoc ADDED
@@ -0,0 +1,51 @@
1
+ = validates_email
2
+
3
+ validates_email is a Rails 3 plugin that validates email addresses against RFC 2822 and RFC 3696
4
+
5
+ == Installation
6
+
7
+ rails plugin install git://github.com/spectator/validates_email.git
8
+
9
+ == Usage
10
+
11
+ class User < ActiveRecord::Base
12
+ validates :field, :email => true
13
+ end
14
+
15
+ As well as any other Rails 3 validation this one has the same triggers, such as <tt>:on</tt>, <tt>:if</tt>, <tt>:unless</tt>, <tt>:allow_blank</tt>, and <tt>:allow_nil</tt>.
16
+
17
+ Also, you can pass your custom error message, otherwise it will use ActiveRecord's invalid error message.
18
+
19
+ class User < ActiveRecord::Base
20
+ validates :field, :email => { :message => 'is not an email address' }
21
+ end
22
+
23
+ If you like to check MX Records for email, you can use <tt>:mx</tt> option.
24
+
25
+ class User < ActiveRecord::Base
26
+ validates :field, :email => { :mx => true }
27
+ end
28
+
29
+ And if you like to check MX Records with fallback to A record, use <tt>:a_fallback</tt> option.
30
+
31
+ class User < ActiveRecord::Base
32
+ validates :field, :email => { :mx => { :a_fallback => true } }
33
+ end
34
+
35
+ == Testing
36
+
37
+ To execute unit tests run <tt>rake test</tt> within plugin folder.
38
+
39
+ The unit tests for this plugin use an in-memory sqlite3 database.
40
+
41
+ == Credits
42
+
43
+ Most of the code were taken from Alex Dunae (dunae.ca) plugin (see http://github.com/alexdunae/validates_email_format_of/) and adopted for Rails 3, so pass all beers to him.
44
+
45
+ == Contributors
46
+
47
+ Petr Blaho
48
+
49
+ == Notes
50
+
51
+ Tested under ruby 1.9.2-rc1 and Rails 3 Edge
data/Rakefile ADDED
@@ -0,0 +1,41 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "spectator-validates_email"
8
+ gem.summary = %Q{Gem to validate email addresses for Rails 3}
9
+ gem.description = %Q{validates_email is a Rails 3 plugin that validates email addresses against RFC 2822 and RFC 3696}
10
+ gem.email = "yury.velikanau@gmail.com"
11
+ gem.homepage = "http://github.com/spectator/validates_email"
12
+ gem.authors = ["Yury Velikanau"]
13
+ gem.add_development_dependency "activerecord", ">= 3.0.0.beta"
14
+ gem.add_development_dependency "activesupport", ">= 3.0.0.beta"
15
+ gem.add_development_dependency "sqlite3-ruby", ">= 1.3.1"
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20
+ end
21
+
22
+ require 'rake/testtask'
23
+ Rake::TestTask.new(:test) do |t|
24
+ t.libs << 'lib'
25
+ t.libs << 'test'
26
+ t.pattern = 'test/**/*_test.rb'
27
+ t.verbose = true
28
+ end
29
+
30
+ task :test => :check_dependencies
31
+
32
+ task :default => :test
33
+
34
+ require 'rake/rdoctask'
35
+ Rake::RDocTask.new(:rdoc) do |rdoc|
36
+ rdoc.rdoc_dir = 'rdoc'
37
+ rdoc.title = 'ValidatesEmail'
38
+ rdoc.options << '--line-numbers' << '--inline-source'
39
+ rdoc.rdoc_files.include('README')
40
+ rdoc.rdoc_files.include('lib/**/*.rb')
41
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.2
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'validates_email'
@@ -0,0 +1,40 @@
1
+ class EmailValidator < ActiveModel::EachValidator
2
+
3
+ LocalPartSpecialChars = Regexp.escape('!#$%&\'*-/=?+-^_`{|}~')
4
+ LocalPartUnquoted = '(([[:alnum:]' + LocalPartSpecialChars + ']+[\.\+]+))*[[:alnum:]' + LocalPartSpecialChars + '+]+'
5
+ LocalPartQuoted = '\"(([[:alnum:]' + LocalPartSpecialChars + '\.\+]*|(\\\\[\x00-\xFF]))*)\"'
6
+ Regex = Regexp.new('^((' + LocalPartUnquoted + ')|(' + LocalPartQuoted + ')+)@(((\w+\-+[^_])|(\w+\.[^_]))*([a-z0-9-]{1,63})\.[a-z]{2,6}$)', Regexp::EXTENDED | Regexp::IGNORECASE, 'n')
7
+
8
+ def validates_email_format(email)
9
+ # local part max is 64 chars, domain part max is 255 chars
10
+ # TODO: should this decode escaped entities before counting?
11
+ begin
12
+ local, domain = email.split('@', 2)
13
+ rescue
14
+ return false
15
+ end
16
+
17
+ email =~ Regex and not email =~ /\.\./ and domain.length <= 255 and local.length <= 64
18
+ end
19
+
20
+ def validates_email_domain(email, options)
21
+ require 'resolv'
22
+ a_fallback = options.is_a?(Hash) ? options[:a_fallback] : nil
23
+ domain = email.match(/\@(.+)/)[1]
24
+ Resolv::DNS.open do |dns|
25
+ @mx = dns.getresources(domain, Resolv::DNS::Resource::IN::MX)
26
+ @mx.push(*dns.getresources(domain, Resolv::DNS::Resource::IN::A)) if a_fallback
27
+ end
28
+ @mx.size > 0 ? true : false
29
+ end
30
+
31
+ def validate_each(record, attribute, value)
32
+ unless validates_email_format(value)
33
+ record.errors[attribute] << (options[:message] || I18n.t(:invalid, :scope => [:activerecord, :errors, :messages]))
34
+ end
35
+ if options[:mx] && !validates_email_domain(value, options[:mx])
36
+ record.errors[attribute] << (options[:mx_message] || I18n.t(:mx_invalid, :scope => [:activerecord, :errors, :messages]))
37
+ end
38
+ end
39
+
40
+ end
data/test/database.yml ADDED
@@ -0,0 +1,3 @@
1
+ plugin_test:
2
+ adapter: sqlite3
3
+ database: ":memory:"
data/test/schema.rb ADDED
@@ -0,0 +1,13 @@
1
+ ActiveRecord::Schema.define(:version => 0) do
2
+ create_table :people, :force => true do |t|
3
+ t.string :email
4
+ end
5
+
6
+ create_table :mx_records, :force => true do |t|
7
+ t.string :email
8
+ end
9
+
10
+ create_table :mx_fallbacks, :force => true do |t|
11
+ t.string :email
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'active_record'
4
+ require 'active_support/core_ext/logger'
5
+
6
+ def load_schema
7
+ config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
8
+ ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
9
+
10
+ ActiveRecord::Base.establish_connection(config['plugin_test'])
11
+ load(File.dirname(__FILE__) + "/schema.rb")
12
+ require File.dirname(__FILE__) + '/../init.rb'
13
+ end
@@ -0,0 +1,157 @@
1
+ require 'test_helper'
2
+
3
+ class ValidatesEmailTest < ActiveSupport::TestCase
4
+ load_schema
5
+
6
+ class Person < ActiveRecord::Base
7
+ validates :email, :email => { :message => 'fails with custom message' }
8
+ end
9
+
10
+ class MxRecord < ActiveRecord::Base
11
+ validates :email, :email => { :mx => true,
12
+ :mx_message => 'fails with custom mx message' }
13
+ end
14
+
15
+ class MxFallback < ActiveRecord::Base
16
+ validates :email, :email => { :mx => { :a_fallback => true },
17
+ :mx_message => 'fails with custom mx message' }
18
+ end
19
+
20
+ def test_should_allow_valid_email_addresses
21
+ [
22
+ 'valid@example.com',
23
+ 'Valid@test.example.com',
24
+ 'valid+valid123@test.example.com',
25
+ 'valid_valid123@test.example.com',
26
+ 'valid-valid+123@test.example.co.uk',
27
+ 'valid-valid+1.23@test.example.com.au',
28
+ 'valid@example.co.uk',
29
+ 'v@example.com',
30
+ 'valid@example.ca',
31
+ 'valid_@example.com',
32
+ 'valid123.456@example.org',
33
+ 'valid123.456@example.travel',
34
+ 'valid123.456@example.museum',
35
+ 'valid@example.mobi',
36
+ 'valid@example.info',
37
+ 'valid-@example.com',
38
+ # from RFC 3696, page 6
39
+ 'customer/department=shipping@example.com',
40
+ '$A12345@example.com',
41
+ '!def!xyz%abc@example.com',
42
+ '_somename@example.com',
43
+ # apostrophes
44
+ "test'test@example.com"
45
+ ].each do |email|
46
+ p = create_person(:email => email)
47
+ save_passes(p, email)
48
+ end
49
+ end
50
+
51
+ def test_should_not_allow_invalid_email_addresses
52
+ [
53
+ 'invalid@example-com',
54
+ # period can not start local part
55
+ '.invalid@example.com',
56
+ # period can not end local part
57
+ 'invalid.@example.com',
58
+ # period can not appear twice consecutively in local part
59
+ 'invali..d@example.com',
60
+ # should not allow underscores in domain names
61
+ 'invalid@ex_mple.com',
62
+ 'invalid@example.com.',
63
+ 'invalid@example.com_',
64
+ 'invalid@example.com-',
65
+ 'invalid-example.com',
66
+ 'invalid@example.b#r.com',
67
+ 'invalid@example.c',
68
+ 'invali d@example.com',
69
+ 'invalidexample.com',
70
+ 'invalid@example.'
71
+ ].each do |email|
72
+ p = create_person(:email => email)
73
+ save_fails(p, email)
74
+ end
75
+ end
76
+
77
+ # from http://www.rfc-editor.org/errata_search.php?rfc=3696
78
+ def test_should_allow_quoted_characters
79
+ [
80
+ '"Abc\@def"@example.com',
81
+ '"Fred\ Bloggs"@example.com',
82
+ '"Joe.\\Blow"@example.com'
83
+ ].each do |email|
84
+ p = create_person(:email => email)
85
+ save_passes(p, email)
86
+ end
87
+ end
88
+
89
+ # from http://tools.ietf.org/html/rfc3696, page 5
90
+ # corrected in http://www.rfc-editor.org/errata_search.php?rfc=3696
91
+ def test_should_not_allow_escaped_characters_without_quotes
92
+ [
93
+ 'Fred\ Bloggs_@example.com',
94
+ 'Abc\@def+@example.com',
95
+ 'Joe.\\Blow@example.com'
96
+ ].each do |email|
97
+ p = create_person(:email => email)
98
+ save_fails(p, email)
99
+ end
100
+ end
101
+
102
+ def test_should_check_length_limits
103
+ [
104
+ 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa@example.com',
105
+ 'test@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.com'
106
+ ].each do |email|
107
+ p = create_person(:email => email)
108
+ save_fails(p, email)
109
+ end
110
+ end
111
+
112
+ def test_should_allow_custom_error_message
113
+ p = create_person(:email => @invalid_email)
114
+ save_fails(p)
115
+ # Object.errors[:field] returns array now
116
+ assert_equal ['fails with custom message'], p.errors[:email]
117
+ end
118
+
119
+ def test_should_validate_email_with_mx
120
+ valid_email = 'test@gmail.com'
121
+ p = MxRecord.new(:email => valid_email)
122
+ save_passes(p, valid_email)
123
+
124
+ invalid_email = 'test@example.com'
125
+ p = MxRecord.new(:email => invalid_email)
126
+ save_fails(p, invalid_email)
127
+ end
128
+
129
+ def test_should_validate_with_mx_fallback
130
+ valid_email = 'test@gmail.com'
131
+ p = MxFallback.new(:email => valid_email)
132
+ save_passes(p, valid_email)
133
+
134
+ invalid_email = 'test@example.com'
135
+ p = MxFallback.new(:email => invalid_email)
136
+ save_passes(p, invalid_email)
137
+ end
138
+
139
+ protected
140
+
141
+ def create_person(params)
142
+ Person.new(params)
143
+ end
144
+
145
+ def save_passes(p, email = '')
146
+ assert p.valid?, " validating #{email}"
147
+ assert p.save
148
+ assert_empty p.errors[:email]
149
+ end
150
+
151
+ def save_fails(p, email = '')
152
+ assert !p.valid?, " validating #{email}"
153
+ assert !p.save
154
+ assert_not_empty p.errors[:email]
155
+ end
156
+
157
+ end
@@ -0,0 +1,62 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{validates_email}
8
+ s.version = "0.0.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Yury Velikanau"]
12
+ s.date = %q{2010-08-06}
13
+ s.description = %q{validates_email is a Rails 3 plugin that validates email addresses against RFC 2822 and RFC 3696}
14
+ s.email = %q{yury.velikanau@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "README.rdoc"
17
+ ]
18
+ s.files = [
19
+ ".gitignore",
20
+ "MIT-LICENSE",
21
+ "README.rdoc",
22
+ "Rakefile",
23
+ "VERSION",
24
+ "init.rb",
25
+ "lib/validates_email.rb",
26
+ "test/database.yml",
27
+ "test/schema.rb",
28
+ "test/test_helper.rb",
29
+ "test/validates_email_test.rb",
30
+ "validates_email.gemspec"
31
+ ]
32
+ s.homepage = %q{http://github.com/spectator/validates_email}
33
+ s.rdoc_options = ["--charset=UTF-8"]
34
+ s.require_paths = ["lib"]
35
+ s.rubygems_version = %q{1.3.6}
36
+ s.summary = %q{Gem to validate email addresses for Rails 3}
37
+ s.test_files = [
38
+ "test/schema.rb",
39
+ "test/test_helper.rb",
40
+ "test/validates_email_test.rb"
41
+ ]
42
+
43
+ if s.respond_to? :specification_version then
44
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
45
+ s.specification_version = 3
46
+
47
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
48
+ s.add_development_dependency(%q<activerecord>, [">= 3.0.0.beta"])
49
+ s.add_development_dependency(%q<activesupport>, [">= 3.0.0.beta"])
50
+ s.add_development_dependency(%q<sqlite3-ruby>, [">= 1.3.1"])
51
+ else
52
+ s.add_dependency(%q<activerecord>, [">= 3.0.0.beta"])
53
+ s.add_dependency(%q<activesupport>, [">= 3.0.0.beta"])
54
+ s.add_dependency(%q<sqlite3-ruby>, [">= 1.3.1"])
55
+ end
56
+ else
57
+ s.add_dependency(%q<activerecord>, [">= 3.0.0.beta"])
58
+ s.add_dependency(%q<activesupport>, [">= 3.0.0.beta"])
59
+ s.add_dependency(%q<sqlite3-ruby>, [">= 1.3.1"])
60
+ end
61
+ end
62
+
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spectator-validates_email
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 2
9
+ version: 0.0.2
10
+ platform: ruby
11
+ authors:
12
+ - Yury Velikanau
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-08-06 00:00:00 +03:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: activerecord
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 3
29
+ - 0
30
+ - 0
31
+ - beta
32
+ version: 3.0.0.beta
33
+ type: :development
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: activesupport
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ segments:
43
+ - 3
44
+ - 0
45
+ - 0
46
+ - beta
47
+ version: 3.0.0.beta
48
+ type: :development
49
+ version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ name: sqlite3-ruby
52
+ prerelease: false
53
+ requirement: &id003 !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ segments:
58
+ - 1
59
+ - 3
60
+ - 1
61
+ version: 1.3.1
62
+ type: :development
63
+ version_requirements: *id003
64
+ description: validates_email is a Rails 3 plugin that validates email addresses against RFC 2822 and RFC 3696
65
+ email: yury.velikanau@gmail.com
66
+ executables: []
67
+
68
+ extensions: []
69
+
70
+ extra_rdoc_files:
71
+ - README.rdoc
72
+ files:
73
+ - .gitignore
74
+ - MIT-LICENSE
75
+ - README.rdoc
76
+ - Rakefile
77
+ - VERSION
78
+ - init.rb
79
+ - lib/validates_email.rb
80
+ - test/database.yml
81
+ - test/schema.rb
82
+ - test/test_helper.rb
83
+ - test/validates_email_test.rb
84
+ - validates_email.gemspec
85
+ has_rdoc: true
86
+ homepage: http://github.com/spectator/validates_email
87
+ licenses: []
88
+
89
+ post_install_message:
90
+ rdoc_options:
91
+ - --charset=UTF-8
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ segments:
99
+ - 0
100
+ version: "0"
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ segments:
106
+ - 0
107
+ version: "0"
108
+ requirements: []
109
+
110
+ rubyforge_project:
111
+ rubygems_version: 1.3.6
112
+ signing_key:
113
+ specification_version: 3
114
+ summary: Gem to validate email addresses for Rails 3
115
+ test_files:
116
+ - test/schema.rb
117
+ - test/test_helper.rb
118
+ - test/validates_email_test.rb