valid_email 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/.rvmrc CHANGED
@@ -1 +1 @@
1
- rvm use --create ruby-1.9.2@email_validator
1
+ rvm use --create ruby-1.9.3@email_validator
data/README.md CHANGED
@@ -55,6 +55,29 @@ Or in your Gemfile
55
55
  gem 'valid_email', :require => 'valid_email/email_validator'
56
56
 
57
57
 
58
+ ### Usage outside of model validation
59
+ There is a chance that you want to use e-mail validator outside of model validation.
60
+ If that's the case, you can use the following methods:
61
+
62
+ ```ruby
63
+ ValidateEmail.valid?('email@randommail.com') # You can optionally pass a hash of options, same as validator
64
+ ValidateEmail.mx_valid?('email@randommail.com')
65
+ ValidateEmail.mx_valid_with_fallback?('email@randommail.com')
66
+ ValidateEmail.valid?('email@randommail.com')
67
+ ```
68
+
69
+ There is also a String and Nil class extension, if you require the gem in this way in Gemfile:
70
+
71
+ ```ruby
72
+ gem 'valid_email', require: ['valid_email/all_with_extensions']
73
+ ```
74
+
75
+ You will be able to use the following methods:
76
+ ```ruby
77
+ nil.email? # => false
78
+ "jhon@gmail.com".email? # => May return true if it exists. It accepts a hash of options like ValidateEmail.valid?
79
+ ```
80
+
58
81
  # Credits
59
82
 
60
83
  * Dush dusanek[at]iquest.cz
data/Rakefile CHANGED
@@ -7,4 +7,4 @@ RSpec::Core::RakeTask.new do |t|
7
7
  end
8
8
 
9
9
  task :default => [:spec]
10
- task :build => [:spec]
10
+ task :build
@@ -0,0 +1,5 @@
1
+ fi:
2
+ valid_email:
3
+ validations:
4
+ email:
5
+ invalid: ei ole kelvollinen
@@ -1,3 +1,4 @@
1
+ require 'valid_email/validate_email'
1
2
  require 'valid_email/email_validator'
2
3
  require 'valid_email/mx_validator'
3
4
  require 'valid_email/mx_with_fallback_validator'
@@ -0,0 +1,16 @@
1
+ require 'valid_email'
2
+ class String
3
+
4
+ def email?(options={})
5
+ ValidateEmail.valid?(self, options)
6
+ end
7
+
8
+ end
9
+
10
+ class NilClass
11
+
12
+ def email?(options={})
13
+ false
14
+ end
15
+
16
+ end
@@ -1,19 +1,24 @@
1
1
  require 'active_model'
2
2
  require 'active_model/validations'
3
3
  require 'mail'
4
+ require 'valid_email/validate_email'
4
5
  class BanDisposableEmailValidator < ActiveModel::EachValidator
5
6
  # A list of disposable email domains
6
7
  def self.config=(options)
7
8
  @@config = options
8
9
  end
9
10
 
11
+ # Required to use config outside
12
+ def self.config
13
+ @@config = [] unless defined? @@config
14
+
15
+ @@config
16
+ end
17
+
10
18
  def validate_each(record, attribute, value)
11
- begin
12
- m = Mail::Address.new(value)
13
- r = !@@config.include?(m.domain) if m.domain
14
- rescue Exception => e
15
- r = false
16
- end
19
+ r = ValidateEmail.ban_disposable_email?(value)
17
20
  record.errors.add attribute, (options[:message] || I18n.t(:invalid, :scope => "valid_email.validations.email")) unless r
21
+
22
+ r
18
23
  end
19
24
  end
@@ -1,23 +1,14 @@
1
1
  require 'active_model'
2
2
  require 'active_model/validations'
3
3
  require 'mail'
4
+ require 'valid_email/validate_email'
4
5
  class EmailValidator < ActiveModel::EachValidator
5
6
  def validate_each(record,attribute,value)
6
7
  begin
7
8
  return if options[:allow_nil] && value.nil?
8
9
  return if options[:allow_blank] && value.blank?
9
10
 
10
- m = Mail::Address.new(value)
11
- # We must check that value contains a domain and that value is an email address
12
- r = m.domain && m.address == value
13
- t = m.__send__(:tree)
14
- # We need to dig into treetop
15
- # A valid domain must have dot_atom_text elements size > 1
16
- # user@localhost is excluded
17
- # treetop must respond to domain
18
- # We exclude valid email values like <user@localhost.com>
19
- # Hence we use m.__send__(tree).domain
20
- r &&= (t.domain.dot_atom_text.elements.size > 1)
11
+ r = ValidateEmail.valid?(value)
21
12
  # Check if domain has DNS MX record
22
13
  if r && options[:mx]
23
14
  require 'valid_email/mx_validator'
@@ -2,24 +2,12 @@ require 'active_model'
2
2
  require 'active_model/validations'
3
3
  require 'mail'
4
4
  require 'resolv'
5
+ require 'valid_email/validate_email'
5
6
  class MxValidator < ActiveModel::EachValidator
6
7
  def validate_each(record,attribute,value)
7
- begin
8
- m = Mail::Address.new(value)
9
- if m.domain
10
- r = valid_domain?(m.domain)
11
- end
12
- rescue Mail::Field::ParseError
13
- #ignore this
14
- end
8
+ r = ValidateEmail.mx_valid?(value)
15
9
  record.errors.add attribute, (options[:message] || I18n.t(:invalid, :scope => "valid_email.validations.email")) unless r
16
- end
17
10
 
18
- def valid_domain?(domain)
19
- mx = []
20
- Resolv::DNS.open do |dns|
21
- mx.concat dns.getresources(domain, Resolv::DNS::Resource::IN::MX)
22
- end
23
- mx.size > 0
11
+ r
24
12
  end
25
13
  end
@@ -1,11 +1,10 @@
1
1
  require 'valid_email/mx_validator'
2
- class MxWithFallbackValidator < MxValidator
3
- def valid_domain?(domain)
4
- mx = []
5
- Resolv::DNS.open do |dns|
6
- mx.concat dns.getresources(domain, Resolv::DNS::Resource::IN::MX)
7
- mx.concat dns.getresources(domain, Resolv::DNS::Resource::IN::A)
8
- end
9
- mx.size > 0
2
+ require 'valid_email/validate_email'
3
+ class MxWithFallbackValidator < ActiveModel::EachValidator
4
+ def validate_each(record,attribute,value)
5
+ r = ValidateEmail.mx_valid_with_fallback?(value)
6
+ record.errors.add attribute, (options[:message] || I18n.t(:invalid, :scope => "valid_email.validations.email")) unless r
7
+
8
+ r
10
9
  end
11
10
  end
@@ -0,0 +1,67 @@
1
+ class ValidateEmail
2
+
3
+ class << self
4
+
5
+ def valid?(value, user_options={})
6
+ options = { :mx => false, :message => nil }.merge!(user_options)
7
+ r = false
8
+ begin
9
+ m = Mail::Address.new(value)
10
+ # We must check that value contains a domain and that value is an email address
11
+ r = m.domain && m.address == value
12
+ t = m.__send__(:tree)
13
+ # We need to dig into treetop
14
+ # A valid domain must have dot_atom_text elements size > 1
15
+ # user@localhost is excluded
16
+ # treetop must respond to domain
17
+ # We exclude valid email values like <user@localhost.com>
18
+ # Hence we use m.__send__(tree).domain
19
+ r &&= (t.domain.dot_atom_text.elements.size > 1)
20
+ # Check if domain has DNS MX record
21
+ if r && options[:mx]
22
+ require 'valid_email/mx_validator'
23
+ r &&= mx_valid?(email)
24
+ end
25
+ rescue Exception => e
26
+ r = false
27
+ end
28
+ r
29
+ end
30
+
31
+ def mx_valid?(value, fallback=false)
32
+ r = false
33
+ begin
34
+ m = Mail::Address.new(value)
35
+ if m.domain
36
+ mx = []
37
+ Resolv::DNS.open do |dns|
38
+ mx.concat dns.getresources(m.domain, Resolv::DNS::Resource::IN::MX)
39
+ mx.concat dns.getresources(m.domain, Resolv::DNS::Resource::IN::A) if fallback
40
+ end
41
+ r = mx.size > 0
42
+ end
43
+ rescue Exception =>e
44
+ r = false
45
+ end
46
+ r
47
+ end
48
+
49
+ def mx_valid_with_fallback?(value)
50
+ mx_valid?(value, true)
51
+ end
52
+
53
+ def ban_disposable_email?(value)
54
+ r = false
55
+ begin
56
+ m = Mail::Address.new(value)
57
+ r = !BanDisposableEmailValidator.config.include?(m.domain) if m.domain
58
+ rescue Exception => e
59
+ r = false
60
+ end
61
+
62
+ r
63
+ end
64
+
65
+ end
66
+
67
+ end
@@ -1 +1 @@
1
- ValidEmailVersion = "0.0.5"
1
+ ValidEmailVersion = "0.0.6"
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+ require 'valid_email/all_with_extensions'
3
+
4
+ describe String do
5
+
6
+ it { "mymail@gmail".should respond_to(:email?) }
7
+
8
+ it "is a valid e-mail" do
9
+ "mymail@gmail.com".email?.should be_true
10
+ end
11
+
12
+ it "is not valid when text is not a real e-mail" do
13
+ "invalidMail".email?.should be_false
14
+ end
15
+
16
+ context "when nil" do
17
+
18
+ it "is invalid e-mail" do
19
+ nil.email?.should be_false
20
+ end
21
+
22
+ end
23
+
24
+ end
metadata CHANGED
@@ -1,69 +1,78 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: valid_email
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
+ prerelease:
5
6
  platform: ruby
6
7
  authors:
7
8
  - Ramihajamalala Hery
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2014-05-30 00:00:00.000000000 Z
12
+ date: 2014-08-02 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: rspec
15
16
  requirement: !ruby/object:Gem::Requirement
17
+ none: false
16
18
  requirements:
17
- - - ">="
19
+ - - ! '>='
18
20
  - !ruby/object:Gem::Version
19
21
  version: '0'
20
22
  type: :development
21
23
  prerelease: false
22
24
  version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
23
26
  requirements:
24
- - - ">="
27
+ - - ! '>='
25
28
  - !ruby/object:Gem::Version
26
29
  version: '0'
27
30
  - !ruby/object:Gem::Dependency
28
31
  name: rake
29
32
  requirement: !ruby/object:Gem::Requirement
33
+ none: false
30
34
  requirements:
31
- - - ">="
35
+ - - ! '>='
32
36
  - !ruby/object:Gem::Version
33
37
  version: '0'
34
38
  type: :development
35
39
  prerelease: false
36
40
  version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
37
42
  requirements:
38
- - - ">="
43
+ - - ! '>='
39
44
  - !ruby/object:Gem::Version
40
45
  version: '0'
41
46
  - !ruby/object:Gem::Dependency
42
47
  name: mail
43
48
  requirement: !ruby/object:Gem::Requirement
49
+ none: false
44
50
  requirements:
45
- - - ">="
51
+ - - ! '>='
46
52
  - !ruby/object:Gem::Version
47
53
  version: '0'
48
54
  type: :runtime
49
55
  prerelease: false
50
56
  version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
51
58
  requirements:
52
- - - ">="
59
+ - - ! '>='
53
60
  - !ruby/object:Gem::Version
54
61
  version: '0'
55
62
  - !ruby/object:Gem::Dependency
56
63
  name: activemodel
57
64
  requirement: !ruby/object:Gem::Requirement
65
+ none: false
58
66
  requirements:
59
- - - ">="
67
+ - - ! '>='
60
68
  - !ruby/object:Gem::Version
61
69
  version: '0'
62
70
  type: :runtime
63
71
  prerelease: false
64
72
  version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
65
74
  requirements:
66
- - - ">="
75
+ - - ! '>='
67
76
  - !ruby/object:Gem::Version
68
77
  version: '0'
69
78
  description: ActiveModel Validation for email
@@ -73,49 +82,55 @@ executables: []
73
82
  extensions: []
74
83
  extra_rdoc_files: []
75
84
  files:
76
- - ".gitignore"
77
- - ".rvmrc"
85
+ - .gitignore
86
+ - .rvmrc
78
87
  - Gemfile
79
88
  - LICENSE
80
89
  - README.md
81
90
  - Rakefile
82
91
  - config/locales/de.yml
83
92
  - config/locales/en.yml
93
+ - config/locales/fi.yml
84
94
  - config/locales/fr.yml
85
95
  - config/valid_email.yml
86
96
  - lib/valid_email.rb
87
97
  - lib/valid_email/all.rb
98
+ - lib/valid_email/all_with_extensions.rb
88
99
  - lib/valid_email/ban_disposable_email_validator.rb
89
100
  - lib/valid_email/email_validator.rb
90
101
  - lib/valid_email/mx_validator.rb
91
102
  - lib/valid_email/mx_with_fallback_validator.rb
103
+ - lib/valid_email/validate_email.rb
92
104
  - lib/valid_email/version.rb
93
105
  - spec/email_validator_spec.rb
106
+ - spec/extensions_validator_spec.rb
94
107
  - spec/spec_helper.rb
95
108
  - valid_email.gemspec
96
109
  homepage: http://my.rails-royce.org/2010/07/21/email-validation-in-ruby-on-rails-without-regexp
97
110
  licenses: []
98
- metadata: {}
99
111
  post_install_message:
100
112
  rdoc_options: []
101
113
  require_paths:
102
114
  - lib
103
115
  required_ruby_version: !ruby/object:Gem::Requirement
116
+ none: false
104
117
  requirements:
105
- - - ">="
118
+ - - ! '>='
106
119
  - !ruby/object:Gem::Version
107
120
  version: '0'
108
121
  required_rubygems_version: !ruby/object:Gem::Requirement
122
+ none: false
109
123
  requirements:
110
- - - ">="
124
+ - - ! '>='
111
125
  - !ruby/object:Gem::Version
112
126
  version: '0'
113
127
  requirements: []
114
128
  rubyforge_project: valid_email
115
- rubygems_version: 2.2.2
129
+ rubygems_version: 1.8.23.2
116
130
  signing_key:
117
- specification_version: 4
131
+ specification_version: 3
118
132
  summary: ActiveModel Validation for email
119
133
  test_files:
120
134
  - spec/email_validator_spec.rb
135
+ - spec/extensions_validator_spec.rb
121
136
  - spec/spec_helper.rb
checksums.yaml DELETED
@@ -1,7 +0,0 @@
1
- ---
2
- SHA1:
3
- metadata.gz: 0837471ae7d6496eb96b6270bda8a0244dca52c1
4
- data.tar.gz: 75c8181d2ec9d649306ea8c1a4d914192fab196b
5
- SHA512:
6
- metadata.gz: bd6495a472174bdc871f7a42d867693a8c55e5986f8d65d3669fa4df8f6f2ad78bbcea6549dfb47e753461114eb215482e33c5fed29e2c3a5d974aea26169ac9
7
- data.tar.gz: bf8813115c60c09de4c7fbd2d0d6bfacdce156ee564f3d6aaa16c50afd48f718a90a2739b5459a37aaf03762e3dea9a2253d596eaa6b19c586f134e47bb830d8