portuguese_validators 0.0.1 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cb5e228a583342dcd6b0afdfe85cadc5508054f3
4
- data.tar.gz: 398b47b5b3f861d5a94bb5d537c5fa7647247ba8
3
+ metadata.gz: dda9a0ba1218650749627b149ac72a33a2db99a6
4
+ data.tar.gz: cddc577ffa4606e7298f2b770082559ca77ca8c6
5
5
  SHA512:
6
- metadata.gz: dcb090dc850e45d3259dede6dd878975526712d55773ac07c92062e8fa94500fcf213a6c291b634a6b40ad42d7717d982f20540d495a18ea730e942ce8179c95
7
- data.tar.gz: d74ed5af267eea869f35efd8c3482f99a86453a00c3e13aab6d7b7f1f6bc823a220fc1d9f6eab9e627c98e2a1ca3b5f255e662eae79d4a65802912075f76304b
6
+ metadata.gz: 3741c81cce9cba804019218667329a609694220e37ef51731975eb01a8e2bbe5c4da775acbd225444c8e624803410c0d3f914ad1f77f007b55fed9452555d3f4
7
+ data.tar.gz: 40da33c44934c9a0cf6c8eaaa218d86c6ab69c04ec12f2a7e21e53eeac5abf808071ce965f6b5710925210ca08f1b77fccdd9d31f5e461911c7f328765a6ad74
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --colour
2
+ --format s -c
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ portuguese_validators
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.0.0
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - '1.9.3'
4
+ - '2.0.0'
5
+ - jruby-19mode # JRuby in 1.9 mode
6
+
7
+ script: bundle exec rspec
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
+ [![Build Status](https://travis-ci.org/rikas/portuguese_validators.svg?branch=master)](https://travis-ci.org/rikas/portuguese_validators) [![Dependency Status](https://gemnasium.com/rikas/portuguese_validators.svg)](https://gemnasium.com/rikas/portuguese_validators)
2
+
1
3
  # PortugueseValidators
2
4
 
3
- This is a simple gem to validate NIB, NIF and BI numbers.
5
+ This is a simple gem to validate NIB, NIF, BI and phone numbers.
4
6
 
5
7
  ## Installation
6
8
 
@@ -24,15 +26,14 @@ $ gem install portuguese_validators
24
26
 
25
27
  ## Usage
26
28
 
27
- You will have to include `PortugueseValidators` and then just add the validators to your model:
29
+ Just add the custom validators to your models and you're ready to go!
28
30
 
29
31
  ```ruby
30
32
  class MyModel < ActiveRecord::Base
31
- include PortugueseValidators
32
-
33
- validates :nif_field, nif: true
34
- validates :nib_field, nib: true
35
- validates :bi_field, bi: true
33
+ validates :nif, portuguese_nif: true
34
+ validates :nib, portuguese_nib: true
35
+ validates :bi, portuguese_bi: true
36
+ validates :phone, portuguese_phone: true
36
37
  end
37
38
  ```
38
39
 
@@ -40,9 +41,7 @@ Note that these validators are just like the ones that Rails provide you so you
40
41
 
41
42
  ```ruby
42
43
  class MyModel < ActiveRecord::Base
43
- include PortugueseValidators
44
-
45
- validates :bi_field, bi: { message: 'é um número errado' }, if: :user_pt?
44
+ validates :bi, portuguese_bi: { message: 'é inválido' }, if: :user_pt?
46
45
  end
47
46
  ```
48
47
 
@@ -4,26 +4,30 @@ module PortugueseValidators
4
4
  # A Portuguese BI number is comprised by eight digits. Between that number and the "issue" box,
5
5
  # the card also has a small box with a single digit. That's the control digit. In order to
6
6
  # validate a Portuguese BI you have to give the full number (including the control digit).
7
- class BiValidator
8
- def validate_each(record, attribute, value)
7
+ class PortugueseBiValidator < ActiveModel::EachValidator
8
+ BLACKLIST = %w(000000000)
9
+
10
+ def validate_each(record, attribute, value) # :nodoc:
9
11
  unless is_valid?(value)
10
12
  record.errors[attribute] << (options[:message] || 'is not a valid BI')
11
13
  end
12
14
  end
13
15
 
16
+ # Returns true if the number is a valid BI or false otherwise.
14
17
  def is_valid?(number)
15
- looks_like_bi?(number.to_s) && is_valid_bi?(number.to_s)
18
+ return false unless number
19
+
20
+ number = number.to_s
21
+ looks_like_bi?(number) && valid_bi?(number)
16
22
  end
17
23
 
18
24
  private
19
25
 
20
- def is_valid_bi?(number)
26
+ def valid_bi?(number)
21
27
  control = number.split('').map { |digit| digit.to_i }
22
28
 
23
29
  sum = 0
24
- 9.downto(2) do |num|
25
- sum += num * control.shift
26
- end
30
+ 9.downto(2) { |num| sum += num * control.shift }
27
31
 
28
32
  expected = 11 - sum % 11;
29
33
  expected = 0 if expected > 9 # when the value is greater than 9 then we assume 0
@@ -32,7 +36,8 @@ module PortugueseValidators
32
36
  end
33
37
 
34
38
  def looks_like_bi?(number)
35
- return false unless number
39
+ return false if !number || BLACKLIST.include?(number)
40
+
36
41
  number.match(/^\d{9}$/) ? true : false
37
42
  end
38
43
  end
@@ -2,7 +2,7 @@ module PortugueseValidators
2
2
  # Validates Portuguese bank numbers (NIB).
3
3
  #
4
4
  # The number is always composed by 21 where the last two form the control number.
5
- class NibValidator < ActiveModel::EachValidator
5
+ class PortugueseNibValidator < ActiveModel::EachValidator
6
6
  def validate_each(record, attribute, value)
7
7
  unless is_valid?(value)
8
8
  record.errors[attribute] << (options[:message] || 'is not a valid NIB')
@@ -10,13 +10,15 @@ module PortugueseValidators
10
10
  end
11
11
 
12
12
  def is_valid?(number)
13
+ return false unless number
14
+
13
15
  number = sprintf("%021o", number) if number.kind_of?(Integer)
14
- looks_like_nib?(number) && is_valid_nib?(number)
16
+ looks_like_nib?(number) && valid_nib?(number)
15
17
  end
16
18
 
17
19
  private
18
20
 
19
- def is_valid_nib?(number)
21
+ def valid_nib?(number)
20
22
  nib = number.slice(0..18).split('').map { |digit| digit.to_i }
21
23
  control = number.slice(19..20).to_i
22
24
 
@@ -3,7 +3,7 @@ module PortugueseValidators
3
3
  #
4
4
  # The portuguese NIF is composed by 9 digits and it must start with 1, 2, 5, 6, 7, 8 or 9. The
5
5
  # last digit is the control digit.
6
- class NifValidator < ActiveModel::EachValidator
6
+ class PortugueseNifValidator < ActiveModel::EachValidator
7
7
  def validate_each(record, attribute, value)
8
8
  unless is_valid?(value)
9
9
  record.errors[attribute] << (options[:message] || 'is not a valid NIF')
@@ -11,13 +11,15 @@ module PortugueseValidators
11
11
  end
12
12
 
13
13
  def is_valid?(number)
14
+ return false unless number
15
+
14
16
  nif = number.to_s
15
- looks_like_nif?(nif) && is_valid_nif?(nif)
17
+ looks_like_nif?(nif) && valid_nif?(nif)
16
18
  end
17
19
 
18
20
  private
19
21
 
20
- def is_valid_nif?(number)
22
+ def valid_nif?(number)
21
23
  control = number.split('').map { |digit| digit.to_i }
22
24
 
23
25
  sum = 0
@@ -31,8 +33,6 @@ module PortugueseValidators
31
33
  expected == control.last
32
34
  end
33
35
 
34
- # Checks if at least the given number has the correct number of digits and starts with a valid
35
- # digit.
36
36
  def looks_like_nif?(number)
37
37
  return false unless number
38
38
  number.match(/^\d{9}$/) && number.match(/^[1256789]/) ? true : false
@@ -0,0 +1,37 @@
1
+ module PortugueseValidators
2
+ # Validates Portuguese Phone numbers.
3
+ #
4
+ # The portuguese phone numbers can start by 00351 or +351 and are composed by 9 digits.
5
+ # The number must start with one of the values in VALID_PREFIXES
6
+ class PortuguesePhoneValidator < ActiveModel::EachValidator
7
+ VALID_PREFIXES = %w(91 92 93 96 21 226 231 232 233 234 235 236 238 239 241 242 243 244 245 249
8
+ 251 252 253 254 255 256 258 259 261 262 263 265 266 268 269 271 272 273 274 275 276 277 278
9
+ 279 281 281 282 283 284 285 286 289 291 292 295 296)
10
+
11
+ COUNTRY_PREFIXES = %w(00351 +351)
12
+
13
+ def validate_each(record, attribute, value)
14
+ return if value.blank?
15
+
16
+ number = value.gsub(" ", "")
17
+ record.errors.add(attribute, options[:message] || :invalid) unless is_valid?(number)
18
+ end
19
+
20
+ def is_valid?(number)
21
+ looks_like_phone_number?(number.to_s) && valid_phone_number?(number.to_s)
22
+ end
23
+
24
+ private
25
+
26
+ def valid_phone_number?(number)
27
+ COUNTRY_PREFIXES.each { |pref| number.gsub!(pref, '') if number.start_with?(pref) }
28
+ VALID_PREFIXES.each { |pref| return true if number.start_with?(pref) }
29
+ false
30
+ end
31
+
32
+ def looks_like_phone_number?(number)
33
+ number.match(/^((00|\+)(\d{3}))?\d{9}$/) ? true : false
34
+ end
35
+ end
36
+
37
+ end
@@ -1,3 +1,3 @@
1
1
  module PortugueseValidators
2
- VERSION = '0.0.1'
2
+ VERSION = '0.1.1'
3
3
  end
@@ -1,9 +1,19 @@
1
1
  require 'active_model/validator'
2
2
 
3
+ require 'portuguese_validators/bi'
4
+ require 'portuguese_validators/nif'
5
+ require 'portuguese_validators/nib'
6
+ require 'portuguese_validators/phone'
3
7
  require 'portuguese_validators/version'
4
- require 'portuguese_validators/nif_validator'
5
- require 'portuguese_validators/nib_validator'
6
- require 'portuguese_validators/bi_validator'
7
8
 
9
+ # PortugueseValidators offer a set of Rails validators that you can use as normal ActiveRecord
10
+ # validators.
8
11
  module PortugueseValidators
9
12
  end
13
+
14
+ # Open ActiveModel Validations to include PortugueseValidators and avoid the manual inclusion.
15
+ module ActiveModel
16
+ module Validations
17
+ include PortugueseValidators
18
+ end
19
+ end
@@ -1,6 +1,7 @@
1
1
  # coding: utf-8
2
2
  lib = File.expand_path('../lib', __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
4
5
  require 'portuguese_validators/version'
5
6
 
6
7
  Gem::Specification.new do |spec|
@@ -8,7 +9,7 @@ Gem::Specification.new do |spec|
8
9
  spec.version = PortugueseValidators::VERSION
9
10
  spec.authors = ['Ricardo Otero']
10
11
  spec.email = ['oterosantos@gmail.com']
11
- spec.summary = 'Validators for portuguese related numbers like NIB, NIF and BI.'
12
+ spec.summary = 'Validators for portuguese NIB, NIF, BI and phone.'
12
13
  spec.description = spec.summary
13
14
  spec.homepage = 'http://github.com/rikas/portuguese_validators/fork'
14
15
  spec.license = 'MIT'
@@ -20,6 +21,9 @@ Gem::Specification.new do |spec|
20
21
 
21
22
  spec.add_development_dependency 'bundler', '~> 1.5'
22
23
  spec.add_development_dependency 'rake'
24
+ spec.add_development_dependency 'rspec'
25
+ spec.add_development_dependency 'reek'
26
+ spec.add_development_dependency 'rubocop'
23
27
 
24
- spec.add_dependency 'activemodel'
28
+ spec.add_dependency 'activemodel', '~> 4.1.4'
25
29
  end
@@ -0,0 +1,45 @@
1
+ require 'spec_helper'
2
+
3
+ describe PortugueseBiValidator do
4
+ before(:all) {
5
+ class User < TestModel
6
+ validates :bi, portuguese_bi: true
7
+ end
8
+ }
9
+
10
+ after(:all) { Object.send(:remove_const, :User) }
11
+
12
+ describe 'validation' do
13
+ it 'returns false if the number is empty' do
14
+ expect(User.new(bi: '')).to be_invalid
15
+ end
16
+
17
+ it 'returns false if the number is not defined' do
18
+ expect(User.new(bi: nil)).to be_invalid
19
+ end
20
+
21
+ context 'given valid BI numbers' do
22
+ %w(117052337 134307607 178756830 101812418).each do |bi|
23
+ it "returns true for `#{bi}'" do
24
+ expect(User.new(bi: bi)).to be_valid
25
+ end
26
+ end
27
+ end
28
+
29
+ context 'given blacklisted BI numbers' do
30
+ %w(000000000).each do |bi|
31
+ it "returns false for `#{bi}'" do
32
+ expect(User.new(bi: bi)).to be_invalid
33
+ end
34
+ end
35
+ end
36
+
37
+ context 'given invalid BI numbers' do
38
+ %w(-3 117051343 138473133 7873 kjas 017051349 117051348).each do |bi|
39
+ it "returns false for `#{bi}'" do
40
+ expect(User.new(bi: bi)).to be_invalid
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ describe PortugueseNibValidator do
4
+ before(:all) {
5
+ class User < TestModel
6
+ validates :nib, portuguese_nib: true
7
+ end
8
+ }
9
+
10
+ after(:all) { Object.send(:remove_const, :User) }
11
+
12
+ describe 'validation' do
13
+ it 'returns false if the number is empty' do
14
+ expect(User.new(nib: '')).to be_invalid
15
+ end
16
+
17
+ it 'returns false if the number is not defined' do
18
+ expect(User.new(nib: nil)).to be_invalid
19
+ end
20
+
21
+ context 'given invalid NIB numbers' do
22
+ %w(1 -39 three 0 823792873 004609950003132833098).each do |nib|
23
+ it "returns false for `#{nib}'" do
24
+ expect(User.new(nib: nib)).to be_invalid
25
+ end
26
+ end
27
+ end
28
+
29
+ context 'given valid NIB numbers' do
30
+ %w(003509950003132833098 001800031700953102078 003600339910037234245).each do |nib|
31
+ it "returns true for `#{nib}'" do
32
+ expect(User.new(nib: nib)).to be_valid
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe PortugueseNifValidator do
4
+ before(:all) {
5
+ class User < TestModel
6
+ validates :nif, portuguese_nif: true
7
+ end
8
+ }
9
+
10
+ after(:all) { Object.send(:remove_const, :User) }
11
+
12
+ describe 'validation' do
13
+ it 'returns false if the number is empty' do
14
+ expect(User.new(nif: '')).to be_invalid
15
+ end
16
+
17
+ it 'returns false if the number is not defined' do
18
+ expect(User.new(nif: nil)).to be_invalid
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,23 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'active_model'
4
+
5
+ require 'portuguese_validators'
6
+
7
+ RSpec.configure do |config|
8
+ include PortugueseValidators
9
+
10
+ config.order = 'random'
11
+ end
12
+
13
+ class TestModel
14
+ include ActiveModel::Validations
15
+
16
+ def initialize(attributes = {})
17
+ @attributes = attributes
18
+ end
19
+
20
+ def read_attribute_for_validation(key)
21
+ @attributes[key]
22
+ end
23
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: portuguese_validators
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ricardo Otero
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-10 00:00:00.000000000 Z
11
+ date: 2014-07-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -39,20 +39,62 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: activemodel
42
+ name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
- type: :runtime
48
+ type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
- description: Validators for portuguese related numbers like NIB, NIF and BI.
55
+ - !ruby/object:Gem::Dependency
56
+ name: reek
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: rubocop
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: activemodel
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 4.1.4
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 4.1.4
97
+ description: Validators for portuguese NIB, NIF, BI and phone.
56
98
  email:
57
99
  - oterosantos@gmail.com
58
100
  executables: []
@@ -60,16 +102,25 @@ extensions: []
60
102
  extra_rdoc_files: []
61
103
  files:
62
104
  - ".gitignore"
105
+ - ".rspec"
106
+ - ".ruby-gemset"
107
+ - ".ruby-version"
108
+ - ".travis.yml"
63
109
  - Gemfile
64
110
  - LICENSE.txt
65
111
  - README.md
66
112
  - Rakefile
67
113
  - lib/portuguese_validators.rb
68
- - lib/portuguese_validators/bi_validator.rb
69
- - lib/portuguese_validators/nib_validator.rb
70
- - lib/portuguese_validators/nif_validator.rb
114
+ - lib/portuguese_validators/bi.rb
115
+ - lib/portuguese_validators/nib.rb
116
+ - lib/portuguese_validators/nif.rb
117
+ - lib/portuguese_validators/phone.rb
71
118
  - lib/portuguese_validators/version.rb
72
119
  - portuguese_validators.gemspec
120
+ - spec/bi_validator_spec.rb
121
+ - spec/nib_validator_spec.rb
122
+ - spec/nif_validator_spec.rb
123
+ - spec/spec_helper.rb
73
124
  homepage: http://github.com/rikas/portuguese_validators/fork
74
125
  licenses:
75
126
  - MIT
@@ -93,5 +144,9 @@ rubyforge_project:
93
144
  rubygems_version: 2.2.1
94
145
  signing_key:
95
146
  specification_version: 4
96
- summary: Validators for portuguese related numbers like NIB, NIF and BI.
97
- test_files: []
147
+ summary: Validators for portuguese NIB, NIF, BI and phone.
148
+ test_files:
149
+ - spec/bi_validator_spec.rb
150
+ - spec/nib_validator_spec.rb
151
+ - spec/nif_validator_spec.rb
152
+ - spec/spec_helper.rb