email_check 0.1.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/.travis.yml +2 -0
- data/README.md +25 -13
- data/lib/email_check.rb +1 -0
- data/lib/email_check/email_address.rb +6 -5
- data/lib/email_check/email_validator.rb +29 -15
- data/lib/email_check/helper_methods.rb +27 -0
- data/lib/email_check/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d9b4feed89776984cfde75a1eae36c6a6d07c3b8
|
4
|
+
data.tar.gz: 9234e3a6758143a7db7c91fc4a3ca1651a7e32c0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 065585a773e9d398f905ea439e8301a2c7e36d3bc7d2d2d215b3666a265a3ea666fbde2827a0078505b8c89583f70d727ccb5a9ced43d1a791bba44b0f2ae551
|
7
|
+
data.tar.gz: 75bba08a31cf07117bb4db4f42f9eb601cf230a85e343423e10e26ddf63f319d8374074312aa0798667b587a10f7034f77f3208b4d7150b58bef4e4ea367024d
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -31,38 +31,49 @@ gem "check_email"
|
|
31
31
|
|
32
32
|
## Usage
|
33
33
|
### Use with ActiveModel
|
34
|
-
To validate just the email address
|
34
|
+
To validate just the format of the email address
|
35
35
|
```ruby
|
36
36
|
class User < ActiveRecord::Base
|
37
|
-
|
37
|
+
validates_email :email
|
38
38
|
end
|
39
39
|
```
|
40
40
|
To validate that the domain has a MX record:
|
41
41
|
```ruby
|
42
|
-
|
42
|
+
validates_email :email, check_mx: true
|
43
43
|
```
|
44
44
|
To validate that the email is not from a disposable or free email provider:
|
45
45
|
```ruby
|
46
|
-
|
46
|
+
validates_email :email, not_disposable:true, not_free:true
|
47
47
|
```
|
48
48
|
To validate that the domain is not blacklisted:
|
49
49
|
```ruby
|
50
|
-
|
50
|
+
validates_email :email, not_blacklisted:true
|
51
51
|
```
|
52
52
|
|
53
53
|
To validate that the username is not blocked
|
54
54
|
```ruby
|
55
|
-
|
55
|
+
validates_email :email, block_special_usernames:true
|
56
|
+
```
|
56
57
|
|
57
58
|
Everything together:
|
58
59
|
```ruby
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
message: "Please register with your corporate email"
|
60
|
+
validates_email :email,
|
61
|
+
check_mx: true,
|
62
|
+
not_disposable:true,
|
63
|
+
not_free:true,
|
64
|
+
not_blacklisted:true,
|
65
|
+
block_special_usernames:true,
|
66
|
+
message: "Please register with your corporate email"
|
67
|
+
```
|
68
|
+
|
69
|
+
This can be replaced by the validates_email_strictness helper. This turns on all the options
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
# Example above
|
73
|
+
validates_email_strictness :email
|
74
|
+
|
75
|
+
# Everything but allow free emails. This is what most people would want to use
|
76
|
+
validates_email_strictness :email, not_free:false
|
66
77
|
```
|
67
78
|
|
68
79
|
### Modifying inbuilt lists
|
@@ -77,6 +88,7 @@ EmailCheck.whitelisted_domains << 'gmail.com'
|
|
77
88
|
EmailCheck.free_email_domains << 'thenewgmail.com'
|
78
89
|
# Setting a domain in the blacklist also will blacklist all subdomains
|
79
90
|
EmailCheck.blacklisted_domains << 'lvh.me'
|
91
|
+
# Block the 'anonymous' username for all domains
|
80
92
|
EmailCheck.blocked_usernames << 'anonymous'
|
81
93
|
```
|
82
94
|
|
data/lib/email_check.rb
CHANGED
@@ -17,11 +17,11 @@ module EmailCheck
|
|
17
17
|
EmailCheck.disposable_email_domains.include?(@email.domain)
|
18
18
|
end
|
19
19
|
|
20
|
-
def
|
20
|
+
def free_email_provider?
|
21
21
|
EmailCheck.free_email_domains.include?(@email.domain)
|
22
22
|
end
|
23
23
|
|
24
|
-
def
|
24
|
+
def blacklisted_domain?
|
25
25
|
EmailCheck.blacklisted_domains.each do |domain|
|
26
26
|
return true if @email.domain.include?(domain)
|
27
27
|
end
|
@@ -29,7 +29,7 @@ module EmailCheck
|
|
29
29
|
false
|
30
30
|
end
|
31
31
|
|
32
|
-
def
|
32
|
+
def whitelisted_domain?
|
33
33
|
EmailCheck.whitelisted_domains.include?(@email.domain)
|
34
34
|
end
|
35
35
|
|
@@ -39,10 +39,11 @@ module EmailCheck
|
|
39
39
|
|
40
40
|
def domain_has_mx?
|
41
41
|
return false unless format_valid?
|
42
|
-
|
42
|
+
val = false
|
43
43
|
Resolv::DNS.open do |dns|
|
44
|
-
|
44
|
+
val = dns.getresources(@email.domain, Resolv::DNS::Resource::IN::MX).any?
|
45
45
|
end
|
46
|
+
return val
|
46
47
|
end
|
47
48
|
|
48
49
|
private
|
@@ -1,40 +1,54 @@
|
|
1
1
|
require 'active_model'
|
2
2
|
require 'active_model/validations'
|
3
3
|
require 'email_check/email_address'
|
4
|
+
require 'pp'
|
4
5
|
|
5
6
|
class EmailValidator < ActiveModel::EachValidator
|
6
7
|
def validate_each(record, attribute, value)
|
7
|
-
error(record, attribute) unless value.present?
|
8
8
|
|
9
|
-
|
9
|
+
unless value.present?
|
10
|
+
add_error(record, attribute)
|
11
|
+
return
|
12
|
+
end
|
10
13
|
|
11
|
-
|
14
|
+
address = EmailCheck::EmailAddress.new(value)
|
12
15
|
|
13
|
-
|
16
|
+
unless address && address.format_valid?
|
17
|
+
add_error(record, attribute)
|
18
|
+
return
|
19
|
+
end
|
14
20
|
|
15
|
-
if options[:
|
16
|
-
|
21
|
+
if options[:block_special_usernames] && address.blocked_username?
|
22
|
+
add_error(record, attribute)
|
23
|
+
return
|
17
24
|
end
|
18
25
|
|
19
|
-
if
|
20
|
-
|
26
|
+
return if address.whitelisted_domain?
|
27
|
+
|
28
|
+
if options[:not_disposable] && address.disposable?
|
29
|
+
add_error(record, attribute)
|
30
|
+
return
|
21
31
|
end
|
22
32
|
|
23
|
-
if options[:
|
24
|
-
|
33
|
+
if options[:not_blacklisted] && address.blacklisted_domain?
|
34
|
+
add_error(record, attribute)
|
35
|
+
return
|
25
36
|
end
|
26
37
|
|
27
|
-
if options[:
|
28
|
-
|
38
|
+
if options[:not_free] && address.free_email_provider?
|
39
|
+
add_error(record, attribute)
|
40
|
+
return
|
29
41
|
end
|
30
42
|
|
31
|
-
if
|
32
|
-
|
43
|
+
# TODO: Add a callback to bypass this if the domain is already known
|
44
|
+
if options[:check_mx] && address.domain_has_mx? == false
|
45
|
+
add_error(record, attribute)
|
46
|
+
return
|
33
47
|
end
|
34
48
|
end
|
35
49
|
|
36
50
|
private
|
37
|
-
def
|
51
|
+
def add_error(record, attribute)
|
38
52
|
record.errors.add(attribute, options[:message] || :invalid)
|
39
53
|
end
|
40
54
|
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module ActiveModel
|
2
|
+
module Validations
|
3
|
+
module HelperMethods
|
4
|
+
# Validates email
|
5
|
+
#
|
6
|
+
# Configuration options:
|
7
|
+
# * <tt>:check_mx</tt> - Check MX record for domain
|
8
|
+
# * <tt>:not_disposable</tt> - Check that this is not a disposable email
|
9
|
+
# * <tt>:not_free</tt> - Not a free email (ex. gmail.com, hotmail.com)
|
10
|
+
# * <tt>:not_blacklisted</tt> - If domain is on the blacklist, reject it
|
11
|
+
# * <tt>:block_special_usernames</tt> - If the username is one of the special usernames, reject it
|
12
|
+
def validates_email(*attr_names)
|
13
|
+
validates_with EmailValidator, _merge_attributes(attr_names)
|
14
|
+
end
|
15
|
+
|
16
|
+
# Turn everything on..
|
17
|
+
def validates_email_strictness(*attr_names)
|
18
|
+
validates_with EmailValidator, _merge_attributes(attr_names).merge(
|
19
|
+
:check_mx => true,
|
20
|
+
:not_disposable => true,
|
21
|
+
:not_free => true,
|
22
|
+
:not_blacklisted => true,
|
23
|
+
:block_special_usernames => true)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/email_check/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: email_check
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Darshan Patil
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-05-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -130,6 +130,7 @@ files:
|
|
130
130
|
- lib/email_check.rb
|
131
131
|
- lib/email_check/email_address.rb
|
132
132
|
- lib/email_check/email_validator.rb
|
133
|
+
- lib/email_check/helper_methods.rb
|
133
134
|
- lib/email_check/version.rb
|
134
135
|
- vendor/blacklist.yml
|
135
136
|
- vendor/blocked_usernames.yml
|