email_check 0.1.4 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2fa37a71c436036dacc36c1bf38d1fc276e3a644
4
- data.tar.gz: 56c787b14b00b78172b51d0b12a58252d1de5d00
3
+ metadata.gz: d9b4feed89776984cfde75a1eae36c6a6d07c3b8
4
+ data.tar.gz: 9234e3a6758143a7db7c91fc4a3ca1651a7e32c0
5
5
  SHA512:
6
- metadata.gz: 4837b5e6772915a42264e22a54e8d656cb5a30f596d899f74b0e97d199b620e68822e6eb73156a3fa5e25c9127e74af7ad4ba7b7051565f53d7da7a4b4200e32
7
- data.tar.gz: e9c9d583eecec2a3395261f285fd53815875f9269539f4ae3f424faedf94c21b2a587e292b14c65c23b489a1afdd84cfe099d51ba03f018e0408994d2b909bb4
6
+ metadata.gz: 065585a773e9d398f905ea439e8301a2c7e36d3bc7d2d2d215b3666a265a3ea666fbde2827a0078505b8c89583f70d727ccb5a9ced43d1a791bba44b0f2ae551
7
+ data.tar.gz: 75bba08a31cf07117bb4db4f42f9eb601cf230a85e343423e10e26ddf63f319d8374074312aa0798667b587a10f7034f77f3208b4d7150b58bef4e4ea367024d
@@ -7,3 +7,5 @@ rvm:
7
7
  gemfile:
8
8
  - gemfiles/activemodel4.gemfile
9
9
 
10
+ before_install: gem install bundler
11
+
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
- validates :email, email: true
37
+ validates_email :email
38
38
  end
39
39
  ```
40
40
  To validate that the domain has a MX record:
41
41
  ```ruby
42
- validates :email, email: { mx: true }
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
- validates :email, email: { disposable:true, free:true }
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
- validates :email, email: { blacklist:true}
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
- validates :email, email: { blocked_usernames:true }
55
+ validates_email :email, block_special_usernames:true
56
+ ```
56
57
 
57
58
  Everything together:
58
59
  ```ruby
59
- validates :email, email: {
60
- mx: true,
61
- disposable:true,
62
- free:true,
63
- blacklist:true,
64
- blocked_usernames:true,
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
 
@@ -1,5 +1,6 @@
1
1
  require "email_check/version"
2
2
  require "email_check/email_validator"
3
+ require 'email_check/helper_methods'
3
4
 
4
5
  module EmailCheck
5
6
  # Load the data
@@ -17,11 +17,11 @@ module EmailCheck
17
17
  EmailCheck.disposable_email_domains.include?(@email.domain)
18
18
  end
19
19
 
20
- def free?
20
+ def free_email_provider?
21
21
  EmailCheck.free_email_domains.include?(@email.domain)
22
22
  end
23
23
 
24
- def blacklisted?
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 whitelisted?
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
- return dns.getresources(@email.domain, Resolv::DNS::Resource::IN::MX).any?
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
- address = EmailCheck::EmailAddress.new(value)
9
+ unless value.present?
10
+ add_error(record, attribute)
11
+ return
12
+ end
10
13
 
11
- error(record, attribute) && return unless address.format_valid?
14
+ address = EmailCheck::EmailAddress.new(value)
12
15
 
13
- return if address.whitelisted?
16
+ unless address && address.format_valid?
17
+ add_error(record, attribute)
18
+ return
19
+ end
14
20
 
15
- if options[:blocked_usernames]
16
- error(record, attribute) && return if address.blocked_username?
21
+ if options[:block_special_usernames] && address.blocked_username?
22
+ add_error(record, attribute)
23
+ return
17
24
  end
18
25
 
19
- if options[:disposable]
20
- error(record, attribute) && return if address.disposable?
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[:blacklist]
24
- error(record, attribute) && return if address.blacklisted?
33
+ if options[:not_blacklisted] && address.blacklisted_domain?
34
+ add_error(record, attribute)
35
+ return
25
36
  end
26
37
 
27
- if options[:free]
28
- error(record, attribute) && return if address.free?
38
+ if options[:not_free] && address.free_email_provider?
39
+ add_error(record, attribute)
40
+ return
29
41
  end
30
42
 
31
- if options[:mx]
32
- error(record, attribute) && return unless address.domain_has_mx?
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 error(record, attribute)
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
@@ -1,3 +1,3 @@
1
1
  module EmailCheck
2
- VERSION = "0.1.4"
2
+ VERSION = "1.0.0"
3
3
  end
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.1.4
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-04-30 00:00:00.000000000 Z
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