email_format 0.0.2 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +1 -0
- data/.travis.yml +5 -7
- data/LICENSE.txt +1 -1
- data/README.md +16 -2
- data/Rakefile +1 -1
- data/email_format.gemspec +11 -11
- data/lib/email_format.rb +9 -6
- data/lib/email_format/email_format_validator.rb +2 -3
- data/lib/email_format/version.rb +1 -1
- data/spec/email_format_validator_spec.rb +61 -24
- data/spec/spec_helper.rb +8 -4
- metadata +8 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c31c4d88f2a0645a013a802257d3babbc90f0e2b
|
4
|
+
data.tar.gz: be4d1fd92dc9bf36dfa4522ae65e6156fff2d5ee
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bfacbc6d8596a86ea00d9316dd68774b4d3d149d73890ef09dd955c1658053d04f9db4105b02a5a6c8767cbd9c93df6b3b307e97d296632eda9ad4e4758819a7
|
7
|
+
data.tar.gz: c2d7be4a4014895dfbf7e43946742788ef55813a9d4ab0210c2a2d8cbb3affe8072b66599157431c76910b0e0fbf4370b70867bedac18ea9aef800a2ccc1fdf0
|
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.travis.yml
CHANGED
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -22,7 +22,7 @@ Or install it yourself as:
|
|
22
22
|
|
23
23
|
## Usage
|
24
24
|
|
25
|
-
###
|
25
|
+
### EmailFormat Module
|
26
26
|
|
27
27
|
There's a `valid?` method in the `EmailFormat` module that accepts an email as an argument:
|
28
28
|
|
@@ -32,7 +32,7 @@ EmailFormat.valid?('invalid_email') # => false
|
|
32
32
|
EmailFormat.valid?('valid@email.com') # => true
|
33
33
|
```
|
34
34
|
|
35
|
-
###
|
35
|
+
### ActiveModel::Validations
|
36
36
|
|
37
37
|
Using it is as simple as using the `validates` keyword in your model:
|
38
38
|
|
@@ -55,6 +55,20 @@ User.new('valid@email.com').valid? # => true
|
|
55
55
|
User.new('invalid_email@@').valid? # => false
|
56
56
|
```
|
57
57
|
|
58
|
+
By default, `email_format` is pretty relaxed, but there is a `strict` mode
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
class User < ActiveRecord::Base
|
62
|
+
|
63
|
+
# ...
|
64
|
+
|
65
|
+
validates :email, email_format: { strict: true }
|
66
|
+
|
67
|
+
# ...
|
68
|
+
|
69
|
+
end
|
70
|
+
```
|
71
|
+
|
58
72
|
Also, the model in question doesn't need to inherit from ActiveRecord::Base, you only need to `include ActiveModel::Validations` in your class:
|
59
73
|
|
60
74
|
```ruby
|
data/Rakefile
CHANGED
data/email_format.gemspec
CHANGED
@@ -4,24 +4,24 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
require 'email_format/version'
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name =
|
7
|
+
spec.name = 'email_format'
|
8
8
|
spec.version = EmailFormat::VERSION
|
9
|
-
spec.authors = [
|
10
|
-
spec.email = [
|
9
|
+
spec.authors = ['johnotander']
|
10
|
+
spec.email = ['johnotander@gmail.com']
|
11
11
|
spec.description = %q{Validates the email format.}
|
12
12
|
spec.summary = %q{Validates the email format with the email_regex gem.}
|
13
|
-
spec.homepage =
|
14
|
-
spec.license =
|
13
|
+
spec.homepage = 'https://github.com/johnotander/email_format'
|
14
|
+
spec.license = 'MIT'
|
15
15
|
|
16
16
|
spec.files = `git ls-files`.split($/)
|
17
17
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
-
spec.require_paths = [
|
19
|
+
spec.require_paths = ['lib']
|
20
20
|
|
21
|
-
spec.add_development_dependency
|
22
|
-
spec.add_development_dependency
|
23
|
-
spec.add_development_dependency
|
21
|
+
spec.add_development_dependency 'rspec'
|
22
|
+
spec.add_development_dependency 'bundler', '> 1.10'
|
23
|
+
spec.add_development_dependency 'rake'
|
24
24
|
|
25
|
-
spec.add_dependency
|
26
|
-
spec.add_dependency
|
25
|
+
spec.add_dependency 'activemodel'
|
26
|
+
spec.add_dependency 'email_regex'
|
27
27
|
end
|
data/lib/email_format.rb
CHANGED
@@ -1,10 +1,13 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
1
|
+
require 'email_regex'
|
2
|
+
require 'email_format/version'
|
3
|
+
require 'email_format/email_format_validator'
|
4
4
|
|
5
5
|
module EmailFormat
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
def self.valid?(email, strict)
|
7
|
+
if strict
|
8
|
+
!!(email =~ EmailRegex::EMAIL_ADDRESS_REGEX)
|
9
|
+
else
|
10
|
+
!!(email =~ /^[\S&&[^@]]+@[\S&&[^@]]+$/)
|
11
|
+
end
|
9
12
|
end
|
10
13
|
end
|
@@ -1,7 +1,6 @@
|
|
1
1
|
class EmailFormatValidator < ActiveModel::EachValidator
|
2
2
|
def validate_each(record, attribute, value)
|
3
|
-
|
4
|
-
|
5
|
-
end
|
3
|
+
return if EmailFormat.valid?(value, options[:strict])
|
4
|
+
record.errors[attribute] << (options[:message] || 'is invalid')
|
6
5
|
end
|
7
6
|
end
|
data/lib/email_format/version.rb
CHANGED
@@ -1,47 +1,84 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe EmailFormatValidator do
|
4
|
-
|
5
4
|
let(:fake_model) { FakeModel.new }
|
5
|
+
let(:fake_model_strict) { FakeModelStrict.new }
|
6
|
+
|
7
|
+
describe 'valid emails' do
|
8
|
+
context 'with strict email requirements' do
|
9
|
+
let(:valid_emails) { %w(valid@email.com another.valid@email.email.net) }
|
6
10
|
|
7
|
-
|
11
|
+
it 'should be happy' do
|
12
|
+
valid_emails.each do |email|
|
13
|
+
fake_model_strict.email = email
|
14
|
+
expect(fake_model_strict.valid?).to be_truthy
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
8
18
|
|
9
|
-
|
19
|
+
context 'with non strict requirements' do
|
20
|
+
let(:valid_emails) do
|
21
|
+
%w(valid@email.com
|
22
|
+
another.valid@email.email.net
|
23
|
+
valid@a
|
24
|
+
a@vaild
|
25
|
+
a@a)
|
26
|
+
end
|
10
27
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
28
|
+
it 'should be happy' do
|
29
|
+
valid_emails.each do |email|
|
30
|
+
fake_model.email = email
|
31
|
+
expect(fake_model.valid?).to be_truthy
|
32
|
+
end
|
15
33
|
end
|
16
34
|
end
|
17
35
|
end
|
18
36
|
|
19
|
-
|
37
|
+
describe 'invalid emails' do
|
38
|
+
context 'with strict requirements' do
|
39
|
+
let(:invalid_emails) do
|
40
|
+
%w(invalid_email@
|
41
|
+
another_invalid_email@@email.email
|
42
|
+
invalid
|
43
|
+
bad@email@here
|
44
|
+
@bad_email
|
45
|
+
another@bad,email)
|
46
|
+
end
|
20
47
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
48
|
+
it 'should not be happy' do
|
49
|
+
invalid_emails.each do |email|
|
50
|
+
fake_model_strict.email = email
|
51
|
+
expect(fake_model_strict.valid?).to be_falsey
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
27
55
|
|
28
|
-
|
29
|
-
invalid_emails
|
30
|
-
|
31
|
-
|
56
|
+
context 'with non strict requirements' do
|
57
|
+
let(:invalid_emails) do
|
58
|
+
%w(invalid_email@
|
59
|
+
another_invalid_email@@email.email
|
60
|
+
invalid
|
61
|
+
bad@email@here
|
62
|
+
@bad_email)
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'should not be happy' do
|
66
|
+
invalid_emails.each do |email|
|
67
|
+
fake_model.email = email
|
68
|
+
expect(fake_model.valid?).to be_falsey
|
69
|
+
end
|
32
70
|
end
|
33
71
|
end
|
34
72
|
end
|
35
73
|
|
36
|
-
context
|
37
|
-
|
38
|
-
let(:fake_model) { FakeModelWithBlankEmail.new }
|
74
|
+
context 'with allow_blank: true' do
|
75
|
+
let(:fake_model_strict) { FakeModelWithBlankEmail.new }
|
39
76
|
let(:blank_emails) { ['', nil, ' '] }
|
40
77
|
|
41
|
-
it
|
78
|
+
it 'should allow blank emails' do
|
42
79
|
blank_emails.each do |blank_email|
|
43
|
-
|
44
|
-
expect(
|
80
|
+
fake_model_strict.email = blank_email
|
81
|
+
expect(fake_model_strict.valid?).to be_truthy
|
45
82
|
end
|
46
83
|
end
|
47
84
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -4,10 +4,6 @@ require 'rspec'
|
|
4
4
|
require 'active_model'
|
5
5
|
require 'email_format'
|
6
6
|
|
7
|
-
RSpec.configure do |config|
|
8
|
-
config.color_enabled = true
|
9
|
-
end
|
10
|
-
|
11
7
|
class FakeModel
|
12
8
|
include ActiveModel::Validations
|
13
9
|
|
@@ -16,6 +12,14 @@ class FakeModel
|
|
16
12
|
validates :email, email_format: true
|
17
13
|
end
|
18
14
|
|
15
|
+
class FakeModelStrict
|
16
|
+
include ActiveModel::Validations
|
17
|
+
|
18
|
+
attr_accessor :email
|
19
|
+
|
20
|
+
validates :email, email_format: { strict: true }
|
21
|
+
end
|
22
|
+
|
19
23
|
class FakeModelWithBlankEmail
|
20
24
|
include ActiveModel::Validations
|
21
25
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: email_format
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- johnotander
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-06-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -28,16 +28,16 @@ dependencies:
|
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - ">"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '1.
|
33
|
+
version: '1.10'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - ">"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '1.
|
40
|
+
version: '1.10'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rake
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -88,6 +88,7 @@ extensions: []
|
|
88
88
|
extra_rdoc_files: []
|
89
89
|
files:
|
90
90
|
- ".gitignore"
|
91
|
+
- ".rspec"
|
91
92
|
- ".travis.yml"
|
92
93
|
- Gemfile
|
93
94
|
- LICENSE.txt
|
@@ -119,11 +120,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
119
120
|
version: '0'
|
120
121
|
requirements: []
|
121
122
|
rubyforge_project:
|
122
|
-
rubygems_version: 2.
|
123
|
+
rubygems_version: 2.5.1
|
123
124
|
signing_key:
|
124
125
|
specification_version: 4
|
125
126
|
summary: Validates the email format with the email_regex gem.
|
126
127
|
test_files:
|
127
128
|
- spec/email_format_validator_spec.rb
|
128
129
|
- spec/spec_helper.rb
|
129
|
-
has_rdoc:
|