activemodel-email_address_validator 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ab1d7fc08cf4665a14bb017c968b338df6561cad
4
+ data.tar.gz: 90f10888639066f3c41ae8f8a605766c3a2e8607
5
+ SHA512:
6
+ metadata.gz: 280ca906a98d1ae8d3fa525811402be9cf40a2e061a109bac5dfc0e023c7017a4324e9d44f746f2d194eb2ecfdd5201f391aec3ff32ade335d5329a479d9ade3
7
+ data.tar.gz: e8c5f219aff734af345a3265537492a87f204354ddac415bf7d642c6de930b5c620abf97096ff500a569c766d784ca9a15b4e0cfe14f9f1131b1d556db78fb9e
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.travis.yml ADDED
@@ -0,0 +1,13 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 2.1.7
5
+ - 2.2.0
6
+ - 2.2.1
7
+ - 2.2.2
8
+ - 2.2.3
9
+ - jruby-9.0.0.0
10
+
11
+ notifications:
12
+ recipients:
13
+ - jakob@mentalized.net
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in activemodel-email_address_validator.gemspec
4
+ gemspec
5
+
6
+ gem "coveralls", :require => false
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Jakob Skjerning
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,79 @@
1
+ # Email Address Validator
2
+ ## ActiveModel-style email address format validator
3
+
4
+ [![Build Status](https://travis-ci.org/substancelab/activemodel-email_address_validator.svg?branch=master)](https://travis-ci.org/substancelab/activemodel-email_address_validator) [![Code Climate](https://codeclimate.com/github/substancelab/activemodel-email_address_validator/badges/gpa.svg)](https://codeclimate.com/github/substancelab/activemodel-email_address_validator) [![Coverage Status](https://coveralls.io/repos/substancelab/activemodel-email_address_validator/badge.svg?branch=more-badges&service=github)](https://coveralls.io/github/substancelab/activemodel-email_address_validator?branch=more-badges) [![Dependency Status](https://gemnasium.com/substancelab/activemodel-email_address_validator.svg)](https://gemnasium.com/substancelab/activemodel-email_address_validator)
5
+
6
+ Whenever I have wanted to validate an email address it has been because I wanted to be somewhat certain I can send an email to someone. Usually this happens as part of a signup procedure.
7
+
8
+ At this point I have pretty much one criteria:
9
+
10
+ * Don't reject a valid email address realistically in use by a potential user. Err on the side of accepting too much.
11
+
12
+ Quite frankly, I don't care about the RFC at this point, neither does the user. I care that my users can enter their email address and get on with using my product. I appreciate it if the application catches any misspellings of their email addresses, though - this is the opportune moment for them to correct it.
13
+
14
+ ## Requirements
15
+
16
+ * Should not accept local email addresses: No user ever needed to sign up using `"postmaster@localhost"` or `"bob@1.2.3.4"` even though they are perfectly valid email addresses.
17
+ * Must work with I18n like Rails' built-in validators do. If not configured otherwise, the default translation key must be `:invalid`.
18
+
19
+ ## Usage examples
20
+
21
+ ### Simplest case
22
+
23
+ validates :email, :email_address => true
24
+
25
+ ### Bring your own regex
26
+
27
+ If you want to use a specific regular expression:
28
+
29
+ validates :email, :email_address => {:format => /.+@.+\..+/}
30
+
31
+ ### Verify domain (still to be done - pull request, anybody?)
32
+
33
+ This also checks that the domain actually has an MX record. Note this might take a while because of DNS lookups.
34
+
35
+ validates :email, :email_address => {:mx => true}
36
+
37
+ ## Installation
38
+
39
+ Add this line to your application's Gemfile:
40
+
41
+ ```ruby
42
+ gem 'activemodel-email_address_validator'
43
+ ```
44
+
45
+ And then execute:
46
+
47
+ $ bundle
48
+
49
+ Or install it yourself as:
50
+
51
+ $ gem install activemodel-email_address_validator
52
+
53
+ ## Resources
54
+
55
+ * http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address
56
+ * http://davidcel.is/blog/2012/09/06/stop-validating-email-addresses-with-regex/
57
+ * http://blog.myitcv.org.uk/2013/03/14/validators-in-rails-using-email-as-an-example.html
58
+
59
+ ## Other libraries
60
+
61
+ ### Serverside
62
+
63
+ * https://github.com/balexand/email_validator
64
+ * https://github.com/codyrobbins/active-model-email-validator
65
+ * https://github.com/franckverrot/activevalidators
66
+ * https://github.com/hallelujah/valid_email
67
+ * https://github.com/validates-email-format-of/validates_email_format_of
68
+
69
+ ### Clientside
70
+
71
+ * https://github.com/mailcheck/mailcheck
72
+
73
+ ## Contributing
74
+
75
+ 1. Fork it ( https://github.com/substancelab/activemodel-email_address_validator/fork )
76
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
77
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
78
+ 4. Push to the branch (`git push origin my-new-feature`)
79
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.test_files = Dir.glob("test/**/test_*.rb")
6
+ t.libs << "test"
7
+ end
8
+
9
+ task :default => :test
10
+
11
+ desc "Launch an IRB console with the gem loaded"
12
+ task :console do
13
+ require "irb"
14
+ require "irb/completion"
15
+ require "activemodel-email_address_validator"
16
+ ARGV.clear
17
+ IRB.start
18
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "activemodel_email_address_validator/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "activemodel-email_address_validator"
8
+ spec.version = ActiveModelEmailAddressValidator::VERSION
9
+ spec.authors = ["Jakob Skjerning"]
10
+ spec.email = ["jakob@mentalized.net"]
11
+ spec.summary = "ActiveModel-style email address format validator"
12
+ spec.homepage = ""
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "activemodel", "~> 4.0"
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.7"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "minitest"
25
+ end
@@ -0,0 +1,5 @@
1
+ require "activemodel_email_address_validator/version"
2
+ require "validator/email_address_validator"
3
+
4
+ module ActiveModelEmailAddressValidator
5
+ end
@@ -0,0 +1,39 @@
1
+ module ActiveModelEmailAddressValidator
2
+ class EmailAddress
3
+ def initialize(address)
4
+ @address = address.to_s
5
+ end
6
+
7
+ def to_s
8
+ address
9
+ end
10
+
11
+ def valid?(regex = nil)
12
+ if regex
13
+ valid_using_regex?(regex)
14
+ else
15
+ valid_using_default?
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ attr_reader :address
22
+
23
+ def valid_using_regex?(regex)
24
+ address.to_s =~ regex
25
+ end
26
+
27
+ def valid_using_default?
28
+ return false if address =~ /\s+/
29
+ email_parts = address.split("@")
30
+
31
+ return false unless email_parts.size == 2
32
+
33
+ user, host = *email_parts
34
+ return false unless user =~ /^([^.]+\S)*[^. ]+$/
35
+ return false unless host =~ /^([^,. ]+\.)+[^,. ]+$/
36
+ true
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,3 @@
1
+ module ActiveModelEmailAddressValidator
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,16 @@
1
+ require "active_model/validator"
2
+ require "activemodel_email_address_validator/email_address"
3
+
4
+ class EmailAddressValidator < ActiveModel::EachValidator
5
+ def validate_each(record, attribute, value)
6
+ address = ActiveModelEmailAddressValidator::EmailAddress.new(value)
7
+ regex = options[:format]
8
+ invalidate(record, attribute) unless address.valid?(regex)
9
+ end
10
+
11
+ private
12
+
13
+ def invalidate(record, attribute)
14
+ record.errors.add(attribute, :invalid)
15
+ end
16
+ end
@@ -0,0 +1,128 @@
1
+ require "minitest_helper"
2
+
3
+ class EmailAddressValidTest < MiniTest::Test
4
+ def test_accepts_common_email_address_format
5
+ accept("bob@example.com")
6
+ end
7
+
8
+ def test_rejects_email_address_without_at_sign
9
+ reject("bobexample.com")
10
+ end
11
+
12
+ def test_rejects_any_space
13
+ reject("John. Doe. somewhere@gmail.com")
14
+ end
15
+
16
+ def test_rejects_nil
17
+ reject(nil)
18
+ end
19
+
20
+ def test_rejects_blank_string
21
+ reject("")
22
+ end
23
+
24
+ def test_rejects_empty_string
25
+ reject(" ")
26
+ end
27
+
28
+ def test_accepts_hostname_with_dashes
29
+ accept("my@do-main.com")
30
+ end
31
+
32
+ def test_accepts_hostname_with_numbers
33
+ accept("my@d0main42.com")
34
+ end
35
+
36
+ def test_accepts_hostname_with_only_numbers
37
+ accept("iam@1337.com")
38
+ end
39
+
40
+ def test_accepts_hostname_with_multiple_parts
41
+ accept("my@secure.email.domain.name")
42
+ end
43
+
44
+ def test_accepts_local_part_with_dashes
45
+ accept("super-bob@example.com")
46
+ end
47
+
48
+ def test_accepts_local_part_with_period
49
+ accept("super.bob@example.com")
50
+ end
51
+
52
+ def test_accepts_local_part_with_plus
53
+ accept("super+bob@example.com")
54
+ end
55
+
56
+ def test_rejects_local_part_starting_with_dot
57
+ reject(".my@domain.com")
58
+ end
59
+
60
+ def test_rejects_local_part_with_spaces
61
+ reject("f oo @domain.com")
62
+ end
63
+
64
+ def test_rejects_local_part_ending_with_dot
65
+ reject("my.@domain.com")
66
+ end
67
+
68
+ def test_rejects_hostname_starting_with_dot
69
+ reject("my@.domain.com")
70
+ end
71
+
72
+ def test_rejects_hostname_starting_with_space
73
+ reject("my@ domain.com")
74
+ end
75
+
76
+ def test_rejects_hostname_ending_with_dot
77
+ reject("my@domain.com.")
78
+ end
79
+
80
+ def test_rejects_domain_part_without_dot
81
+ reject("my@domaindk")
82
+ end
83
+
84
+ def test_rejects_multiple_sequential_dots_in_local_part
85
+ reject("my..nice@domain.com")
86
+ end
87
+
88
+ def test_rejects_multiple_sequential_dots_in_hostname
89
+ reject("my@nice..domain.com")
90
+ end
91
+
92
+ def test_rejects_zero_at_signs
93
+ reject("mydomain.com")
94
+ end
95
+
96
+ def test_rejects_multiple_at_signs
97
+ reject("my@nice@domain.com")
98
+ end
99
+
100
+ def test_rejects_commas_in_hostname
101
+ reject("my@domain.com,")
102
+ end
103
+
104
+ def test_handles_long_failing_strings
105
+ reject("fernandeztorralbofrancisco@sabadellatlantico.")
106
+ end
107
+
108
+ private
109
+
110
+ def accept(email_address)
111
+ subject = ActiveModelEmailAddressValidator::EmailAddress.new(email_address)
112
+ assert subject.valid?, "Expected #{email_address} to be valid"
113
+ end
114
+
115
+ def reject(email_address)
116
+ subject = ActiveModelEmailAddressValidator::EmailAddress.new(email_address)
117
+ assert !subject.valid?, "Expected #{email_address} to be invalid"
118
+ end
119
+ end
120
+
121
+ class EmailAddressTest < MiniTest::Test
122
+ def test_to_s_uses_address
123
+ email_address = ActiveModelEmailAddressValidator::EmailAddress.new(
124
+ "bob@example.com"
125
+ )
126
+ assert_equal "bob@example.com", email_address.to_s
127
+ end
128
+ end
@@ -0,0 +1,8 @@
1
+ require "coveralls"
2
+ Coveralls.wear!
3
+
4
+ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
5
+ require "activemodel-email_address_validator"
6
+
7
+ require "minitest/autorun"
8
+ require "minitest/spec"
@@ -0,0 +1,73 @@
1
+ require "minitest_helper"
2
+ require "active_model"
3
+
4
+ # rubocop:disable Metrics/MethodLength
5
+ SIMPLEST_VALIDATION = {:email => {:email_address => true}}
6
+ def build_model_with_validations(validations = SIMPLEST_VALIDATION)
7
+ klass = Class.new do
8
+ include ActiveModel::Validations
9
+ def self.model_name
10
+ ActiveModel::Name.new(self, nil, "ValidatorModel")
11
+ end
12
+
13
+ validations.each do |attribute, options|
14
+ attr_accessor attribute
15
+ validates attribute, options
16
+ end
17
+ end
18
+ klass.new
19
+ end
20
+ # rubocop:enable Metrics/MethodLength
21
+
22
+ class EmailAddressValidatorTest < MiniTest::Test
23
+ def setup
24
+ @subject = build_model_with_validations
25
+ end
26
+
27
+ def test_accepts_nil_email_address
28
+ @subject = build_model_with_validations(
29
+ :email => {:email_address => true, :allow_nil => true}
30
+ )
31
+ accept(nil)
32
+ end
33
+
34
+ def test_accepts_valid_email_address
35
+ accept("bob@example.com")
36
+ end
37
+
38
+ def test_rejects_invalid_email_address
39
+ reject("bobexample.com")
40
+ end
41
+
42
+ def test_adds_errors_to_validated_attribute
43
+ subject = build_model_with_validations(
44
+ :work_email => {:email_address => true}
45
+ )
46
+ subject.work_email = "whatever"
47
+ subject.valid?
48
+ assert subject.errors[:email].empty?
49
+ assert !subject.errors[:work_email].empty?
50
+ end
51
+
52
+ def test_validates_with_custom_regular_expression
53
+ subject = build_model_with_validations(
54
+ :email => {:email_address => {:format => /.+@enterprise\..+/}}
55
+ )
56
+ subject.email = "whatever@enterprise.museum"
57
+ assert subject.valid?
58
+ subject.email = "totally@valid.com"
59
+ assert !subject.valid?
60
+ end
61
+
62
+ private
63
+
64
+ def accept(email_address)
65
+ @subject.email = email_address
66
+ assert @subject.valid?, "Expected #{email_address.inspect} to be valid"
67
+ end
68
+
69
+ def reject(email_address)
70
+ @subject.email = email_address
71
+ assert !@subject.valid?, "Expected #{email_address.inspect} to be invalid"
72
+ end
73
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activemodel-email_address_validator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jakob Skjerning
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-10-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activemodel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
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'
69
+ description:
70
+ email:
71
+ - jakob@mentalized.net
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - activemodel-email_address_validator.gemspec
83
+ - lib/activemodel-email_address_validator.rb
84
+ - lib/activemodel_email_address_validator/email_address.rb
85
+ - lib/activemodel_email_address_validator/version.rb
86
+ - lib/validator/email_address_validator.rb
87
+ - test/activemodel-email_address_validator/test_email_address.rb
88
+ - test/minitest_helper.rb
89
+ - test/test_active_model_integration.rb
90
+ homepage: ''
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.4.6
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: ActiveModel-style email address format validator
114
+ test_files:
115
+ - test/activemodel-email_address_validator/test_email_address.rb
116
+ - test/minitest_helper.rb
117
+ - test/test_active_model_integration.rb