br_documents 0.0.15 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/workflows/config.yml +54 -0
- data/.gitignore +1 -1
- data/.rubocop.yml +65 -0
- data/Gemfile +1 -1
- data/Gemfile.lock +100 -0
- data/README.md +58 -1
- data/Rakefile +3 -3
- data/br_documents.gemspec +23 -19
- data/lib/br_documents.rb +3 -3
- data/lib/br_documents/cnpj_cpf/cnpj.rb +2 -2
- data/lib/br_documents/cnpj_cpf/cpf.rb +2 -2
- data/lib/br_documents/cnpj_cpf/document.rb +9 -9
- data/lib/br_documents/cnpj_validator.rb +4 -4
- data/lib/br_documents/commons/mod11.rb +1 -2
- data/lib/br_documents/commons/mod14.rb +20 -0
- data/lib/br_documents/cpf_validator.rb +4 -4
- data/lib/br_documents/gtin/gtin.rb +28 -0
- data/lib/br_documents/gtin_validator.rb +8 -0
- data/lib/br_documents/ie/ac.rb +3 -2
- data/lib/br_documents/ie/al.rb +2 -1
- data/lib/br_documents/ie/am.rb +3 -2
- data/lib/br_documents/ie/ap.rb +20 -16
- data/lib/br_documents/ie/ba.rb +28 -25
- data/lib/br_documents/ie/base.rb +1 -1
- data/lib/br_documents/ie/ce.rb +3 -2
- data/lib/br_documents/ie/df.rb +3 -2
- data/lib/br_documents/ie/es.rb +2 -1
- data/lib/br_documents/ie/factory.rb +3 -3
- data/lib/br_documents/ie/go.rb +3 -2
- data/lib/br_documents/ie/ma.rb +2 -1
- data/lib/br_documents/ie/mg.rb +14 -17
- data/lib/br_documents/ie/ms.rb +2 -1
- data/lib/br_documents/ie/mt.rb +6 -5
- data/lib/br_documents/ie/pa.rb +3 -2
- data/lib/br_documents/ie/pattern1.rb +10 -7
- data/lib/br_documents/ie/pattern2.rb +9 -8
- data/lib/br_documents/ie/pb.rb +3 -2
- data/lib/br_documents/ie/pe.rb +10 -10
- data/lib/br_documents/ie/pi.rb +2 -1
- data/lib/br_documents/ie/pr.rb +3 -2
- data/lib/br_documents/ie/rj.rb +3 -2
- data/lib/br_documents/ie/rn.rb +19 -16
- data/lib/br_documents/ie/ro.rb +17 -14
- data/lib/br_documents/ie/rr.rb +4 -3
- data/lib/br_documents/ie/rs.rb +3 -2
- data/lib/br_documents/ie/sc.rb +3 -2
- data/lib/br_documents/ie/se.rb +3 -2
- data/lib/br_documents/ie/sp.rb +17 -14
- data/lib/br_documents/ie/to.rb +6 -5
- data/lib/br_documents/ie_validator.rb +16 -15
- data/lib/br_documents/suframa/suframa.rb +60 -0
- data/lib/br_documents/suframa_validator.rb +8 -0
- data/lib/br_documents/version.rb +1 -1
- data/spec/cnpj_cpf/cnpj_spec.rb +21 -21
- data/spec/cnpj_cpf/cpf_spec.rb +21 -21
- data/spec/cnpj_validator_spec.rb +12 -12
- data/spec/cpf_validator_spec.rb +12 -12
- data/spec/gtin/gtin_spec.rb +49 -0
- data/spec/gtin_validator_spec.rb +47 -0
- data/spec/ie/ac_spec.rb +16 -16
- data/spec/ie/al_spec.rb +14 -14
- data/spec/ie/am_spec.rb +16 -16
- data/spec/ie/ap_spec.rb +14 -14
- data/spec/ie/ba_spec.rb +28 -28
- data/spec/ie/ce_spec.rb +9 -9
- data/spec/ie/df_spec.rb +16 -16
- data/spec/ie/es_spec.rb +9 -9
- data/spec/ie/factory_spec.rb +88 -89
- data/spec/ie/go_spec.rb +16 -16
- data/spec/ie/ma_spec.rb +9 -9
- data/spec/ie/mg_spec.rb +16 -16
- data/spec/ie/ms_spec.rb +9 -9
- data/spec/ie/mt_spec.rb +17 -17
- data/spec/ie/pa_spec.rb +16 -16
- data/spec/ie/pb_spec.rb +9 -9
- data/spec/ie/pe_spec.rb +27 -27
- data/spec/ie/pi_spec.rb +9 -9
- data/spec/ie/pr_spec.rb +14 -14
- data/spec/ie/rj_spec.rb +16 -16
- data/spec/ie/rn_spec.rb +27 -27
- data/spec/ie/ro_spec.rb +27 -27
- data/spec/ie/rr_spec.rb +16 -16
- data/spec/ie/rs_spec.rb +16 -16
- data/spec/ie/sc_spec.rb +16 -16
- data/spec/ie/se_spec.rb +9 -9
- data/spec/ie/shared_examples_for_pattern1.rb +11 -11
- data/spec/ie/shared_examples_for_to_remove_all_masks.rb +7 -8
- data/spec/ie/sp_spec.rb +19 -19
- data/spec/ie/to_spec.rb +35 -35
- data/spec/ie_validator_spec.rb +43 -31
- data/spec/spec_helper.rb +9 -6
- data/spec/suframa/suframa_spec.rb +109 -0
- data/spec/suframa_validator_spec.rb +36 -0
- data/spec/support/utilities.rb +2 -2
- metadata +94 -25
- data/.travis.yml +0 -6
- data/circle.yml +0 -3
@@ -1,8 +1,8 @@
|
|
1
1
|
class CnpjValidator < ActiveModel::EachValidator
|
2
2
|
def validate_each(record, attribute, value)
|
3
|
-
if value.
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
return if value.blank?
|
4
|
+
|
5
|
+
cnpj = BrDocuments::CnpjCpf::Cnpj.new(value)
|
6
|
+
record.errors.add(attribute, :invalid) unless cnpj.valid?
|
7
7
|
end
|
8
8
|
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module BrDocuments
|
2
|
+
module Commons
|
3
|
+
module Mod14
|
4
|
+
def generate_check_digit(values, weights)
|
5
|
+
sum = reduce_weights(values, weights)
|
6
|
+
mod = sum % 11
|
7
|
+
mod = (11 - mod)
|
8
|
+
mod < 10 ? mod : (mod - 10)
|
9
|
+
end
|
10
|
+
|
11
|
+
def reduce_weights(values, weights)
|
12
|
+
sum = 0
|
13
|
+
weights.each_index do |i|
|
14
|
+
sum += weights[i] * values[i].to_i
|
15
|
+
end
|
16
|
+
sum
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -1,8 +1,8 @@
|
|
1
1
|
class CpfValidator < ActiveModel::EachValidator
|
2
2
|
def validate_each(record, attribute, value)
|
3
|
-
if value.
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
return if value.blank?
|
4
|
+
|
5
|
+
cpf = BrDocuments::CnpjCpf::Cpf.new(value)
|
6
|
+
record.errors.add(attribute, :invalid) unless cpf.valid?
|
7
7
|
end
|
8
8
|
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module BrDocuments
|
2
|
+
class Gtin
|
3
|
+
def initialize(code)
|
4
|
+
@code = code
|
5
|
+
end
|
6
|
+
|
7
|
+
def valid?
|
8
|
+
valid_length? && valid_checksum?
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
def valid_length?
|
14
|
+
[8, 12, 13, 14].include?(@code.length)
|
15
|
+
end
|
16
|
+
|
17
|
+
def valid_checksum?
|
18
|
+
numbers = @code.chars[0, @code.length - 1].reverse
|
19
|
+
odd = even = 0
|
20
|
+
|
21
|
+
numbers.each_with_index do |number, i|
|
22
|
+
(i + 1).even? ? (even += number.to_i) : (odd += number.to_i * 3)
|
23
|
+
end
|
24
|
+
|
25
|
+
@code.chars.last.to_i == ((10 - (odd + even)) % 10)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/lib/br_documents/ie/ac.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require_relative
|
1
|
+
require_relative 'pattern2'
|
2
2
|
|
3
3
|
module BrDocuments
|
4
4
|
module IE
|
@@ -9,8 +9,9 @@ module BrDocuments
|
|
9
9
|
end
|
10
10
|
|
11
11
|
private
|
12
|
+
|
12
13
|
def format_ie(number)
|
13
|
-
number.sub(/(\d{2})(\d{3})(\d{3})(\d{3})(\d{2})/,
|
14
|
+
number.sub(/(\d{2})(\d{3})(\d{3})(\d{3})(\d{2})/, '\\1.\\2.\\3/\\4-\\5')
|
14
15
|
end
|
15
16
|
end
|
16
17
|
end
|
data/lib/br_documents/ie/al.rb
CHANGED
data/lib/br_documents/ie/am.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require_relative
|
1
|
+
require_relative 'pattern1'
|
2
2
|
|
3
3
|
module BrDocuments
|
4
4
|
module IE
|
@@ -9,8 +9,9 @@ module BrDocuments
|
|
9
9
|
end
|
10
10
|
|
11
11
|
private
|
12
|
+
|
12
13
|
def format_ie(number)
|
13
|
-
number.sub(/(\d{2})(\d{3})(\d{3})(\d{1})/,
|
14
|
+
number.sub(/(\d{2})(\d{3})(\d{3})(\d{1})/, '\\1.\\2.\\3-\\4')
|
14
15
|
end
|
15
16
|
end
|
16
17
|
end
|
data/lib/br_documents/ie/ap.rb
CHANGED
@@ -1,12 +1,13 @@
|
|
1
|
-
require_relative
|
2
|
-
require_relative
|
1
|
+
require_relative 'base'
|
2
|
+
require_relative '../commons/mod11'
|
3
3
|
|
4
4
|
module BrDocuments
|
5
5
|
module IE
|
6
6
|
class AP < Base
|
7
7
|
include Commons::Mod11
|
8
8
|
|
9
|
-
|
9
|
+
protected
|
10
|
+
|
10
11
|
def format_ie(number)
|
11
12
|
number
|
12
13
|
end
|
@@ -16,29 +17,32 @@ module BrDocuments
|
|
16
17
|
regex.match(@number).present?
|
17
18
|
end
|
18
19
|
|
19
|
-
def
|
20
|
+
def valid_check_digit?
|
20
21
|
weight = [9, 8, 7, 6, 5, 4, 3, 2]
|
21
22
|
detect_range_digits
|
22
|
-
@number[-1]
|
23
|
+
@number[-1] == generate_check_digit(@number, weight).to_s
|
23
24
|
end
|
24
25
|
|
26
|
+
private
|
27
|
+
|
28
|
+
# rubocop:disable Metrics/MethodLength
|
25
29
|
def detect_range_digits
|
26
|
-
number = @number[0, 8]
|
27
|
-
|
30
|
+
number = @number[0, 8].to_i
|
31
|
+
|
32
|
+
if number >= 3_000_001 && number <= 3_017_000
|
28
33
|
@p = 5
|
29
34
|
@d = 0
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
@d = 0
|
37
|
-
end
|
35
|
+
elsif number >= 3_017_001 && number <= 3_019_022
|
36
|
+
@p = 9
|
37
|
+
@d = 1
|
38
|
+
elsif number >= 3_019_023
|
39
|
+
@p = 0
|
40
|
+
@d = 0
|
38
41
|
end
|
39
42
|
end
|
43
|
+
# rubocop:enable Metrics/MethodLength
|
40
44
|
|
41
|
-
def
|
45
|
+
def generate_check_digit(values, weights)
|
42
46
|
sum = reduce_weights(values, weights)
|
43
47
|
sum += @p
|
44
48
|
mod = sum % 11
|
data/lib/br_documents/ie/ba.rb
CHANGED
@@ -1,17 +1,18 @@
|
|
1
|
-
require_relative
|
2
|
-
require_relative
|
1
|
+
require_relative 'base'
|
2
|
+
require_relative '../commons/mod11'
|
3
3
|
|
4
4
|
module BrDocuments
|
5
5
|
module IE
|
6
6
|
class BA < Base
|
7
7
|
include Commons::Mod11
|
8
8
|
|
9
|
-
|
9
|
+
protected
|
10
|
+
|
10
11
|
def format_ie(number)
|
11
|
-
if number.gsub(/-/,
|
12
|
-
number.sub(/(\d{6})(\d{2})/,
|
12
|
+
if number.gsub(/-/, '').length == 8
|
13
|
+
number.sub(/(\d{6})(\d{2})/, '\\1-\\2')
|
13
14
|
else
|
14
|
-
number.sub(/(\d{7})(\d{2})/,
|
15
|
+
number.sub(/(\d{7})(\d{2})/, '\\1-\\2')
|
15
16
|
end
|
16
17
|
end
|
17
18
|
|
@@ -19,19 +20,8 @@ module BrDocuments
|
|
19
20
|
valid_8_digits_format || valid_9_digits_format
|
20
21
|
end
|
21
22
|
|
22
|
-
def
|
23
|
-
|
24
|
-
regex_mask_2 = /^(\d{3}\.\d{3}\.\d{3})$|^(\d{9})$/
|
25
|
-
regex_mask_1.match(@number).present? || regex_mask_2.match(@number).present?
|
26
|
-
end
|
27
|
-
|
28
|
-
def valid_8_digits_format
|
29
|
-
regex = /^(\d{6}\-\d{2})$|^(\d{8})$/
|
30
|
-
regex.match(@number).present?
|
31
|
-
end
|
32
|
-
|
33
|
-
def valid_digital_check?
|
34
|
-
@number.gsub!(/[\.\/-]/, "")
|
23
|
+
def valid_check_digit?
|
24
|
+
@number.gsub!(/[\.\/-]/, '')
|
35
25
|
|
36
26
|
weight1 = make_weight(total_size)
|
37
27
|
weight2 = make_weight(total_size - 1)
|
@@ -40,7 +30,20 @@ module BrDocuments
|
|
40
30
|
number = "#{@number[0, body_size]}#{digital_check2}"
|
41
31
|
digital_check1 = digital_check_generator(number, weight1)
|
42
32
|
|
43
|
-
@number[-2, 2]
|
33
|
+
@number[-2, 2] == "#{digital_check1}#{digital_check2}"
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def valid_9_digits_format
|
39
|
+
regex_mask1 = /^(\d{7}\-\d{2})$|^(\d{9})$/
|
40
|
+
regex_mask2 = /^(\d{3}\.\d{3}\.\d{3})$|^(\d{9})$/
|
41
|
+
regex_mask1.match(@number).present? || regex_mask2.match(@number).present?
|
42
|
+
end
|
43
|
+
|
44
|
+
def valid_8_digits_format
|
45
|
+
regex = /^(\d{6}\-\d{2})$|^(\d{8})$/
|
46
|
+
regex.match(@number).present?
|
44
47
|
end
|
45
48
|
|
46
49
|
def make_weight(size)
|
@@ -51,7 +54,7 @@ module BrDocuments
|
|
51
54
|
end
|
52
55
|
|
53
56
|
def total_size
|
54
|
-
@number.gsub(/[\.\/-]/,
|
57
|
+
@number.gsub(/[\.\/-]/, '').length
|
55
58
|
end
|
56
59
|
|
57
60
|
def body_size
|
@@ -61,9 +64,9 @@ module BrDocuments
|
|
61
64
|
|
62
65
|
def digital_check_generator(number, weight)
|
63
66
|
if use_digital_check_mod10
|
64
|
-
|
67
|
+
generate_check_digit_mod10(number, weight)
|
65
68
|
else
|
66
|
-
|
69
|
+
generate_check_digit(number, weight)
|
67
70
|
end
|
68
71
|
end
|
69
72
|
|
@@ -80,10 +83,10 @@ module BrDocuments
|
|
80
83
|
end
|
81
84
|
end
|
82
85
|
|
83
|
-
def
|
86
|
+
def generate_check_digit_mod10(values, weights)
|
84
87
|
sum = reduce_weights(values, weights)
|
85
88
|
mod = sum % 10
|
86
|
-
mod
|
89
|
+
mod.zero? ? 0 : (10 - mod)
|
87
90
|
end
|
88
91
|
end
|
89
92
|
end
|
data/lib/br_documents/ie/base.rb
CHANGED
data/lib/br_documents/ie/ce.rb
CHANGED
@@ -1,11 +1,12 @@
|
|
1
|
-
require_relative
|
1
|
+
require_relative 'pattern1'
|
2
2
|
|
3
3
|
module BrDocuments
|
4
4
|
module IE
|
5
5
|
class CE < Pattern1
|
6
6
|
private
|
7
|
+
|
7
8
|
def format_ie(number)
|
8
|
-
number.sub(/(\d{8})(\d{1})/,
|
9
|
+
number.sub(/(\d{8})(\d{1})/, '\\1-\\2')
|
9
10
|
end
|
10
11
|
end
|
11
12
|
end
|
data/lib/br_documents/ie/df.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require_relative
|
1
|
+
require_relative 'pattern2'
|
2
2
|
|
3
3
|
module BrDocuments
|
4
4
|
module IE
|
@@ -9,8 +9,9 @@ module BrDocuments
|
|
9
9
|
end
|
10
10
|
|
11
11
|
private
|
12
|
+
|
12
13
|
def format_ie(number)
|
13
|
-
number.sub(/(\d{11})(\d{2})/,
|
14
|
+
number.sub(/(\d{11})(\d{2})/, '\\1-\\2')
|
14
15
|
end
|
15
16
|
end
|
16
17
|
end
|
data/lib/br_documents/ie/es.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
module BrDocuments
|
2
2
|
module IE
|
3
3
|
class Factory
|
4
|
-
def self.create(
|
5
|
-
klass = IE.const_get(
|
4
|
+
def self.create(uf_symbol, number)
|
5
|
+
klass = IE.const_get(uf_symbol.to_s)
|
6
6
|
klass.new(number)
|
7
7
|
rescue NameError
|
8
|
-
raise(ArgumentError, I18n.t(
|
8
|
+
raise(ArgumentError, I18n.t('validator.ie.uf.invalid'))
|
9
9
|
end
|
10
10
|
end
|
11
11
|
end
|
data/lib/br_documents/ie/go.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require_relative
|
1
|
+
require_relative 'pattern1'
|
2
2
|
|
3
3
|
module BrDocuments
|
4
4
|
module IE
|
@@ -9,8 +9,9 @@ module BrDocuments
|
|
9
9
|
end
|
10
10
|
|
11
11
|
private
|
12
|
+
|
12
13
|
def format_ie(number)
|
13
|
-
number.sub(/(\d{2})(\d{3})(\d{3})(\d{1})/,
|
14
|
+
number.sub(/(\d{2})(\d{3})(\d{3})(\d{1})/, '\\1.\\2.\\3-\\4')
|
14
15
|
end
|
15
16
|
end
|
16
17
|
end
|
data/lib/br_documents/ie/ma.rb
CHANGED
data/lib/br_documents/ie/mg.rb
CHANGED
@@ -1,14 +1,15 @@
|
|
1
|
-
require_relative
|
2
|
-
require_relative
|
1
|
+
require_relative 'base'
|
2
|
+
require_relative '../commons/mod11'
|
3
3
|
|
4
4
|
module BrDocuments
|
5
5
|
module IE
|
6
6
|
class MG < Base
|
7
7
|
include Commons::Mod11
|
8
8
|
|
9
|
-
|
9
|
+
protected
|
10
|
+
|
10
11
|
def format_ie(number)
|
11
|
-
number.sub(/(\d{3})(\d{3})(\d{3})(\d{4})/,
|
12
|
+
number.sub(/(\d{3})(\d{3})(\d{3})(\d{4})/, '\\1.\\2.\\3/\\4')
|
12
13
|
end
|
13
14
|
|
14
15
|
def valid_format?
|
@@ -16,37 +17,33 @@ module BrDocuments
|
|
16
17
|
regex.match(@number).present?
|
17
18
|
end
|
18
19
|
|
19
|
-
def
|
20
|
-
@number.gsub!(/[\.\/-]/,
|
20
|
+
def valid_check_digit?
|
21
|
+
@number.gsub!(/[\.\/-]/, '')
|
21
22
|
|
22
|
-
number1 = @number[0, 3]
|
23
|
+
number1 = "#{@number[0, 3]}0#{@number[3..10]}"
|
23
24
|
weight1 = [1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2]
|
24
25
|
digital_check1 = generate_first_digital_check(number1, weight1)
|
25
26
|
|
26
27
|
number2 = "#{@number[0, 11]}#{digital_check1}"
|
27
28
|
weight2 = [3, 2, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2]
|
28
|
-
digital_check2 =
|
29
|
+
digital_check2 = generate_check_digit(number2, weight2)
|
29
30
|
|
30
|
-
@number[-2, 2]
|
31
|
+
@number[-2, 2] == "#{digital_check1}#{digital_check2}"
|
31
32
|
end
|
32
33
|
|
34
|
+
private
|
35
|
+
|
33
36
|
def generate_first_digital_check(values, weights)
|
34
37
|
sum = 0
|
35
38
|
weights.each_index do |i|
|
36
39
|
number = weights[i] * values[i].to_i
|
37
|
-
|
38
|
-
sum += number
|
39
|
-
else
|
40
|
-
sum += number.to_s[0].to_i + number.to_s[1].to_i
|
41
|
-
end
|
40
|
+
sum += number < 10 ? number : number.to_s[0].to_i + number.to_s[1].to_i
|
42
41
|
end
|
43
42
|
next_dozen(sum) - sum
|
44
43
|
end
|
45
44
|
|
46
45
|
def next_dozen(number)
|
47
|
-
while number % 10 != 0
|
48
|
-
number = number + 1
|
49
|
-
end
|
46
|
+
number += 1 while number % 10 != 0
|
50
47
|
number
|
51
48
|
end
|
52
49
|
end
|