rails_email_validator 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -8,6 +8,5 @@ source "http://rubygems.org"
8
8
  group :development do
9
9
  gem "bundler", "~> 1.0.0"
10
10
  gem "jeweler", "~> 1.5.1"
11
- gem "rcov", ">= 0"
12
- gem "reek", "~> 1.2.8"
11
+ gem "idn", :git => "git://github.com/mihu/idn.git"
13
12
  end
data/README.rdoc CHANGED
@@ -9,17 +9,28 @@ If you want to use it as plugin
9
9
 
10
10
  == Using
11
11
  Use to validate email
12
- validate :email => true
12
+ validates :email, :email => true
13
13
 
14
14
  Use email validation without mx validation
15
- validate :email => { :validate_mx => false }
15
+ validates :email, :email => { :validate_mx => false }
16
16
 
17
+ Use email validation without idn support
18
+ validates :email, :email => { :allow_idn => false }
17
19
 
18
20
  == Features
19
21
 
20
22
  * validates local part of email
21
23
  * validates domain part of email
22
24
  * validate domain has a valid mx entry
25
+ * idn support if idn available
26
+ * tests for validator
27
+
28
+ == IDN support
29
+
30
+ If you want to support idn for email, you must install idn.
31
+
32
+ fork with ruby 1.9 support
33
+ gem "idn", :git => "git://github.com/mihu/idn.git"
23
34
 
24
35
  == Contributing to rails_email_validator
25
36
 
data/Rakefile CHANGED
@@ -33,20 +33,6 @@ Rake::TestTask.new(:test) do |test|
33
33
  test.verbose = true
34
34
  end
35
35
 
36
- require 'rcov/rcovtask'
37
- Rcov::RcovTask.new do |test|
38
- test.libs << 'test'
39
- test.pattern = 'test/**/test_*.rb'
40
- test.verbose = true
41
- end
42
-
43
- require 'reek/rake/task'
44
- Reek::Rake::Task.new do |t|
45
- t.fail_on_error = true
46
- t.verbose = false
47
- t.source_files = 'lib/**/*.rb'
48
- end
49
-
50
36
  task :default => :test
51
37
 
52
38
  require 'rake/rdoctask'
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.1.0
@@ -1,12 +1,16 @@
1
-
2
1
  require 'active_model'
3
2
 
3
+ # Validator for email
4
4
  class EmailValidator < ActiveModel::EachValidator
5
5
  # check the options for do mx validation
6
6
  def validate_mx?
7
7
  options[:validate_mx].nil? or options[:validate_mx] == true
8
8
  end
9
9
 
10
+ def allow_idn?
11
+ options[:allow_idn].nil? or options[:allow_idn] == true
12
+ end
13
+
10
14
  # validate if an mx exists on domain
11
15
  def has_mx?(domain)
12
16
  require 'resolv'
@@ -17,18 +21,41 @@ class EmailValidator < ActiveModel::EachValidator
17
21
  not mx.nil? and mx.size > 0
18
22
  end
19
23
 
24
+ def convert_idn(domain_part)
25
+ if allow_idn?
26
+ # idn suport if available
27
+ begin
28
+ require 'idn'
29
+ # encode domain part
30
+ return IDN::Idna.toASCII(domain_part)
31
+ rescue LoadError
32
+ rescue IDN::Idna::IdnaError
33
+ end
34
+ end
35
+ domain_part
36
+ end
37
+
20
38
  # main validator for email
21
39
  def validate_each(record, attribute, value)
22
40
  unless value.blank?
23
- # split local and domain part
24
- (local_part, domain_part) = value.split('@', 2)
25
41
 
26
42
  # pre var
27
43
  valid = true
44
+ local_part = nil
45
+ domain_part = nil
46
+
47
+ if valid
48
+ # split local and domain part
49
+ (local_part, domain_part) = value.to_s.split('@', 2)
50
+ end
28
51
 
29
- # check syntax
30
- valid = false unless local_part =~ /\A[A-Za-z0-9.!\#$%&'*+-\/=?^_`{|}~]+\Z/
31
- valid = false unless domain_part =~ /\A((?:[-a-zA-Z0-9]+\.)+[a-zA-Z]{2,})\Z/
52
+ domain_part = convert_idn domain_part
53
+
54
+ if valid
55
+ # check syntax
56
+ valid = false unless local_part =~ /\A[A-Za-z0-9.!\#$%&'*+-\/=?^_`{|}~]+\Z/
57
+ valid = false unless domain_part =~ /\A((?:[-a-zA-Z0-9]+\.)+[a-zA-Z]{2,})(.|)\Z/
58
+ end
32
59
 
33
60
  # check mx
34
61
  if valid and validate_mx?
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{rails_email_validator}
8
- s.version = "0.0.3"
8
+ s.version = "0.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Marco Scholl"]
12
- s.date = %q{2010-12-08}
12
+ s.date = %q{2011-01-22}
13
13
  s.description = %q{a class to validate emails. it builded for rails}
14
14
  s.email = %q{develop@marco-scholl.de}
15
15
  s.extra_rdoc_files = [
@@ -47,23 +47,20 @@ Gem::Specification.new do |s|
47
47
  s.add_runtime_dependency(%q<activemodel>, [">= 3.0.0"])
48
48
  s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
49
49
  s.add_development_dependency(%q<jeweler>, ["~> 1.5.1"])
50
- s.add_development_dependency(%q<rcov>, [">= 0"])
51
- s.add_development_dependency(%q<reek>, ["~> 1.2.8"])
50
+ s.add_development_dependency(%q<idn>, [">= 0"])
52
51
  s.add_runtime_dependency(%q<activemodel>, [">= 3.0.0"])
53
52
  else
54
53
  s.add_dependency(%q<activemodel>, [">= 3.0.0"])
55
54
  s.add_dependency(%q<bundler>, ["~> 1.0.0"])
56
55
  s.add_dependency(%q<jeweler>, ["~> 1.5.1"])
57
- s.add_dependency(%q<rcov>, [">= 0"])
58
- s.add_dependency(%q<reek>, ["~> 1.2.8"])
56
+ s.add_dependency(%q<idn>, [">= 0"])
59
57
  s.add_dependency(%q<activemodel>, [">= 3.0.0"])
60
58
  end
61
59
  else
62
60
  s.add_dependency(%q<activemodel>, [">= 3.0.0"])
63
61
  s.add_dependency(%q<bundler>, ["~> 1.0.0"])
64
62
  s.add_dependency(%q<jeweler>, ["~> 1.5.1"])
65
- s.add_dependency(%q<rcov>, [">= 0"])
66
- s.add_dependency(%q<reek>, ["~> 1.2.8"])
63
+ s.add_dependency(%q<idn>, [">= 0"])
67
64
  s.add_dependency(%q<activemodel>, [">= 3.0.0"])
68
65
  end
69
66
  end
@@ -1,7 +1,126 @@
1
+ # encoding:utf-8
2
+
1
3
  require 'helper'
2
4
 
5
+ class ValidateEmailWithMx
6
+ include ActiveModel::Validations
7
+ attr_accessor :email
8
+ validates :email, :email => true
9
+ end
10
+
11
+ class ValidateEmailWithoutMx
12
+ include ActiveModel::Validations
13
+ attr_accessor :email
14
+ validates :email, :email => {:validate_mx => false}
15
+ end
16
+
17
+ class ValidateEmailDisabledIdm
18
+ include ActiveModel::Validations
19
+ attr_accessor :email
20
+ validates :email, :email => {:allow_idn => false}
21
+ end
22
+
23
+
3
24
  class TestRailsEmailValidator < Test::Unit::TestCase
4
- def test_something_for_real
5
- flunk "hey buddy, you should probably rename this file and start testing for real"
25
+
26
+
27
+ def test_valid_emails_with_mx_validation
28
+ model = ValidateEmailWithMx.new
29
+
30
+ [
31
+ 'test@marco-scholl.de'
32
+ ].each do |email|
33
+
34
+ model.email = email
35
+ assert model.valid?, ">>#{email}<<"
36
+
37
+ end
38
+ end
39
+
40
+ def test_invalid_emails_with_mx_validation
41
+ model = ValidateEmailWithMx.new
42
+
43
+ [
44
+ 'test@invalidmx.marco-scholl.de'
45
+ ].each do |email|
46
+
47
+ model.email = email
48
+ assert !model.valid?, ">>#{email}<<"
49
+
50
+ end
6
51
  end
52
+
53
+
54
+ def test_valid_emails_without_mx_validation
55
+ model = ValidateEmailWithoutMx.new
56
+
57
+ [
58
+ nil,
59
+ '',
60
+ ' ',
61
+ 'test@example.net',
62
+ 'test@sub1.example.net',
63
+ 'test@sub1.example.co.uk',
64
+ 'test@sub2.sub1.example.net',
65
+ 'test@sub2.sub1.example.tttttttt',
66
+ 'test@mexample.net.'
67
+ ].each do |email|
68
+
69
+ model.email = email
70
+ assert model.valid?, ">>#{email}<<"
71
+
72
+ end
73
+ end
74
+
75
+ def test_invalid_emails_without_mx_validation
76
+ model = ValidateEmailWithoutMx.new
77
+
78
+ [
79
+ 1.2,
80
+ 1,
81
+ 'email',
82
+ 'test@localhost',
83
+ 'müller@example.net',
84
+ 'test@example.net..'
85
+ ].each do |email|
86
+
87
+ model.email = email
88
+ assert !model.valid?, ">>#{email}<<"
89
+
90
+ end
91
+ end
92
+
93
+ def test_idn_adresses
94
+ model = ValidateEmailWithoutMx.new
95
+
96
+ [
97
+ 'mueller@müller.de',
98
+ 'test@räksmörgås.nu',
99
+ 'test@sub1.räksmörgås.nu',
100
+ 'test@xn--rksmrgs-5wao1o.nu',
101
+ 'test@sub1.xn--rksmrgs-5wao1o.nu'
102
+ ].each do |email|
103
+
104
+ model.email = email
105
+ assert model.valid?, ">>#{email}<<"
106
+ end
107
+ end
108
+
109
+ def test_idn_with_disabled_idn_support
110
+ model = ValidateEmailDisabledIdm.new
111
+
112
+ [
113
+ 'mueller@müller.de',
114
+ 'test@räksmörgås.nu',
115
+ 'test@sub1.räksmörgås.nu',
116
+ 'test@xn--rksmrgs-5wao1o.nu',
117
+ 'test@sub1.xn--rksmrgs-5wao1o.nu'
118
+ ].each do |email|
119
+
120
+ model.email = email
121
+ assert !model.valid?, ">>#{email}<<"
122
+ end
123
+ end
124
+
125
+
7
126
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
+ - 1
7
8
  - 0
8
- - 3
9
- version: 0.0.3
9
+ version: 0.1.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Marco Scholl
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-12-08 00:00:00 +01:00
17
+ date: 2011-01-22 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -63,7 +63,7 @@ dependencies:
63
63
  prerelease: false
64
64
  version_requirements: *id003
65
65
  - !ruby/object:Gem::Dependency
66
- name: rcov
66
+ name: idn
67
67
  requirement: &id004 !ruby/object:Gem::Requirement
68
68
  none: false
69
69
  requirements:
@@ -75,24 +75,9 @@ dependencies:
75
75
  type: :development
76
76
  prerelease: false
77
77
  version_requirements: *id004
78
- - !ruby/object:Gem::Dependency
79
- name: reek
80
- requirement: &id005 !ruby/object:Gem::Requirement
81
- none: false
82
- requirements:
83
- - - ~>
84
- - !ruby/object:Gem::Version
85
- segments:
86
- - 1
87
- - 2
88
- - 8
89
- version: 1.2.8
90
- type: :development
91
- prerelease: false
92
- version_requirements: *id005
93
78
  - !ruby/object:Gem::Dependency
94
79
  name: activemodel
95
- requirement: &id006 !ruby/object:Gem::Requirement
80
+ requirement: &id005 !ruby/object:Gem::Requirement
96
81
  none: false
97
82
  requirements:
98
83
  - - ">="
@@ -104,7 +89,7 @@ dependencies:
104
89
  version: 3.0.0
105
90
  type: :runtime
106
91
  prerelease: false
107
- version_requirements: *id006
92
+ version_requirements: *id005
108
93
  description: a class to validate emails. it builded for rails
109
94
  email: develop@marco-scholl.de
110
95
  executables: []
@@ -140,7 +125,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
140
125
  requirements:
141
126
  - - ">="
142
127
  - !ruby/object:Gem::Version
143
- hash: 4213132792556836098
128
+ hash: -2629671929889912192
144
129
  segments:
145
130
  - 0
146
131
  version: "0"