brazilian-documents-validations 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/brazilian-documents-validations.rb +2 -0
- data/lib/brazilian-documents-validations/cnpj.rb +17 -34
- data/lib/brazilian-documents-validations/cpf.rb +14 -47
- data/lib/brazilian-documents-validations/document_validator.rb +68 -0
- data/lib/brazilian-documents-validations/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 67e25d17a350084aaabfa4be325b6ff5ab6100bf
|
4
|
+
data.tar.gz: bad9cc5b9f666864fcd0614cad1420aa1457b97e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 91ff96e10aff7b26b9e13cb9fafaad883ce1e85dd99907fdf3475f33e998c573a9081b19d6553dc78a5a86b157044da35bf8dcc2bf4160bf94e979b4b52ed0ac
|
7
|
+
data.tar.gz: ceac58a4ae460b25f4217e571d61fd70c5db900834ba190d91c02577e91b6b9b2f8899a4e74795a6b04c49c87279271b7ffbb61a11038d347024d4bf9fc797d6
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
[![Build Status](https://travis-ci.org/joaomilho/brazilian-documents-validations.png?branch=master)](https://travis-ci.org/joaomilho/brazilian-documents-validations.)
|
2
2
|
|
3
|
-
[![Code
|
3
|
+
[![Code Climate](https://codeclimate.com/github/joaomilho/brazilian-documents-validations.png)](https://codeclimate.com/github/joaomilho/brazilian-documents-validations)
|
4
4
|
|
5
5
|
## Brazilian Documents Validations
|
6
6
|
|
@@ -1,46 +1,29 @@
|
|
1
1
|
module CNPJ
|
2
2
|
|
3
|
-
def self.valid?
|
4
|
-
cnpj
|
5
|
-
|
6
|
-
# could be 13 or 14 digits or with mask 99.999.999/9999-99
|
7
|
-
return false if cnpj !~ /^\d{13,14}|\d{2}\.\d{3}\.\d{3}\/\d{4}-\d{2}$/
|
8
|
-
|
9
|
-
|
10
|
-
cnpj = cnpj.scan(/\d/).collect(&:to_i)
|
11
|
-
|
12
|
-
# filter 111... to 999...
|
13
|
-
return false if cnpj.uniq.size == 1
|
14
|
-
|
15
|
-
cnpj.unshift(0) if cnpj.length == 13
|
3
|
+
def self.valid? cnpj
|
4
|
+
Validator.new(cnpj).valid?
|
5
|
+
end
|
16
6
|
|
7
|
+
def self.invalid?(cnpj)
|
8
|
+
not valid?(cnpj)
|
9
|
+
end
|
17
10
|
|
18
|
-
|
19
|
-
factor = [5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]
|
11
|
+
class Validator < DocumentValidator
|
20
12
|
|
21
|
-
|
22
|
-
|
13
|
+
def initialize cnpj
|
14
|
+
@document = cnpj.to_s
|
15
|
+
@size = 14
|
23
16
|
end
|
24
17
|
|
25
|
-
|
26
|
-
result = result < 2 ? 0 : 11 - result
|
27
|
-
|
28
|
-
return false if result != cnpj[12]
|
29
|
-
|
30
|
-
# calculate second digit
|
31
|
-
factor.unshift 6
|
18
|
+
private
|
32
19
|
|
33
|
-
|
34
|
-
|
20
|
+
def valid_format?
|
21
|
+
@document =~ /^\d{13,14}|\d{2}\.\d{3}\.\d{3}\/\d{4}-\d{2}$/
|
35
22
|
end
|
36
23
|
|
37
|
-
|
38
|
-
|
24
|
+
def factor
|
25
|
+
[6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]
|
26
|
+
end
|
39
27
|
|
40
|
-
result == cnpj[13]
|
41
|
-
end
|
42
|
-
|
43
|
-
def self.invalid?(cnpj)
|
44
|
-
!valid?(cnpj)
|
45
28
|
end
|
46
|
-
end
|
29
|
+
end
|
@@ -1,62 +1,29 @@
|
|
1
1
|
module CPF
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
def self.mock size=1
|
6
|
-
size == 1 ? generate : size.times.map{ generate }
|
3
|
+
def self.valid?(cpf)
|
4
|
+
Validator.new(cpf).valid?
|
7
5
|
end
|
8
6
|
|
9
|
-
def self.
|
10
|
-
|
11
|
-
rec = 10
|
12
|
-
d1 = ns.inject(0){|s,i| s = s + i*rec; rec -= 1; s }
|
13
|
-
|
14
|
-
d1 = 11 - ( d1 % 11 )
|
15
|
-
d1 = 0 if d1 >= 10
|
16
|
-
rec = 11
|
17
|
-
d2 = d1*2 + ns.inject(0){|s,i| s = s + i*rec; rec -= 1; s }
|
18
|
-
d2 = 11 - ( d2 % 11 )
|
19
|
-
d2 = 0 if d2 >= 10
|
20
|
-
ns << d1 << d2
|
21
|
-
ns.join
|
7
|
+
def self.invalid?(cpf)
|
8
|
+
not valid?(cpf)
|
22
9
|
end
|
23
10
|
|
24
|
-
|
25
|
-
cpf = cpf.to_s
|
26
|
-
|
27
|
-
# could be 10 or 11 digits or with mask 999.999.999-99
|
28
|
-
return false if cpf !~ /^\d{10,11}|\d{3}\.\d{3}\.\d{3}-\d{2}$/
|
29
|
-
|
30
|
-
cpf = cpf.scan(/\d/).collect(&:to_i)
|
31
|
-
cpf.unshift(0) if cpf.length == 10
|
32
|
-
|
33
|
-
# filter black list
|
34
|
-
return false if BLACK_LIST.include? cpf.join
|
11
|
+
class Validator < DocumentValidator
|
35
12
|
|
36
|
-
|
37
|
-
|
38
|
-
|
13
|
+
def initialize cpf
|
14
|
+
@document = cpf.to_s
|
15
|
+
@size = 11
|
39
16
|
end
|
40
17
|
|
41
|
-
|
42
|
-
result = result < 2 ? 0 : 11 - result
|
18
|
+
private
|
43
19
|
|
44
|
-
|
45
|
-
|
46
|
-
# calculate second digit
|
47
|
-
sum = (0..8).inject(0) do |sum, i|
|
48
|
-
sum + cpf[i] * (11 - i)
|
20
|
+
def valid_format?
|
21
|
+
@document =~ /^\d{10,11}|\d{3}\.\d{3}\.\d{3}-\d{2}$/
|
49
22
|
end
|
50
23
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
result = result < 2 ? 0 : 11 - result
|
55
|
-
|
56
|
-
result == cpf[10]
|
57
|
-
end
|
24
|
+
def factor
|
25
|
+
(2..11).to_a.reverse
|
26
|
+
end
|
58
27
|
|
59
|
-
def self.invalid?(cpf)
|
60
|
-
!valid?(cpf)
|
61
28
|
end
|
62
29
|
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
class DocumentValidator
|
2
|
+
|
3
|
+
def valid?
|
4
|
+
valid_format? and
|
5
|
+
whitelisted? and
|
6
|
+
valid_first_digit? and
|
7
|
+
valid_second_digit?
|
8
|
+
end
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def whitelisted?
|
13
|
+
to_a.uniq.size > 1
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_a
|
17
|
+
@to_a ||= split(@document)
|
18
|
+
end
|
19
|
+
|
20
|
+
def calculate_mod value
|
21
|
+
mod = value % 11
|
22
|
+
mod < 2 ? 0 : 11 - mod
|
23
|
+
end
|
24
|
+
|
25
|
+
def valid_first_digit?
|
26
|
+
first_digit_calculated == first_digit
|
27
|
+
end
|
28
|
+
|
29
|
+
def valid_second_digit?
|
30
|
+
second_digit_calculated == second_digit
|
31
|
+
end
|
32
|
+
|
33
|
+
def first_digit
|
34
|
+
to_a[-2]
|
35
|
+
end
|
36
|
+
|
37
|
+
def second_digit
|
38
|
+
to_a.last
|
39
|
+
end
|
40
|
+
|
41
|
+
def split value
|
42
|
+
splitted = value.scan(/\d/).collect(&:to_i)
|
43
|
+
splitted.length == @size - 1 ? splitted.unshift(0) : splitted
|
44
|
+
end
|
45
|
+
|
46
|
+
def sum chars, factor
|
47
|
+
(0..chars).inject(0) do |sum, i|
|
48
|
+
sum + to_a[i] * factor[i]
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def first_sum
|
53
|
+
sum @size-3, factor[1..-1]
|
54
|
+
end
|
55
|
+
|
56
|
+
def second_sum
|
57
|
+
sum @size-2, factor
|
58
|
+
end
|
59
|
+
|
60
|
+
def first_digit_calculated
|
61
|
+
calculate_mod(first_sum)
|
62
|
+
end
|
63
|
+
|
64
|
+
def second_digit_calculated
|
65
|
+
calculate_mod(second_sum)
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: brazilian-documents-validations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Maiz Lulkin
|
@@ -52,6 +52,7 @@ files:
|
|
52
52
|
- lib/brazilian-documents-validations/cnpj_validator.rb
|
53
53
|
- lib/brazilian-documents-validations/cpf.rb
|
54
54
|
- lib/brazilian-documents-validations/cpf_validator.rb
|
55
|
+
- lib/brazilian-documents-validations/document_validator.rb
|
55
56
|
- lib/brazilian-documents-validations/version.rb
|
56
57
|
homepage: https://github.com/joaomilho/brazilian-documents-validations
|
57
58
|
licenses: []
|