validates_email_address_of 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +60 -0
- data/Rakefile +2 -0
- data/lib/validates_email_address_of.rb +65 -0
- data/lib/validates_email_address_of/version.rb +3 -0
- data/validates_email_address_of.gemspec +20 -0
- metadata +70 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Nicholas Dobie
|
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,60 @@
|
|
1
|
+
# validates\_email\_address\_of
|
2
|
+
|
3
|
+
This provides an email validator for ActiveRecord 3.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'validates_email_address_of'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install validates_email_address_of
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Here is the quick and dirty way to include ```validates_email_address_of```
|
22
|
+
|
23
|
+
class User < ActiveRecord::Base
|
24
|
+
attr_accessible :email, :password, :password_confirmation
|
25
|
+
has_secure_password
|
26
|
+
validates :email, :email_address => true
|
27
|
+
end
|
28
|
+
|
29
|
+
There are three separate validation tests that get more and more complete. Each test is independent of the others.
|
30
|
+
While all test are indepent if a lower level test fails it will skip the high level test. All test run by default.
|
31
|
+
|
32
|
+
### Format
|
33
|
+
|
34
|
+
This is a basic RegEx validation and only checks that the email is in the correct format. You can enable or disable
|
35
|
+
by specifying the ```:format``` option.
|
36
|
+
|
37
|
+
validates :email, :email_address => { :format => false }
|
38
|
+
|
39
|
+
### MX
|
40
|
+
|
41
|
+
This test gets even more acurate than just checking the format. This checks that the domain has at least one
|
42
|
+
MX record attached to it. this can be disabled similarly to format, except with the ```:mx``` option.
|
43
|
+
|
44
|
+
validates :email, :email_address => { :mx => false }
|
45
|
+
|
46
|
+
### SMTP Address Verification
|
47
|
+
|
48
|
+
This is the ultimate test. This will actually contact the mail server and ask it to verify that the provided email
|
49
|
+
exists. While this guarentees the email will exist it is slow, due to the slow run we suggest only running this test
|
50
|
+
on create. This can be controlled with the ```:smtp``` option.
|
51
|
+
|
52
|
+
validates :email, :email_address => { :smtp => flase }
|
53
|
+
|
54
|
+
## Contributing
|
55
|
+
|
56
|
+
1. Fork it
|
57
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
58
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
59
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
60
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
require "validates_email_address_of/version"
|
2
|
+
require "resolv"
|
3
|
+
require "net/smtp"
|
4
|
+
|
5
|
+
class EmailAddressValidator < ActiveModel::EachValidator
|
6
|
+
|
7
|
+
def default_options
|
8
|
+
@default_options ||= {:format => true, :mx => true, :smtp => true, :message => "invalid email address"}
|
9
|
+
end
|
10
|
+
|
11
|
+
def validate_each (record, attribute, value)
|
12
|
+
options = default_options.merge(self.options)
|
13
|
+
|
14
|
+
if options[:format] && check_format(value) == false
|
15
|
+
record.errors.add(attribute, options[:message])
|
16
|
+
return
|
17
|
+
end
|
18
|
+
|
19
|
+
if options[:mx] && check_mx(value) == false
|
20
|
+
record.errors.add(attribute, options[:message])
|
21
|
+
return
|
22
|
+
end
|
23
|
+
|
24
|
+
if options[:smtp] && check_smtp(value) == false
|
25
|
+
record.errors.add(attribute, options[:message])
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def check_format (value)
|
33
|
+
/\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i.match(value).to_s == value
|
34
|
+
end
|
35
|
+
|
36
|
+
def check_mx (value)
|
37
|
+
@mx = get_mx(value)
|
38
|
+
@mx.size > 0
|
39
|
+
end
|
40
|
+
|
41
|
+
def check_smtp (value)
|
42
|
+
@mx ||= get_mx(value)
|
43
|
+
return unless @mx.size > 0
|
44
|
+
Net::SMTP.start(mx[0].exchange.to_s) do |smtp|
|
45
|
+
begin
|
46
|
+
smtp.mailfrom("checkemail@nickdobie.com")
|
47
|
+
smtp.rcptto(value)
|
48
|
+
rescue
|
49
|
+
nil
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def get_mx(value)
|
55
|
+
# Basic email validation
|
56
|
+
email_match = /\@(.+)/.match(value)
|
57
|
+
if email_match
|
58
|
+
domain = email_match[1]
|
59
|
+
Resolv::DNS.open do |dns|
|
60
|
+
dns.getresources(domain, Resolv::DNS::Resource::IN::MX)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/validates_email_address_of/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Nicholas Dobie"]
|
6
|
+
gem.email = ["dobienick@gmail.com"]
|
7
|
+
gem.description = %q{An advance email validator. Checks format, MX record existence for email domain,
|
8
|
+
and SMTP address validation.}
|
9
|
+
gem.summary = %q{ActiveModel 3 email validator}
|
10
|
+
gem.homepage = ""
|
11
|
+
|
12
|
+
gem.files = `git ls-files`.split($\)
|
13
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
14
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
15
|
+
gem.name = "validates_email_address_of"
|
16
|
+
gem.require_paths = ["lib"]
|
17
|
+
gem.version = ValidatesEmailAddressOf::VERSION
|
18
|
+
|
19
|
+
gem.add_dependency("activemodel", ">=3.0.0")
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: validates_email_address_of
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.6
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Nicholas Dobie
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-07-18 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activemodel
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.0.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: 3.0.0
|
30
|
+
description: ! "An advance email validator. Checks format, MX record existence for
|
31
|
+
email domain,\n and SMTP address validation."
|
32
|
+
email:
|
33
|
+
- dobienick@gmail.com
|
34
|
+
executables: []
|
35
|
+
extensions: []
|
36
|
+
extra_rdoc_files: []
|
37
|
+
files:
|
38
|
+
- .gitignore
|
39
|
+
- Gemfile
|
40
|
+
- LICENSE
|
41
|
+
- README.md
|
42
|
+
- Rakefile
|
43
|
+
- lib/validates_email_address_of.rb
|
44
|
+
- lib/validates_email_address_of/version.rb
|
45
|
+
- validates_email_address_of.gemspec
|
46
|
+
homepage: ''
|
47
|
+
licenses: []
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options: []
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ! '>='
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ! '>='
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
64
|
+
requirements: []
|
65
|
+
rubyforge_project:
|
66
|
+
rubygems_version: 1.8.23
|
67
|
+
signing_key:
|
68
|
+
specification_version: 3
|
69
|
+
summary: ActiveModel 3 email validator
|
70
|
+
test_files: []
|