valid_email2 0.0.3 → 0.0.4

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: f1d4fe39e4ead990b22676834dcea2a54e090d9a
4
- data.tar.gz: 82de0c3528c6b6ea1bc10c730b6fa96f4343fbc3
3
+ metadata.gz: 1d3456e7cb1c7feb85fdff2260fd595711cd2a9f
4
+ data.tar.gz: 36f3e8a848db9bbb0abb3ea5c30cd5b85d182f8b
5
5
  SHA512:
6
- metadata.gz: a538c07451106710b27fe29fc5437292be553ed73936fa819fea0bc651e14298e905627642237bf9a2058221247c8f02f5a9449ead73450b8994ddc7abec1d1f
7
- data.tar.gz: 1252e5e86622060ad93538fd4e69db846b2bff6831f5e82f3ef9a0211429fe6fc2f36820c70671f221cfb8f8eacbbdc91c6af0149ee90db0870bdc5f073a31de
6
+ metadata.gz: 2bfb2d8ceaeba93622137e87ebf5fb2c990d04419dd3f5bd0e0f063022459859f2520f617a8ef943c120cc54034f242f946d337e6870ca803c700a410640c61c
7
+ data.tar.gz: 10eabac3bb4f6be94139d7820182598908ec4c721a282148aef741914603555608116d89eae4dd339ef7338df593688e80852381c7d6cef97cc2e6d9ac88f578
data/.travis.yml ADDED
@@ -0,0 +1,12 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 2.0.0
5
+ - 1.9.3
6
+ - jruby-19mode
7
+ - jruby-20mode
8
+ - rbx-19mode
9
+
10
+ gemfile:
11
+ - gemfiles/activemodel3.gemfile
12
+ - gemfiles/activemodel4.gemfile
data/README.md CHANGED
@@ -1,6 +1,11 @@
1
1
  # ValidEmail2
2
+ [![Build Status](https://travis-ci.org/lisinge/valid_email2.png?branch=master)](https://travis-ci.org/lisinge/valid_email2)
3
+ [![Coverage Status](https://coveralls.io/repos/lisinge/valid_email2/badge.png)](https://coveralls.io/r/lisinge/valid_email2)
4
+ [![Code Climate](https://codeclimate.com/github/lisinge/valid_email2.png)](https://codeclimate.com/github/lisinge/valid_email2)
5
+ [![Dependency Status](https://gemnasium.com/lisinge/valid_email2.png)](https://gemnasium.com/lisinge/valid_email2)
2
6
 
3
- TODO: Write a gem description
7
+ Validate emails without regexp but with the help of the `mail` gem and MX server lookup.
8
+ Optionally validate against a list of disposable email domains.
4
9
 
5
10
  ## Installation
6
11
 
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem "activemodel", "~> 3.2.13"
4
+
5
+ gemspec path: "../"
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem "activemodel", "~> 4.0.0"
4
+
5
+ gemspec path: "../"
@@ -0,0 +1,48 @@
1
+ require "resolv"
2
+ require "mail"
3
+
4
+ module ValidEmail2
5
+ class Address
6
+ attr_accessor :address
7
+
8
+ def initialize(address)
9
+ @parse_error = false
10
+
11
+ begin
12
+ @raw_address = address
13
+ @address = Mail::Address.new(address)
14
+ rescue Mail::Field::ParseError
15
+ @parse_error = true
16
+ end
17
+ end
18
+
19
+ def valid?
20
+ return false if @parse_error
21
+
22
+ if address.domain && address.address == @raw_address
23
+ tree = address.send(:tree)
24
+
25
+ # Valid address needs to have a dot in the domain
26
+ tree.domain.dot_atom_text.elements.size > 1
27
+ else
28
+ false
29
+ end
30
+ end
31
+
32
+ def disposable?
33
+ valid? && ValidEmail2.disposable_emails.include?(address.domain)
34
+ end
35
+
36
+ def valid_mx?
37
+ return false unless valid?
38
+
39
+ mx = []
40
+
41
+ Resolv::DNS.open do |dns|
42
+ mx.concat dns.getresources(address.domain, Resolv::DNS::Resource::IN::MX)
43
+ end
44
+
45
+ mx.any?
46
+ end
47
+ end
48
+ end
@@ -1,3 +1,3 @@
1
1
  module ValidEmail2
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
data/lib/valid_email2.rb CHANGED
@@ -1,14 +1,15 @@
1
1
  require "valid_email2/version"
2
+ require "valid_email2/address"
2
3
  require "active_model"
3
4
  require "active_model/validations"
4
- require "resolv"
5
- require "mail"
6
5
 
7
- class EmailValidator < ActiveModel::EachValidator
6
+ module ValidEmail2
8
7
  def self.disposable_emails
9
- @@disposable_emails ||= YAML.load_file(File.expand_path("../../disposable_emails.yml",__FILE__))
8
+ @@disposable_emails ||= YAML.load_file(File.expand_path("../../vendor/disposable_emails.yml",__FILE__))
10
9
  end
10
+ end
11
11
 
12
+ class EmailValidator < ActiveModel::EachValidator
12
13
  def default_options
13
14
  { regex: true, disposable: false, mx: false }
14
15
  end
@@ -17,40 +18,16 @@ class EmailValidator < ActiveModel::EachValidator
17
18
  return unless value.present?
18
19
  options = default_options.merge(self.options)
19
20
 
20
- begin
21
- email = Mail::Address.new(value)
22
- rescue Mail::Field::ParseError
23
- error(record, attribute) && return
24
- return
25
- end
26
-
27
- if email.domain && email.address == value
28
- tree = email.send(:tree)
21
+ address = ValidEmail2::Address.new(value)
29
22
 
30
- # Valid email needs to have a dot in the domain
31
- unless tree.domain.dot_atom_text.elements.size > 1
32
- error(record, attribute) && return
33
- end
34
- else
35
- error(record, attribute) && return
36
- end
23
+ error(record, attribute) && return unless address.valid?
37
24
 
38
25
  if options[:disposable]
39
- if self.class.disposable_emails.include?(email.domain)
40
- error(record, attribute) && return
41
- end
26
+ error(record, attribute) && return if address.disposable?
42
27
  end
43
28
 
44
29
  if options[:mx]
45
- mx = []
46
-
47
- Resolv::DNS.open do |dns|
48
- mx.concat dns.getresources(email.domain, Resolv::DNS::Resource::IN::MX)
49
- end
50
-
51
- unless mx.any?
52
- error(record, attribute) && return
53
- end
30
+ error(record, attribute) && return unless address.valid_mx?
54
31
  end
55
32
  end
56
33
 
data/spec/spec_helper.rb CHANGED
@@ -1,4 +1,6 @@
1
1
  $:.unshift File.expand_path("../lib",__FILE__)
2
+ require "coveralls"
3
+ Coveralls.wear!
2
4
  require "valid_email2"
3
5
 
4
6
  class TestModel
@@ -30,11 +30,6 @@ describe ValidEmail2 do
30
30
  Mail::Address.stub(:new).and_raise(Mail::Field::ParseError.new(nil, nil, nil))
31
31
  user.valid?.should be_false
32
32
  end
33
-
34
- it "blah" do
35
- user = TestUserMX.new(email: "invalid_email.com")
36
- user.valid?.should be_false
37
- end
38
33
  end
39
34
 
40
35
  describe "disposable emails" do
@@ -44,7 +39,7 @@ describe ValidEmail2 do
44
39
  end
45
40
 
46
41
  it "should be invalid when email is in the list of disposable emails" do
47
- user = TestUserDisallowDisposable.new(email: "foo@#{EmailValidator.disposable_emails.first}")
42
+ user = TestUserDisallowDisposable.new(email: "foo@#{ValidEmail2.disposable_emails.first}")
48
43
  user.valid?.should be_false
49
44
  end
50
45
  end
data/valid_email2.gemspec CHANGED
@@ -21,6 +21,7 @@ Gem::Specification.new do |spec|
21
21
  spec.add_development_dependency "bundler", "~> 1.3"
22
22
  spec.add_development_dependency "rake"
23
23
  spec.add_development_dependency "rspec", "~> 2.14.1"
24
+ spec.add_development_dependency "coveralls"
24
25
  spec.add_runtime_dependency "mail"
25
26
  spec.add_runtime_dependency "activemodel"
26
27
  end
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.3
4
+ version: 0.0.4
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 00:00:00.000000000 Z
11
+ date: 2013-07-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ~>
53
53
  - !ruby/object:Gem::Version
54
54
  version: 2.14.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: coveralls
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: mail
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -89,16 +103,20 @@ extensions: []
89
103
  extra_rdoc_files: []
90
104
  files:
91
105
  - .gitignore
106
+ - .travis.yml
92
107
  - Gemfile
93
108
  - LICENSE.txt
94
109
  - README.md
95
110
  - Rakefile
96
- - disposable_emails.yml
111
+ - gemfiles/activemodel3.gemfile
112
+ - gemfiles/activemodel4.gemfile
97
113
  - lib/valid_email2.rb
114
+ - lib/valid_email2/address.rb
98
115
  - lib/valid_email2/version.rb
99
116
  - spec/spec_helper.rb
100
117
  - spec/valid_email2_spec.rb
101
118
  - valid_email2.gemspec
119
+ - vendor/disposable_emails.yml
102
120
  homepage: https://github.com/lisinge/valid_email2
103
121
  licenses:
104
122
  - MIT