br_validator 0.0.1
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.
- data/lib/br_validator/cnpj.rb +34 -0
- data/lib/br_validator/cpf.rb +39 -0
- data/lib/br_validator.rb +5 -0
- metadata +47 -0
@@ -0,0 +1,34 @@
|
|
1
|
+
module BRValidator
|
2
|
+
class CNPJ
|
3
|
+
def self.valid?(cnpj)
|
4
|
+
digits = cnpj.gsub(/[^\d]/, '').split(//).map(&:to_i)
|
5
|
+
digits.count == 14 && match_verification_digits?(digits)
|
6
|
+
end
|
7
|
+
|
8
|
+
private
|
9
|
+
|
10
|
+
def self.match_verification_digits?(cnpj_array)
|
11
|
+
*content, vd1, vd2 = cnpj_array
|
12
|
+
calculated_vd1 = first_vd_for(content)
|
13
|
+
calculated_vd1 == vd1 && second_vd_for(content, vd1) == vd2
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.first_vd_for(content)
|
17
|
+
sum = [5,4,3,2,9,8,7,6,5,4,3,2].zip(content)
|
18
|
+
.map { |i| i[0] * i[1] }
|
19
|
+
.inject(:+)
|
20
|
+
remain = sum % 11
|
21
|
+
(11 - remain < 10) ? 11 - remain : 0
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.second_vd_for(content, vd1)
|
25
|
+
content << vd1
|
26
|
+
sum = [6,5,4,3,2,9,8,7,6,5,4,3,2].zip(content)
|
27
|
+
.map { |i| i[0] * i[1] }
|
28
|
+
.inject(:+)
|
29
|
+
remain = sum % 11
|
30
|
+
(11 - remain < 10) ? 11 - remain : 0
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module BRValidator
|
2
|
+
class CPF
|
3
|
+
def self.valid?(cpf)
|
4
|
+
digits = cpf.gsub(/[^\d]/, '').split(//).map(&:to_i)
|
5
|
+
digits.count == 11 && !in_blacklist?(digits) && match_verification_digits?(digits)
|
6
|
+
end
|
7
|
+
|
8
|
+
private
|
9
|
+
|
10
|
+
# Tests if the cpf is an test-porpuse cpf
|
11
|
+
# where all digits are equal
|
12
|
+
def self.in_blacklist?(cpf_array)
|
13
|
+
cpf_array.uniq.count == 1
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.match_verification_digits?(cpf_array)
|
17
|
+
*content, vd1, vd2 = cpf_array
|
18
|
+
calculated_vd1 = first_vd_for(content)
|
19
|
+
calculated_vd1 == vd1 && second_vd_for(content, vd1) == vd2
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.first_vd_for(content)
|
23
|
+
sum = [10,9,8,7,6,5,4,3,2].zip(content)
|
24
|
+
.map { |i| i[0] * i[1] }
|
25
|
+
.inject(:+)
|
26
|
+
remain = sum % 11
|
27
|
+
(11 - remain < 10) ? 11 - remain : 0
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.second_vd_for(content, vd1)
|
31
|
+
content << vd1
|
32
|
+
sum = [11,10,9,8,7,6,5,4,3,2].zip(content)
|
33
|
+
.map { |i| i[0] * i[1] }
|
34
|
+
.inject(:+)
|
35
|
+
remain = sum % 11
|
36
|
+
(11 - remain < 10) ? 11 - remain : 0
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/lib/br_validator.rb
ADDED
metadata
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: br_validator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Germano Nicolini
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-12-16 00:00:00.000000000Z
|
13
|
+
dependencies: []
|
14
|
+
description:
|
15
|
+
email: germanogmn1@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/br_validator.rb
|
21
|
+
- lib/br_validator/cnpj.rb
|
22
|
+
- lib/br_validator/cpf.rb
|
23
|
+
homepage: http://rubygems.org/gems/br_validator
|
24
|
+
licenses: []
|
25
|
+
post_install_message:
|
26
|
+
rdoc_options: []
|
27
|
+
require_paths:
|
28
|
+
- lib
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ! '>='
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
|
+
none: false
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
requirements: []
|
42
|
+
rubyforge_project:
|
43
|
+
rubygems_version: 1.8.12
|
44
|
+
signing_key:
|
45
|
+
specification_version: 3
|
46
|
+
summary: Validators for the brazilian CPF and CNPJ
|
47
|
+
test_files: []
|