brazilian_validators 1.1.1 → 1.2.0
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.
- checksums.yaml +4 -4
- data/lib/brazilian_validators.rb +2 -1
- data/lib/brazilian_validators/cnpj.rb +45 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b69a17852a87e38cea7782dc24541c0288b15e96
|
4
|
+
data.tar.gz: 2f47a5b293f6a725b6c1ce5c31b06a6fb6d44629
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f3addbeeac1109af3d90ad945f506dafd80594bcb7696c6c39a632df8e42576964f7d29bc63bd22d4a7fd4736629645560a6026ac12b2597a126d069c285642b
|
7
|
+
data.tar.gz: 434b6d2f10e8f0afc1bd84150dee161169364c12ded01792e7e666a2bacd58cd6a949b533dae6a406d51dfb747e86340a97dfa4a505cace78e7c5f07aeccb6e1
|
data/lib/brazilian_validators.rb
CHANGED
@@ -0,0 +1,45 @@
|
|
1
|
+
class BrazilianValidators
|
2
|
+
class Cnpj
|
3
|
+
def initialize(document)
|
4
|
+
raise(StandardError, "Not a valid cnpj format") unless document.match(/[0-9]{2}\.[0-9]{3}\.[0-9]{3}\/[0-9]{4}\-[0-9]{2}|\d{14}/)
|
5
|
+
document.gsub!(/\D/, "")
|
6
|
+
@digits = document[0..11]
|
7
|
+
@first_digit_checker = document[12]
|
8
|
+
@second_digit_checker = document[13]
|
9
|
+
end
|
10
|
+
|
11
|
+
def valid?
|
12
|
+
first_digit = first_check_digit
|
13
|
+
second_digit = second_check_digit(first_digit)
|
14
|
+
first_digit == @first_digit_checker.to_i && second_digit == @second_digit_checker.to_i
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.valid?(document)
|
18
|
+
BrazilianValidators::Cnpj.new(document).valid?
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def first_check_digit
|
24
|
+
array = [5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]
|
25
|
+
sum = sum_of_digits(@digits, array)
|
26
|
+
remainder = sum % 11
|
27
|
+
remainder < 2 ? 0 : (11 - remainder)
|
28
|
+
end
|
29
|
+
|
30
|
+
def second_check_digit(first_digit)
|
31
|
+
array = [6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]
|
32
|
+
sum = sum_of_digits(@digits + first_digit.to_s, array)
|
33
|
+
remainder = sum % 11
|
34
|
+
remainder < 2 ? 0 : (11 - remainder)
|
35
|
+
end
|
36
|
+
|
37
|
+
def sum_of_digits(digits, array)
|
38
|
+
sum = 0
|
39
|
+
digits.split("").each_index do |i|
|
40
|
+
sum += digits[i].to_i * array[i]
|
41
|
+
end
|
42
|
+
sum
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: brazilian_validators
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matheus Mina
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-02-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pry
|
@@ -57,6 +57,7 @@ extensions: []
|
|
57
57
|
extra_rdoc_files: []
|
58
58
|
files:
|
59
59
|
- lib/brazilian_validators.rb
|
60
|
+
- lib/brazilian_validators/cnpj.rb
|
60
61
|
- lib/brazilian_validators/cpf.rb
|
61
62
|
- lib/brazilian_validators/phone.rb
|
62
63
|
homepage: https://github.com/mfbmina/brazilian_validators
|