br_documents 0.1.3 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/config.yml +54 -0
- data/.rubocop.yml +65 -0
- data/Gemfile +1 -1
- data/Gemfile.lock +76 -42
- data/README.md +33 -0
- 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 +1 -2
- data/lib/br_documents/cpf_validator.rb +4 -4
- data/lib/br_documents/gtin/gtin.rb +1 -1
- data/lib/br_documents/gtin_validator.rb +6 -6
- 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 +16 -13
- 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 +7 -7
- 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 +2 -2
- data/spec/gtin_validator_spec.rb +13 -13
- 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 +7 -5
- data/spec/spec_helper.rb +6 -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 +86 -23
- data/.circleci/config.yml +0 -42
data/lib/br_documents/ie/ms.rb
CHANGED
data/lib/br_documents/ie/mt.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require_relative
|
1
|
+
require_relative 'pattern1'
|
2
2
|
|
3
3
|
module BrDocuments
|
4
4
|
module IE
|
@@ -12,14 +12,15 @@ module BrDocuments
|
|
12
12
|
end
|
13
13
|
|
14
14
|
private
|
15
|
+
|
15
16
|
def generate_zeros(number)
|
16
|
-
just_numbers = number.gsub(/[\.\/-]/,
|
17
|
-
zeros = (
|
18
|
-
yield
|
17
|
+
just_numbers = number.gsub(/[\.\/-]/, '')
|
18
|
+
zeros = ('0' * 11).slice(just_numbers.length, 11)
|
19
|
+
yield zeros.to_s if block_given?
|
19
20
|
end
|
20
21
|
|
21
22
|
def format_ie(number)
|
22
|
-
number.sub(/(\d{10})(\d{1})/,
|
23
|
+
number.sub(/(\d{10})(\d{1})/, '\\1-\\2')
|
23
24
|
end
|
24
25
|
end
|
25
26
|
end
|
data/lib/br_documents/ie/pa.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{6})(\d{1})/,
|
14
|
+
number.sub(/(\d{2})(\d{6})(\d{1})/, '\\1-\\2-\\3')
|
14
15
|
end
|
15
16
|
end
|
16
17
|
end
|
@@ -1,5 +1,5 @@
|
|
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
|
@@ -12,20 +12,23 @@ module BrDocuments
|
|
12
12
|
@weight = [9, 8, 7, 6, 5, 4, 3, 2]
|
13
13
|
end
|
14
14
|
|
15
|
-
|
15
|
+
protected
|
16
|
+
|
16
17
|
def valid_format?
|
17
18
|
regex = @mask
|
18
19
|
regex.match(@number).present?
|
19
20
|
end
|
20
21
|
|
21
|
-
def
|
22
|
-
calculated_check_digit =
|
23
|
-
check_digit_valid = check_digit
|
22
|
+
def valid_check_digit?
|
23
|
+
calculated_check_digit = generate_check_digit(only_numbers, @weight).to_s
|
24
|
+
check_digit_valid = check_digit == calculated_check_digit
|
24
25
|
remove_mask_of_number if check_digit_valid
|
25
26
|
|
26
27
|
check_digit_valid
|
27
28
|
end
|
28
29
|
|
30
|
+
private
|
31
|
+
|
29
32
|
def check_digit
|
30
33
|
only_numbers[-1]
|
31
34
|
end
|
@@ -35,7 +38,7 @@ module BrDocuments
|
|
35
38
|
end
|
36
39
|
|
37
40
|
def only_numbers
|
38
|
-
@number.gsub(/[\.\/-]/,
|
41
|
+
@number.gsub(/[\.\/-]/, '')
|
39
42
|
end
|
40
43
|
end
|
41
44
|
end
|
@@ -1,5 +1,5 @@
|
|
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
|
@@ -12,19 +12,20 @@ module BrDocuments
|
|
12
12
|
@weight2 = [5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]
|
13
13
|
end
|
14
14
|
|
15
|
-
|
15
|
+
protected
|
16
|
+
|
16
17
|
def valid_format?
|
17
18
|
regex = @mask
|
18
19
|
regex.match(@number).present?
|
19
20
|
end
|
20
21
|
|
21
|
-
def
|
22
|
-
@number.gsub!(/[\.\/-]/,
|
22
|
+
def valid_check_digit?
|
23
|
+
@number.gsub!(/[\.\/-]/, '')
|
23
24
|
|
24
|
-
digital_check1 =
|
25
|
-
digital_check2 =
|
25
|
+
digital_check1 = generate_check_digit(@number, @weight1)
|
26
|
+
digital_check2 = generate_check_digit(@number, @weight2)
|
26
27
|
|
27
|
-
@number[-2, 2]
|
28
|
+
@number[-2, 2] == "#{digital_check1}#{digital_check2}"
|
28
29
|
end
|
29
30
|
end
|
30
31
|
end
|
data/lib/br_documents/ie/pb.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 PB < 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/pe.rb
CHANGED
@@ -1,15 +1,11 @@
|
|
1
|
-
require_relative
|
2
|
-
require_relative
|
1
|
+
require_relative 'pattern1'
|
2
|
+
require_relative 'pattern2'
|
3
3
|
|
4
4
|
module BrDocuments
|
5
5
|
module IE
|
6
6
|
class PE
|
7
7
|
def initialize(number)
|
8
|
-
|
9
|
-
@validator = PE9.new(number)
|
10
|
-
else
|
11
|
-
@validator = PE14.new(number)
|
12
|
-
end
|
8
|
+
@validator = number.gsub(/[\.\/-]/, '').length <= 9 ? PE9.new(number) : PE14.new(number)
|
13
9
|
end
|
14
10
|
|
15
11
|
def valid?
|
@@ -20,7 +16,6 @@ module BrDocuments
|
|
20
16
|
@validator.formatted
|
21
17
|
end
|
22
18
|
|
23
|
-
private
|
24
19
|
class PE14 < Pattern1
|
25
20
|
def initialize(number)
|
26
21
|
super
|
@@ -29,10 +24,12 @@ module BrDocuments
|
|
29
24
|
end
|
30
25
|
|
31
26
|
private
|
27
|
+
|
32
28
|
def format_ie(number)
|
33
|
-
number.sub(/(\d{2})(\d{1})(\d{3})(\d{7})(\d{1})/,
|
29
|
+
number.sub(/(\d{2})(\d{1})(\d{3})(\d{7})(\d{1})/, '\\1.\\2.\\3.\\4-\\5')
|
34
30
|
end
|
35
31
|
end
|
32
|
+
private_constant :PE14
|
36
33
|
|
37
34
|
class PE9 < Pattern2
|
38
35
|
def initialize(number)
|
@@ -43,10 +40,13 @@ module BrDocuments
|
|
43
40
|
end
|
44
41
|
|
45
42
|
private
|
43
|
+
|
46
44
|
def format_ie(number)
|
47
|
-
number.sub(/(\d{7})(\d{2})/,
|
45
|
+
number.sub(/(\d{7})(\d{2})/, '\\1-\\2')
|
48
46
|
end
|
49
47
|
end
|
48
|
+
|
49
|
+
private_constant :PE9
|
50
50
|
end
|
51
51
|
end
|
52
52
|
end
|
data/lib/br_documents/ie/pi.rb
CHANGED
data/lib/br_documents/ie/pr.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require_relative
|
1
|
+
require_relative 'pattern2'
|
2
2
|
|
3
3
|
module BrDocuments
|
4
4
|
module IE
|
@@ -11,8 +11,9 @@ module BrDocuments
|
|
11
11
|
end
|
12
12
|
|
13
13
|
private
|
14
|
+
|
14
15
|
def format_ie(number)
|
15
|
-
number.sub(/(\d{8})(\d{2})/,
|
16
|
+
number.sub(/(\d{8})(\d{2})/, '\\1-\\2')
|
16
17
|
end
|
17
18
|
end
|
18
19
|
end
|
data/lib/br_documents/ie/rj.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require_relative
|
1
|
+
require_relative 'pattern1'
|
2
2
|
|
3
3
|
module BrDocuments
|
4
4
|
module IE
|
@@ -10,8 +10,9 @@ module BrDocuments
|
|
10
10
|
end
|
11
11
|
|
12
12
|
private
|
13
|
+
|
13
14
|
def format_ie(number)
|
14
|
-
number.sub(/(\d{2})(\d{3})(\d{2})(\d{1})/,
|
15
|
+
number.sub(/(\d{2})(\d{3})(\d{2})(\d{1})/, '\\1.\\2.\\3-\\4')
|
15
16
|
end
|
16
17
|
end
|
17
18
|
end
|
data/lib/br_documents/ie/rn.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 RN < 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{2})(\d{3})(\d{3})(\d{1})/,
|
12
|
+
if number.gsub(/(\.)|(\-)/, '').length == 9
|
13
|
+
number.sub(/(\d{2})(\d{3})(\d{3})(\d{1})/, '\\1.\\2.\\3-\\4')
|
13
14
|
else
|
14
|
-
number.sub(/(\d{2})(\d{1})(\d{3})(\d{3})(\d{1})/,
|
15
|
+
number.sub(/(\d{2})(\d{1})(\d{3})(\d{3})(\d{1})/, '\\1.\\2.\\3.\\4-\\5')
|
15
16
|
end
|
16
17
|
end
|
17
18
|
|
@@ -19,6 +20,17 @@ module BrDocuments
|
|
19
20
|
valid_old_format or valid_new_format
|
20
21
|
end
|
21
22
|
|
23
|
+
def valid_check_digit?
|
24
|
+
@number.gsub!(/[\.\/-]/, '')
|
25
|
+
|
26
|
+
weight = []
|
27
|
+
@number.length.downto(2).each { |w| weight << w }
|
28
|
+
|
29
|
+
@number[-1] == generate_check_digit(@number, weight).to_s
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
22
34
|
def valid_old_format
|
23
35
|
regex = /^(\d{2}\.\d{3}\.\d{3}\-\d{1})$|^(\d{9})$/
|
24
36
|
regex.match(@number).present?
|
@@ -29,16 +41,7 @@ module BrDocuments
|
|
29
41
|
regex.match(@number).present?
|
30
42
|
end
|
31
43
|
|
32
|
-
def
|
33
|
-
@number.gsub!(/[\.\/-]/, "")
|
34
|
-
|
35
|
-
weight = []
|
36
|
-
@number.length.downto(2).each {|w| weight << w }
|
37
|
-
|
38
|
-
@number[-1].eql? generate_digital_check(@number, weight).to_s
|
39
|
-
end
|
40
|
-
|
41
|
-
def generate_digital_check(values, weights)
|
44
|
+
def generate_check_digit(values, weights)
|
42
45
|
sum = reduce_weights(values, weights)
|
43
46
|
mod = sum * 10 % 11
|
44
47
|
mod == 10 ? 0 : mod
|
data/lib/br_documents/ie/ro.rb
CHANGED
@@ -1,17 +1,18 @@
|
|
1
|
-
require_relative
|
2
|
-
require_relative
|
1
|
+
require_relative 'base'
|
2
|
+
require_relative '../commons/mod14'
|
3
3
|
|
4
4
|
module BrDocuments
|
5
5
|
module IE
|
6
6
|
class RO < Base
|
7
7
|
include Commons::Mod14
|
8
8
|
|
9
|
-
|
9
|
+
protected
|
10
|
+
|
10
11
|
def format_ie(number)
|
11
|
-
if number.gsub(/(\.)|(\-)/,
|
12
|
-
number.sub(/(\d{3})(\d{5})(\d{1})/,
|
12
|
+
if number.gsub(/(\.)|(\-)/, '').length == 9
|
13
|
+
number.sub(/(\d{3})(\d{5})(\d{1})/, '\\1.\\2-\\3')
|
13
14
|
else
|
14
|
-
number.sub(/(\d{13})(\d{1})/,
|
15
|
+
number.sub(/(\d{13})(\d{1})/, '\\1-\\2')
|
15
16
|
end
|
16
17
|
end
|
17
18
|
|
@@ -19,6 +20,12 @@ module BrDocuments
|
|
19
20
|
valid_old_format or valid_new_format
|
20
21
|
end
|
21
22
|
|
23
|
+
def valid_check_digit?
|
24
|
+
valid_old_digital_check or valid_new_digital_check
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
22
29
|
def valid_old_format
|
23
30
|
regex = /^(\d{3}\.\d{5}\-\d{1})$|^(\d{9})$/
|
24
31
|
regex.match(@number).present?
|
@@ -29,24 +36,20 @@ module BrDocuments
|
|
29
36
|
regex.match(@number).present?
|
30
37
|
end
|
31
38
|
|
32
|
-
def valid_digital_check?
|
33
|
-
valid_old_digital_check or valid_new_digital_check
|
34
|
-
end
|
35
|
-
|
36
39
|
def valid_old_digital_check
|
37
40
|
weight = [3, 2, 9, 8, 7, 6, 5, 4, 3, 2]
|
38
41
|
remove_mask!
|
39
|
-
@number[-1]
|
42
|
+
@number[-1] == generate_check_digit(@number[-6, 6], weight).to_s
|
40
43
|
end
|
41
44
|
|
42
45
|
def valid_new_digital_check
|
43
46
|
weight = [6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]
|
44
47
|
remove_mask!
|
45
|
-
@number[-1]
|
48
|
+
@number[-1] == generate_check_digit(@number, weight).to_s
|
46
49
|
end
|
47
50
|
|
48
51
|
def remove_mask!
|
49
|
-
@number.gsub!(/[\.\/-]/,
|
52
|
+
@number.gsub!(/[\.\/-]/, '')
|
50
53
|
end
|
51
54
|
end
|
52
55
|
end
|
data/lib/br_documents/ie/rr.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require_relative
|
1
|
+
require_relative 'pattern1'
|
2
2
|
|
3
3
|
module BrDocuments
|
4
4
|
module IE
|
@@ -10,11 +10,12 @@ module BrDocuments
|
|
10
10
|
end
|
11
11
|
|
12
12
|
private
|
13
|
+
|
13
14
|
def format_ie(number)
|
14
|
-
number.sub(/(\d{8})(\d{1})/,
|
15
|
+
number.sub(/(\d{8})(\d{1})/, '\\1-\\2')
|
15
16
|
end
|
16
17
|
|
17
|
-
def
|
18
|
+
def generate_check_digit(values, weights)
|
18
19
|
sum = reduce_weights(values, weights)
|
19
20
|
sum % 9
|
20
21
|
end
|
data/lib/br_documents/ie/rs.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require_relative
|
1
|
+
require_relative 'pattern1'
|
2
2
|
|
3
3
|
module BrDocuments
|
4
4
|
module IE
|
@@ -10,8 +10,9 @@ module BrDocuments
|
|
10
10
|
end
|
11
11
|
|
12
12
|
private
|
13
|
+
|
13
14
|
def format_ie(number)
|
14
|
-
number.sub(/(\d{3})(\d{7})/,
|
15
|
+
number.sub(/(\d{3})(\d{7})/, '\\1/\\2')
|
15
16
|
end
|
16
17
|
end
|
17
18
|
end
|
data/lib/br_documents/ie/sc.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{3})(\d{3})(\d{3})/,
|
14
|
+
number.sub(/(\d{3})(\d{3})(\d{3})/, '\\1.\\2.\\3')
|
14
15
|
end
|
15
16
|
end
|
16
17
|
end
|
data/lib/br_documents/ie/se.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 SE < 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
|