valid_email2 0.0.4 → 1.0.0
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.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.travis.yml +6 -0
- data/CHANGELOG.md +3 -0
- data/README.md +61 -4
- data/lib/valid_email2/address.rb +2 -1
- data/lib/valid_email2/email_validator.rb +30 -0
- data/lib/valid_email2.rb +3 -31
- data/spec/valid_email2_spec.rb +5 -0
- data/valid_email2.gemspec +1 -1
- metadata +4 -3
- data/lib/valid_email2/version.rb +0 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c31c962fd50265d3f322c486870d78a569838374
|
4
|
+
data.tar.gz: b35f54c7e817503ef2142b1fda7a22762a4f7b2d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b7554c788ed305803a85e6a9a7a2cb05f33ae1e161fb2b1a9e75fe114e452d9b7d2e68e8dc9dc59e190996de941cbb280f393ef2197918e1d6093e9dc0c91fe9
|
7
|
+
data.tar.gz: 0d8cbff9a1ede7b3d8ae87d71d08c682dd996cf6a57fbd5e198b9de628390eda1a209fd766678a2c95eab214f02e9d035fc35a94996c5372adb8a2b459d595e1
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
@@ -3,6 +3,7 @@ language: ruby
|
|
3
3
|
rvm:
|
4
4
|
- 2.0.0
|
5
5
|
- 1.9.3
|
6
|
+
- 1.9.2
|
6
7
|
- jruby-19mode
|
7
8
|
- jruby-20mode
|
8
9
|
- rbx-19mode
|
@@ -10,3 +11,8 @@ rvm:
|
|
10
11
|
gemfile:
|
11
12
|
- gemfiles/activemodel3.gemfile
|
12
13
|
- gemfiles/activemodel4.gemfile
|
14
|
+
|
15
|
+
matrix:
|
16
|
+
exclude:
|
17
|
+
- rvm: 1.9.2
|
18
|
+
gemfile: gemfiles/activemodel4.gemfile
|
data/CHANGELOG.md
ADDED
data/README.md
CHANGED
@@ -1,17 +1,31 @@
|
|
1
1
|
# ValidEmail2
|
2
2
|
[](https://travis-ci.org/lisinge/valid_email2)
|
3
|
+
[](http://badge.fury.io/rb/valid_email2)
|
3
4
|
[](https://coveralls.io/r/lisinge/valid_email2)
|
4
5
|
[](https://codeclimate.com/github/lisinge/valid_email2)
|
5
6
|
[](https://gemnasium.com/lisinge/valid_email2)
|
6
7
|
|
7
|
-
Validate emails
|
8
|
-
|
8
|
+
Validate emails with the help of the `mail` gem instead of some cluncy regexp.
|
9
|
+
Aditionally validate that the domain has a MX record.
|
10
|
+
Optionally validate against a static [list of disposable email services](vendor/disposable_emails.yml).
|
11
|
+
|
12
|
+
|
13
|
+
### Why?
|
14
|
+
|
15
|
+
There are lots of other gems and libraries that validates email adresses but most of them use some cluncy regexp.
|
16
|
+
I also saw a need to be able to validate that the email address is not coming from a "disposable email" provider.
|
17
|
+
|
18
|
+
### Is it production ready?
|
19
|
+
|
20
|
+
Yes, it is used in several production apps.
|
9
21
|
|
10
22
|
## Installation
|
11
23
|
|
12
24
|
Add this line to your application's Gemfile:
|
13
25
|
|
14
|
-
|
26
|
+
```ruby
|
27
|
+
gem "valid_email2"
|
28
|
+
```
|
15
29
|
|
16
30
|
And then execute:
|
17
31
|
|
@@ -23,7 +37,50 @@ Or install it yourself as:
|
|
23
37
|
|
24
38
|
## Usage
|
25
39
|
|
26
|
-
|
40
|
+
### Use with ActiveModel
|
41
|
+
|
42
|
+
If you just want to validate that it is a valid email address:
|
43
|
+
```ruby
|
44
|
+
class User < ActiveRecord::Base
|
45
|
+
validates :email, presence: true, email: true
|
46
|
+
end
|
47
|
+
```
|
48
|
+
|
49
|
+
To validate that the domain has a MX record:
|
50
|
+
```ruby
|
51
|
+
validates :email, email: { mx: true }
|
52
|
+
```
|
53
|
+
|
54
|
+
To validate that the domain is not a disposable email:
|
55
|
+
```ruby
|
56
|
+
validates :email, email: { disposable: true }
|
57
|
+
```
|
58
|
+
|
59
|
+
All together:
|
60
|
+
```ruby
|
61
|
+
validates :email, email: { mx: true, disposable: true }
|
62
|
+
```
|
63
|
+
|
64
|
+
> Note that this gem will let an empty email pass through so you will need to
|
65
|
+
> add `presence: true` if you require an email
|
66
|
+
|
67
|
+
### Use without ActiveModel
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
address = ValidEmail2::Address.new("lisinge@gmail.com")
|
71
|
+
address.valid? => true
|
72
|
+
address.disposable? => false
|
73
|
+
address.valid_mx? => true
|
74
|
+
```
|
75
|
+
|
76
|
+
## Requirements
|
77
|
+
|
78
|
+
This gem requires Rails 3.2 or 4.0. It is tested against both versions under:
|
79
|
+
* Ruby-1.9
|
80
|
+
* Ruby-2.0
|
81
|
+
* JRuby-1.9
|
82
|
+
* JRuby-2.0
|
83
|
+
* Rubinius-1.9
|
27
84
|
|
28
85
|
## Contributing
|
29
86
|
|
data/lib/valid_email2/address.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require "valid_email2"
|
1
2
|
require "resolv"
|
2
3
|
require "mail"
|
3
4
|
|
@@ -7,9 +8,9 @@ module ValidEmail2
|
|
7
8
|
|
8
9
|
def initialize(address)
|
9
10
|
@parse_error = false
|
11
|
+
@raw_address = address
|
10
12
|
|
11
13
|
begin
|
12
|
-
@raw_address = address
|
13
14
|
@address = Mail::Address.new(address)
|
14
15
|
rescue Mail::Field::ParseError
|
15
16
|
@parse_error = true
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require "valid_email2/address"
|
2
|
+
require "active_model"
|
3
|
+
require "active_model/validations"
|
4
|
+
|
5
|
+
class EmailValidator < ActiveModel::EachValidator
|
6
|
+
def default_options
|
7
|
+
{ regex: true, disposable: false, mx: false }
|
8
|
+
end
|
9
|
+
|
10
|
+
def validate_each(record, attribute, value)
|
11
|
+
return unless value.present?
|
12
|
+
options = default_options.merge(self.options)
|
13
|
+
|
14
|
+
address = ValidEmail2::Address.new(value)
|
15
|
+
|
16
|
+
error(record, attribute) && return unless address.valid?
|
17
|
+
|
18
|
+
if options[:disposable]
|
19
|
+
error(record, attribute) && return if address.disposable?
|
20
|
+
end
|
21
|
+
|
22
|
+
if options[:mx]
|
23
|
+
error(record, attribute) && return unless address.valid_mx?
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def error(record, attribute)
|
28
|
+
record.errors.add(attribute, options[:message] || :invalid)
|
29
|
+
end
|
30
|
+
end
|
data/lib/valid_email2.rb
CHANGED
@@ -1,37 +1,9 @@
|
|
1
|
-
require "valid_email2/
|
2
|
-
require "valid_email2/address"
|
3
|
-
require "active_model"
|
4
|
-
require "active_model/validations"
|
1
|
+
require "valid_email2/email_validator"
|
5
2
|
|
6
3
|
module ValidEmail2
|
4
|
+
VERSION = "1.0.0"
|
5
|
+
|
7
6
|
def self.disposable_emails
|
8
7
|
@@disposable_emails ||= YAML.load_file(File.expand_path("../../vendor/disposable_emails.yml",__FILE__))
|
9
8
|
end
|
10
9
|
end
|
11
|
-
|
12
|
-
class EmailValidator < ActiveModel::EachValidator
|
13
|
-
def default_options
|
14
|
-
{ regex: true, disposable: false, mx: false }
|
15
|
-
end
|
16
|
-
|
17
|
-
def validate_each(record, attribute, value)
|
18
|
-
return unless value.present?
|
19
|
-
options = default_options.merge(self.options)
|
20
|
-
|
21
|
-
address = ValidEmail2::Address.new(value)
|
22
|
-
|
23
|
-
error(record, attribute) && return unless address.valid?
|
24
|
-
|
25
|
-
if options[:disposable]
|
26
|
-
error(record, attribute) && return if address.disposable?
|
27
|
-
end
|
28
|
-
|
29
|
-
if options[:mx]
|
30
|
-
error(record, attribute) && return unless address.valid_mx?
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
def error(record, attribute)
|
35
|
-
record.errors.add(attribute, options[:message] || :invalid)
|
36
|
-
end
|
37
|
-
end
|
data/spec/valid_email2_spec.rb
CHANGED
@@ -20,6 +20,11 @@ describe ValidEmail2 do
|
|
20
20
|
user.valid?.should be_true
|
21
21
|
end
|
22
22
|
|
23
|
+
it "should not be valid when domain is missing" do
|
24
|
+
user = TestUser.new(email: "foo")
|
25
|
+
user.valid?.should be_false
|
26
|
+
end
|
27
|
+
|
23
28
|
it "should be invalid when email is malformed" do
|
24
29
|
user = TestUser.new(email: "foo@bar")
|
25
30
|
user.valid?.should be_false
|
data/valid_email2.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: valid_email2
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Micke Lisinge
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-07-
|
11
|
+
date: 2013-07-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -104,6 +104,7 @@ extra_rdoc_files: []
|
|
104
104
|
files:
|
105
105
|
- .gitignore
|
106
106
|
- .travis.yml
|
107
|
+
- CHANGELOG.md
|
107
108
|
- Gemfile
|
108
109
|
- LICENSE.txt
|
109
110
|
- README.md
|
@@ -112,7 +113,7 @@ files:
|
|
112
113
|
- gemfiles/activemodel4.gemfile
|
113
114
|
- lib/valid_email2.rb
|
114
115
|
- lib/valid_email2/address.rb
|
115
|
-
- lib/valid_email2/
|
116
|
+
- lib/valid_email2/email_validator.rb
|
116
117
|
- spec/spec_helper.rb
|
117
118
|
- spec/valid_email2_spec.rb
|
118
119
|
- valid_email2.gemspec
|
data/lib/valid_email2/version.rb
DELETED