active-model-email-validator 1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,2 @@
1
+ .yardoc
2
+ doc
@@ -0,0 +1 @@
1
+ --charset UTF-8 --markup markdown lib/**/*.rb - LICENSE
data/LICENSE ADDED
@@ -0,0 +1,10 @@
1
+ The MIT License
2
+ ===============
3
+
4
+ © 2011 Cody Robbins
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
7
+
8
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
9
+
10
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,51 @@
1
+ Active Model Email Validator
2
+ ============================
3
+
4
+ This is a simple ActiveModel validator for email addresses built on top of the [Mail gem](http://rubygems.org/gems/mail). Instead of trying to devise an overly complex custom (and probably incorrect) regular expression to validate email addresses that is compliant with RFC 5321/5322—a difficult task—it instead relies on the Mail gem to parse the address. Since the Mail gem is an actively maintained library for working with email, if it can’t deal with an address it’s probably not worth attempting to send to anyways.
5
+
6
+ An additional check is performed to ensure that the domain name in the address has at least two components—that is, a top-level domain and one subdomain. The validator purposefully errs on the side of inclusivity rather than exclusivity: it might allow some invalid email addresses, but it hopefully doesn’t disallow valid addresses.
7
+
8
+ Full documentation is at [RubyDoc.info](http://rubydoc.info/gems/active-model-email-validator).
9
+
10
+ Example
11
+ -------
12
+
13
+ The following model uses `ActiveModel::Validations::PresenceValidator` and `ActiveRecord::Validations::UniquenessValidator` to ensure the presence and uniqueness of the user’s email attribute. The third line uses `EmailValidator` to check that the email address is valid.
14
+
15
+ class User < ActiveRecord::Base
16
+ validates(:email,
17
+ :presence => {:message => "Email can't be blank"},
18
+ :uniqueness => {:message => 'Email must be unique'},
19
+ :email => {:message => 'Email must be valid'})
20
+ end
21
+
22
+ Note
23
+ ----
24
+
25
+ Currently there isn’t a `validates_email` class-level helper method because I’ve never had the need for it in practice, but I may add one at some point.
26
+
27
+ Colophon
28
+ --------
29
+
30
+ ### See also
31
+
32
+ If you like this gem, you may also want to check out [Static Model](http://codyrobbins.com/software/static-model), [Declarative Find](http://codyrobbins.com/software/declarative-find), [Create New](http://codyrobbins.com/software/create-new), [Save Changes To](http://codyrobbins.com/software/save-changes-to), and [HTTP Error](http://codyrobbins.com/software/http-error).
33
+
34
+ ### Tested with
35
+
36
+ * ActiveModel 3.0.5 — 20 May 2011
37
+
38
+ ### Contributing
39
+
40
+ * [Source](https://github.com/codyrobbins/active-model-email-validator)
41
+ * [Bug reports](https://github.com/codyrobbins/active-model-email-validator/issues)
42
+
43
+ To send patches, please fork on GitHub and submit a pull request.
44
+
45
+ ### Credits
46
+
47
+ © 2011 [Cody Robbins](http://codyrobbins.com/). See LICENSE for details.
48
+
49
+ * [Homepage](http://codyrobbins.com/software/active-model-email-validator)
50
+ * [My other gems](http://codyrobbins.com/software#gems)
51
+ * [Follow me on Twitter](http://twitter.com/codyrobbins)
@@ -0,0 +1,20 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'active-model-email-validator'
3
+ s.version = '1.0'
4
+ s.summary = 'An ActiveModel email validator based on the Mail gem.'
5
+ s.homepage = 'http://codyrobbins.com/software/active-model-email-validator'
6
+ s.author = 'Cody Robbins'
7
+ s.email = 'cody@codyrobbins.com'
8
+
9
+ s.post_install_message = '
10
+ -------------------------------------------------------------
11
+ Follow me on Twitter! http://twitter.com/codyrobbins
12
+ -------------------------------------------------------------
13
+
14
+ '
15
+
16
+ s.files = `git ls-files`.split
17
+
18
+ s.add_dependency('activemodel')
19
+ s.add_dependency('mail')
20
+ end
@@ -0,0 +1,64 @@
1
+ # encoding: UTF-8
2
+
3
+ # This is a simple ActiveModel validator for email addresses built on top of the [Mail gem](http://rubygems.org/gems/mail). Instead of trying to devise an overly complex custom (and probably incorrect) regular expression to validate email addresses that is compliant with RFC 5321/5322—a difficult task—it instead relies on the Mail gem to parse the address. Since the Mail gem is an actively maintained library for working with email, if it can’t deal with an address it’s probably not worth attempting to send to anyways.
4
+ #
5
+ # An additional check is performed to ensure that the domain name in the address has at least two components—that is, a top-level domain and one subdomain. The validator purposefully errs on the side of inclusivity rather than exclusivity: it might allow some invalid email addresses, but it hopefully doesn’t disallow valid addresses.
6
+ #
7
+ # Example
8
+ # -------
9
+ #
10
+ # The following model uses `ActiveModel::Validations::PresenceValidator` and `ActiveRecord::Validations::UniquenessValidator` to ensure the presence and uniqueness of the user’s email attribute. The third line uses `EmailValidator` to check that the email address is valid.
11
+ #
12
+ # class User < ActiveRecord::Base
13
+ # validates(:email,
14
+ # :presence => {:message => "Email can't be blank"},
15
+ # :uniqueness => {:message => 'Email must be unique'},
16
+ # :email => {:message => 'Email must be valid'})
17
+ # end
18
+ class EmailValidator < ActiveModel::EachValidator
19
+ # The interface that `ActiveModel::Validations` uses to invoke the validation. This method is not called directly.
20
+ def validate_each(object, attribute, value)
21
+ add_error_to(object, attribute) unless valid?(value)
22
+ end
23
+
24
+ protected
25
+
26
+ def valid?(value)
27
+ # Allow blank values to pass through.
28
+ return(true) if value.blank?
29
+
30
+ # Disallow unparseable addresses.
31
+ begin
32
+ parse_address(value)
33
+ rescue unparseable_address
34
+ return(false)
35
+ end
36
+
37
+ # Make sure the domain in the address is reasonable.
38
+ domain_present? && domain_has_more_than_one_component?
39
+ end
40
+
41
+ def add_error_to(object, attribute)
42
+ object.errors[attribute] << error_message
43
+ end
44
+
45
+ def error_message
46
+ options[:message] || 'is not a valid email'
47
+ end
48
+
49
+ def parse_address(value)
50
+ @address = Mail::Address.new(value)
51
+ end
52
+
53
+ def unparseable_address
54
+ Mail::Field::ParseError
55
+ end
56
+
57
+ def domain_present?
58
+ @address.domain.present?
59
+ end
60
+
61
+ def domain_has_more_than_one_component?
62
+ @address.send(:tree).domain.dot_atom_text.elements.size > 1
63
+ end
64
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active-model-email-validator
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: "1.0"
6
+ platform: ruby
7
+ authors:
8
+ - Cody Robbins
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-05-31 00:00:00 -04:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: activemodel
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: mail
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: "0"
36
+ type: :runtime
37
+ version_requirements: *id002
38
+ description:
39
+ email: cody@codyrobbins.com
40
+ executables: []
41
+
42
+ extensions: []
43
+
44
+ extra_rdoc_files: []
45
+
46
+ files:
47
+ - .gitignore
48
+ - .yardopts
49
+ - LICENSE
50
+ - README.markdown
51
+ - active-model-email-validator.gemspec
52
+ - lib/active-model-email-validator.rb
53
+ has_rdoc: true
54
+ homepage: http://codyrobbins.com/software/active-model-email-validator
55
+ licenses: []
56
+
57
+ post_install_message: "\n\
58
+ -------------------------------------------------------------\n\
59
+ Follow me on Twitter! http://twitter.com/codyrobbins\n\
60
+ -------------------------------------------------------------\n\n"
61
+ rdoc_options: []
62
+
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: "0"
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: "0"
77
+ requirements: []
78
+
79
+ rubyforge_project:
80
+ rubygems_version: 1.6.2
81
+ signing_key:
82
+ specification_version: 3
83
+ summary: An ActiveModel email validator based on the Mail gem.
84
+ test_files: []
85
+