valid_email 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -32,7 +32,26 @@ In your code :
32
32
  p.email = "John Does <john@doe.com>"
33
33
  p.valid? # => false
34
34
 
35
+ You can check if email domain has MX record :
35
36
 
37
+ validates :email, :email => {:mx => true}
38
+
39
+ Or
40
+ validates :email, :email => true, :mx => true
41
+
42
+
43
+ If you don't want the MX validator stuff, just require the right file
44
+
45
+ require 'valid\_email/email_validator'
46
+
47
+ Or in your Gemfile
48
+
49
+ gem 'valid_email', :require => 'valid_email/email_validator'
50
+
51
+
52
+ # Credits
53
+
54
+ * Dush dusanek[at]iquest.cz
36
55
 
37
56
  # Note on Patches/Pull Requests
38
57
 
@@ -1,23 +1,2 @@
1
- require 'active_model'
2
- require 'active_model/validations'
3
- require 'mail'
4
- class EmailValidator < ActiveModel::EachValidator
5
- def validate_each(record,attribute,value)
6
- begin
7
- m = Mail::Address.new(value)
8
- # We must check that value contains a domain and that value is an email address
9
- r = m.domain && m.address == value
10
- t = m.__send__(:tree)
11
- # We need to dig into treetop
12
- # A valid domain must have dot_atom_text elements size > 1
13
- # user@localhost is excluded
14
- # treetop must respond to domain
15
- # We exclude valid email values like <user@localhost.com>
16
- # Hence we use m.__send__(tree).domain
17
- r &&= (t.domain.dot_atom_text.elements.size > 1)
18
- rescue Exception => e
19
- r = false
20
- end
21
- record.errors[attribute] << (options[:message] || "is invalid") unless r
22
- end
23
- end
1
+ require 'valid_email/all'
2
+
@@ -0,0 +1,2 @@
1
+ require 'valid_email/email_validator'
2
+ require 'valid_email/mx_validator'
@@ -0,0 +1,28 @@
1
+ require 'active_model'
2
+ require 'active_model/validations'
3
+ require 'mail'
4
+ class EmailValidator < ActiveModel::EachValidator
5
+ def validate_each(record,attribute,value)
6
+ begin
7
+ m = Mail::Address.new(value)
8
+ # We must check that value contains a domain and that value is an email address
9
+ r = m.domain && m.address == value
10
+ t = m.__send__(:tree)
11
+ # We need to dig into treetop
12
+ # A valid domain must have dot_atom_text elements size > 1
13
+ # user@localhost is excluded
14
+ # treetop must respond to domain
15
+ # We exclude valid email values like <user@localhost.com>
16
+ # Hence we use m.__send__(tree).domain
17
+ r &&= (t.domain.dot_atom_text.elements.size > 1)
18
+ # Check if domain has DNS MX record
19
+ if r && options[:mx]
20
+ require 'valid_email/mx_validator'
21
+ r &&= MxValidator.new(:attributes => attributes).validate(record)
22
+ end
23
+ rescue Exception => e
24
+ r = false
25
+ end
26
+ record.errors.add attribute, (options[:message] || "is invalid") unless r
27
+ end
28
+ end
@@ -0,0 +1,16 @@
1
+ require 'active_model'
2
+ require 'active_model/validations'
3
+ require 'mail'
4
+ require 'resolv'
5
+ class MxValidator < ActiveModel::EachValidator
6
+ def validate_each(record,attribute,value)
7
+ m = Mail::Address.new(value)
8
+ mx = []
9
+ Resolv::DNS.open do |dns|
10
+ mx.concat dns.getresources(m.domain, Resolv::DNS::Resource::IN::MX)
11
+ end
12
+ r = mx.size > 0
13
+ record.errors.add attribute, (options[:message] || "is invalid") unless r
14
+ r
15
+ end
16
+ end
@@ -1,2 +1,2 @@
1
- ValidEmailVersion = "0.0.1"
1
+ ValidEmailVersion = "0.0.2"
2
2
 
@@ -47,6 +47,34 @@ describe EmailValidator do
47
47
  subject.valid?.should be_false
48
48
  subject.errors[:email].should == [ "is invalid" ]
49
49
  end
50
+ end
51
+
52
+ person_class_mx = Class.new do
53
+ include ActiveModel::Validations
54
+ attr_accessor :email
55
+ validates :email, :email => {:mx => true}
56
+ end
57
+
58
+ describe "validating email with MX" do
59
+ subject { person_class_mx.new }
60
+
61
+ it "should pass when email domain has MX record" do
62
+ subject.email = 'john@gmail.com'
63
+ subject.valid?.should be_true
64
+ subject.errors[:email].should be_empty
65
+ end
66
+
67
+ it "should fail when email domain has no MX record" do
68
+ subject.email = 'john@subdomain.rubyonrails.org'
69
+ subject.valid?.should be_false
70
+ subject.errors[:email].should == [ "is invalid" ]
71
+ end
72
+
73
+ it "should fail when domain does not exists" do
74
+ subject.email = 'john@nonexistentdomain.abc'
75
+ subject.valid?.should be_false
76
+ subject.errors[:email].should == [ "is invalid" ]
77
+ end
50
78
 
51
79
  end
52
80
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: valid_email
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-09 00:00:00.000000000Z
12
+ date: 2011-12-09 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &18108960 !ruby/object:Gem::Requirement
16
+ requirement: &70131339816040 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *18108960
24
+ version_requirements: *70131339816040
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &18108020 !ruby/object:Gem::Requirement
27
+ requirement: &70131339815620 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *18108020
35
+ version_requirements: *70131339815620
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: mail
38
- requirement: &18107240 !ruby/object:Gem::Requirement
38
+ requirement: &70131339815200 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *18107240
46
+ version_requirements: *70131339815200
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: activemodel
49
- requirement: &18106280 !ruby/object:Gem::Requirement
49
+ requirement: &70131339814780 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: '0'
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *18106280
57
+ version_requirements: *70131339814780
58
58
  description: ActiveModel Validation for email
59
59
  email:
60
60
  - hery@rails-royce.org
@@ -68,6 +68,9 @@ files:
68
68
  - README.md
69
69
  - Rakefile
70
70
  - lib/valid_email.rb
71
+ - lib/valid_email/all.rb
72
+ - lib/valid_email/email_validator.rb
73
+ - lib/valid_email/mx_validator.rb
71
74
  - lib/valid_email/version.rb
72
75
  - spec/email_validator_spec.rb
73
76
  - spec/spec_helper.rb
@@ -84,18 +87,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
84
87
  - - ! '>='
85
88
  - !ruby/object:Gem::Version
86
89
  version: '0'
87
- segments:
88
- - 0
89
- hash: -211291560077668896
90
90
  required_rubygems_version: !ruby/object:Gem::Requirement
91
91
  none: false
92
92
  requirements:
93
93
  - - ! '>='
94
94
  - !ruby/object:Gem::Version
95
95
  version: '0'
96
- segments:
97
- - 0
98
- hash: -211291560077668896
99
96
  requirements: []
100
97
  rubyforge_project: valid_email
101
98
  rubygems_version: 1.8.6