ama_validators 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f992cedbd73f6a7d0a87e10c5db60808195b3d29
4
+ data.tar.gz: 6b75d4570ac716d663d4a13126c90a7a82d12e52
5
+ SHA512:
6
+ metadata.gz: 05517d720600d56e2835127a0b3dd03ff125098d44a2eb6541110652c54d2806946e8c98a995bb0486d939a01b382cc53e4523b4583ac15908f7e6b9f2211b63
7
+ data.tar.gz: a6581312f978fc4f614421c43e2c652f972ec8a51ae8d88f9682f6c826e2deaa3bf52ab4a9b0f3ad09795884ea2ce247d7dea8f09335856c75eb0cf98eefcfaf
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,4 @@
1
+ --color
2
+ --require spec_helper
3
+ --require rspec/instafail
4
+ --format RSpec::Instafail
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ama_validators.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Ruben Estevez
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.
@@ -0,0 +1,29 @@
1
+ # AmaValidators
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'ama_validators'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install ama_validators
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ama_validators/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ama_validators"
8
+ spec.version = AmaValidators::VERSION
9
+ spec.authors = ["Ruben Estevez"]
10
+ spec.email = ["ruben.a.estevez@gmail.com"]
11
+ spec.description = "Compile the following validators: - Credit card - Email - Membership number - Phone number - Postal code"
12
+ spec.summary = "This gem will compile the following validators - Credit card - Email - Membership number - Phone number - Postal code. With this gem there is no need for the validators classes."
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec-rails"
24
+ spec.add_development_dependency "rspec-instafail"
25
+ spec.add_development_dependency "pry"
26
+ spec.add_development_dependency "pry-debugger"
27
+ spec.add_dependency "rails", ">= 4.0.0"
28
+
29
+ end
@@ -0,0 +1,11 @@
1
+ require "ama_validators/version"
2
+
3
+ module AmaValidators
4
+
5
+ require 'ama_validators/postal_code_format_validator'
6
+ require 'ama_validators/email_format_validator'
7
+ require 'ama_validators/credit_card_format_validator'
8
+ require 'ama_validators/membership_number_format_validator'
9
+ require 'ama_validators/phone_number_format_validator'
10
+
11
+ end
@@ -0,0 +1,7 @@
1
+ class CreditCardFormatValidator < ActiveModel::EachValidator
2
+ def validate_each(object, attribute, value)
3
+ unless value =~ /((4\d{3})|(5[1-5]\d{2})|(6011)|(34\d{1})|(37\d{1}))-?\d{4}-?\d{4}-?\d{4}|3[4,7][\d\s-]{15}/
4
+ object.errors[attribute] << (options[:message] || "enter a valid credit card number (Visa or Mastercard)")
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ class EmailFormatValidator < ActiveModel::EachValidator
2
+ def validate_each(object, attribute, value)
3
+ unless value =~ /\A[^@\s]+@([^@\s\.]+\.)+[^@\s\.]+\z/
4
+ object.errors[attribute] << (options[:message] || "enter a valid email address (e.g. name@example.com)")
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ class MembershipNumberFormatValidator < ActiveModel::EachValidator
2
+ def validate_each(object, attribute, value)
3
+ unless value =~ /\A(620272)(\d){10}\z/
4
+ object.errors[attribute] << (options[:message] || "must be a valid membership number")
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ class PhoneNumberFormatValidator < ActiveModel::EachValidator
2
+ def validate_each(object, attribute, value)
3
+ unless value =~ /\A(1( |-)?)?(\(?[0-9]{3}\)?|[0-9]{3})( |-)?([0-9]{3}( |-)?[0-9]{4}|[a-zA-Z0-9]{7})\z/
4
+ object.errors[attribute] << (options[:message] || "enter a valid 10-digit number (e.g. 587-555-5555)")
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,8 @@
1
+ class PostalCodeFormatValidator < ActiveModel::EachValidator
2
+ def validate_each(object, attribute, value)
3
+ unless value =~ /\A(\d{5}((-|\s)\d{4})?)|([txTX]\d[abceghjklmnprtvxyzABCEGHJKLMNPRSTVWXYZ])\ {0,1}(\d[abceghjklmnprstvxyzABCEGHJKLMNPRSTVWXYZ]\d)\z/
4
+ object.errors[attribute] << (options[:message] || "enter a valid AB or NT postal code (e.g. T2T 2T2)")
5
+ end
6
+ end
7
+ end
8
+
@@ -0,0 +1,3 @@
1
+ module AmaValidators
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,48 @@
1
+ require 'spec_helper'
2
+ require 'factories/profiles.rb'
3
+
4
+ describe CreditCardFormatValidator do
5
+
6
+ let(:subject) { CreditCardFormatValidator }
7
+
8
+ let( :attribute ) { :credit_card }
9
+ let (:object) { Profile.new }
10
+
11
+
12
+ context 'Wrong credit card format' do
13
+
14
+ context 'No message is sent on the options' do
15
+ it 'it returns error message expecified on the validator' do
16
+ n = subject.new( { attributes: attribute } )
17
+ expect(n.validate_each(object, attribute, '9485582751367890')).to include('enter a valid credit card number (Visa or Mastercard)')
18
+ end
19
+ end
20
+
21
+ context 'Message is sent on the options' do
22
+ it 'it returns error message expecified on the options' do
23
+ n = subject.new( { message: 'Test error message', attributes: :postal_code } )
24
+ expect(n.validate_each(object, attribute, '9485582751367890')).to include('Test error message')
25
+ end
26
+ end
27
+
28
+ end
29
+
30
+ context 'Correct credit card format' do
31
+
32
+ context 'No message is sent on the options' do
33
+ it 'it do not return error message' do
34
+ n = subject.new( { attributes: attribute } )
35
+ expect(n.validate_each(object, attribute, '4716157014589654')).to equal(nil)
36
+ end
37
+ end
38
+
39
+ context 'Message is sent on the options' do
40
+ it 'it do not return error message' do
41
+ n = subject.new( { message: 'Test error message', attributes: attribute } )
42
+ expect(n.validate_each(object, attribute, '5595361193388663')).to equal(nil)
43
+ end
44
+ end
45
+
46
+ end
47
+
48
+ end
@@ -0,0 +1,48 @@
1
+ require 'spec_helper'
2
+ require 'factories/profiles.rb'
3
+
4
+ describe EmailFormatValidator do
5
+
6
+ let(:subject) { EmailFormatValidator }
7
+
8
+ let( :attribute ) { :email }
9
+ let (:object) { Profile.new }
10
+
11
+
12
+ context 'Wrong email format' do
13
+
14
+ context 'No message is sent on the options' do
15
+ it 'it returns error message expecified on the validator' do
16
+ n = subject.new( { attributes: attribute } )
17
+ expect(n.validate_each(object, attribute, 'fail@com')).to include('enter a valid email address (e.g. name@example.com)')
18
+ end
19
+ end
20
+
21
+ context 'Message is sent on the options' do
22
+ it 'it returns error message expecified on the options' do
23
+ n = subject.new( { message: 'Test error message', attributes: :postal_code } )
24
+ expect(n.validate_each(object, attribute, 'fail.com')).to include('Test error message')
25
+ end
26
+ end
27
+
28
+ end
29
+
30
+ context 'Correct email format' do
31
+
32
+ context 'No message is sent on the options' do
33
+ it 'it do not return error message' do
34
+ n = subject.new( { attributes: attribute } )
35
+ expect(n.validate_each(object, attribute, 'no_fail@mail.com')).to equal(nil)
36
+ end
37
+ end
38
+
39
+ context 'Message is sent on the options' do
40
+ it 'it do not return error message' do
41
+ n = subject.new( { message: 'Test error message', attributes: attribute } )
42
+ expect(n.validate_each(object, attribute, 'no_fail@mail.com')).to equal(nil)
43
+ end
44
+ end
45
+
46
+ end
47
+
48
+ end
@@ -0,0 +1,24 @@
1
+ class Profile
2
+ # Required dependency for ActiveModel::Errors
3
+ extend ActiveModel::Naming
4
+
5
+ def initialize
6
+ @errors = ActiveModel::Errors.new(self)
7
+ end
8
+
9
+ attr_reader :errors
10
+
11
+ # The following methods are needed to be minimally implemented
12
+
13
+ def read_attribute_for_validation(attr)
14
+ send(attr)
15
+ end
16
+
17
+ def Profile.human_attribute_name(attr, options = {})
18
+ attr
19
+ end
20
+
21
+ def Profile.lookup_ancestors
22
+ [self]
23
+ end
24
+ end
@@ -0,0 +1,48 @@
1
+ require 'spec_helper'
2
+ require 'factories/profiles.rb'
3
+
4
+ describe MembershipNumberFormatValidator do
5
+
6
+ let(:subject) { MembershipNumberFormatValidator }
7
+
8
+ let( :attribute ) { :membership_number }
9
+ let (:object) { Profile.new }
10
+
11
+
12
+ context 'Wrong membership number format' do
13
+
14
+ context 'No message is sent on the options' do
15
+ it 'it returns error message expecified on the validator' do
16
+ n = subject.new( { attributes: attribute } )
17
+ expect(n.validate_each(object, attribute, '6202721111')).to include('must be a valid membership number')
18
+ end
19
+ end
20
+
21
+ context 'Message is sent on the options' do
22
+ it 'it returns error message expecified on the options' do
23
+ n = subject.new( { message: 'Test error message', attributes: :postal_code } )
24
+ expect(n.validate_each(object, attribute, '6202721111')).to include('Test error message')
25
+ end
26
+ end
27
+
28
+ end
29
+
30
+ context 'Correct membership number format' do
31
+
32
+ context 'No message is sent on the options' do
33
+ it 'it do not return error message' do
34
+ n = subject.new( { attributes: attribute } )
35
+ expect(n.validate_each(object, attribute, '6202720022820001')).to equal(nil)
36
+ end
37
+ end
38
+
39
+ context 'Message is sent on the options' do
40
+ it 'it do not return error message' do
41
+ n = subject.new( { message: 'Test error message', attributes: attribute } )
42
+ expect(n.validate_each(object, attribute, '6202720022820001')).to equal(nil)
43
+ end
44
+ end
45
+
46
+ end
47
+
48
+ end
@@ -0,0 +1,48 @@
1
+ require 'spec_helper'
2
+ require 'factories/profiles.rb'
3
+
4
+ describe PhoneNumberFormatValidator do
5
+
6
+ let(:subject) { PhoneNumberFormatValidator }
7
+
8
+ let( :attribute ) { :phone_number }
9
+ let (:object) { Profile.new }
10
+
11
+
12
+ context 'Wrong phone number format' do
13
+
14
+ context 'No message is sent on the options' do
15
+ it 'it returns error message expecified on the validator' do
16
+ n = subject.new( { attributes: attribute } )
17
+ expect(n.validate_each(object, attribute, '777 777')).to include('enter a valid 10-digit number (e.g. 587-555-5555)')
18
+ end
19
+ end
20
+
21
+ context 'Message is sent on the options' do
22
+ it 'it returns error message expecified on the options' do
23
+ n = subject.new( { message: 'Test error message', attributes: attribute } )
24
+ expect(n.validate_each(object, attribute, '777 777')).to include('Test error message')
25
+ end
26
+ end
27
+
28
+ end
29
+
30
+ context 'Correct phone number format' do
31
+
32
+ context 'No message is sent on the options' do
33
+ it 'it do not return error message' do
34
+ n = subject.new( { attributes: attribute } )
35
+ expect(n.validate_each(object, attribute, '777 123 4567')).to equal(nil)
36
+ end
37
+ end
38
+
39
+ context 'Message is sent on the options' do
40
+ it 'it do not return error message' do
41
+ n = subject.new( { message: 'Test error message', attributes: attribute } )
42
+ expect(n.validate_each(object, attribute, '777 123 4567')).to equal(nil)
43
+ end
44
+ end
45
+
46
+ end
47
+
48
+ end
@@ -0,0 +1,48 @@
1
+ require 'spec_helper'
2
+ require 'factories/profiles.rb'
3
+
4
+ describe PostalCodeFormatValidator do
5
+
6
+ let(:subject) { PostalCodeFormatValidator }
7
+
8
+ let( :attribute ) { :postal_code }
9
+ let (:object) { Profile.new }
10
+
11
+
12
+ context 'Wrong postal code format' do
13
+
14
+ context 'No message is sent on the options' do
15
+ it 'it returns error message expecified on the validator' do
16
+ n = subject.new( { attributes: attribute } )
17
+ expect(n.validate_each(object, attribute, 'TTJ4M5')).to include('enter a valid AB or NT postal code (e.g. T2T 2T2)')
18
+ end
19
+ end
20
+
21
+ context 'Message is sent on the options' do
22
+ it 'it returns error message expecified on the options' do
23
+ n = subject.new( { message: 'Test error message', attributes: attribute } )
24
+ expect(n.validate_each(object, attribute, 'G7J4M5')).to include('Test error message')
25
+ end
26
+ end
27
+
28
+ end
29
+
30
+ context 'Correct postal code format' do
31
+
32
+ context 'No message is sent on the options' do
33
+ it 'it do not return error message' do
34
+ n = subject.new( { attributes: attribute } )
35
+ expect(n.validate_each(object, attribute, 'T6J4M5')).to equal(nil)
36
+ end
37
+ end
38
+
39
+ context 'Message is sent on the options' do
40
+ it 'it do not return error message' do
41
+ n = subject.new( { message: 'Test error message', attributes: attribute } )
42
+ expect(n.validate_each(object, attribute, 'T6J4M5')).to equal(nil)
43
+ end
44
+ end
45
+
46
+ end
47
+
48
+ end
@@ -0,0 +1,5 @@
1
+ require 'rails/all'
2
+ require 'rspec/rails'
3
+ require 'rspec/autorun'
4
+
5
+ require 'ama_validators'
metadata ADDED
@@ -0,0 +1,173 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ama_validators
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ruben Estevez
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec-rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-instafail
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
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry-debugger
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rails
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: 4.0.0
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: 4.0.0
111
+ description: 'Compile the following validators: - Credit card - Email - Membership
112
+ number - Phone number - Postal code'
113
+ email:
114
+ - ruben.a.estevez@gmail.com
115
+ executables: []
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - .gitignore
120
+ - .rspec
121
+ - Gemfile
122
+ - LICENSE.txt
123
+ - README.md
124
+ - Rakefile
125
+ - ama_validators.gemspec
126
+ - lib/ama_validators.rb
127
+ - lib/ama_validators/credit_card_format_validator.rb
128
+ - lib/ama_validators/email_format_validator.rb
129
+ - lib/ama_validators/membership_number_format_validator.rb
130
+ - lib/ama_validators/phone_number_format_validator.rb
131
+ - lib/ama_validators/postal_code_format_validator.rb
132
+ - lib/ama_validators/version.rb
133
+ - spec/credit_card_format_validator_spec.rb
134
+ - spec/email_format_validator_spec.rb
135
+ - spec/factories/Profiles.rb
136
+ - spec/membership_number_format_validator_spec.rb
137
+ - spec/phone_number_format_validator_spec.rb
138
+ - spec/postal_code_format_validator_spec.rb
139
+ - spec/spec_helper.rb
140
+ homepage: ''
141
+ licenses:
142
+ - MIT
143
+ metadata: {}
144
+ post_install_message:
145
+ rdoc_options: []
146
+ require_paths:
147
+ - lib
148
+ required_ruby_version: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - '>='
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ required_rubygems_version: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - '>='
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ requirements: []
159
+ rubyforge_project:
160
+ rubygems_version: 2.0.0
161
+ signing_key:
162
+ specification_version: 4
163
+ summary: This gem will compile the following validators - Credit card - Email - Membership
164
+ number - Phone number - Postal code. With this gem there is no need for the validators
165
+ classes.
166
+ test_files:
167
+ - spec/credit_card_format_validator_spec.rb
168
+ - spec/email_format_validator_spec.rb
169
+ - spec/factories/Profiles.rb
170
+ - spec/membership_number_format_validator_spec.rb
171
+ - spec/phone_number_format_validator_spec.rb
172
+ - spec/postal_code_format_validator_spec.rb
173
+ - spec/spec_helper.rb