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.
Files changed (98) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/config.yml +54 -0
  3. data/.gitignore +1 -1
  4. data/.rubocop.yml +65 -0
  5. data/Gemfile +1 -1
  6. data/Gemfile.lock +100 -0
  7. data/README.md +58 -1
  8. data/Rakefile +3 -3
  9. data/br_documents.gemspec +23 -19
  10. data/lib/br_documents.rb +3 -3
  11. data/lib/br_documents/cnpj_cpf/cnpj.rb +2 -2
  12. data/lib/br_documents/cnpj_cpf/cpf.rb +2 -2
  13. data/lib/br_documents/cnpj_cpf/document.rb +9 -9
  14. data/lib/br_documents/cnpj_validator.rb +4 -4
  15. data/lib/br_documents/commons/mod11.rb +1 -2
  16. data/lib/br_documents/commons/mod14.rb +20 -0
  17. data/lib/br_documents/cpf_validator.rb +4 -4
  18. data/lib/br_documents/gtin/gtin.rb +28 -0
  19. data/lib/br_documents/gtin_validator.rb +8 -0
  20. data/lib/br_documents/ie/ac.rb +3 -2
  21. data/lib/br_documents/ie/al.rb +2 -1
  22. data/lib/br_documents/ie/am.rb +3 -2
  23. data/lib/br_documents/ie/ap.rb +20 -16
  24. data/lib/br_documents/ie/ba.rb +28 -25
  25. data/lib/br_documents/ie/base.rb +1 -1
  26. data/lib/br_documents/ie/ce.rb +3 -2
  27. data/lib/br_documents/ie/df.rb +3 -2
  28. data/lib/br_documents/ie/es.rb +2 -1
  29. data/lib/br_documents/ie/factory.rb +3 -3
  30. data/lib/br_documents/ie/go.rb +3 -2
  31. data/lib/br_documents/ie/ma.rb +2 -1
  32. data/lib/br_documents/ie/mg.rb +14 -17
  33. data/lib/br_documents/ie/ms.rb +2 -1
  34. data/lib/br_documents/ie/mt.rb +6 -5
  35. data/lib/br_documents/ie/pa.rb +3 -2
  36. data/lib/br_documents/ie/pattern1.rb +10 -7
  37. data/lib/br_documents/ie/pattern2.rb +9 -8
  38. data/lib/br_documents/ie/pb.rb +3 -2
  39. data/lib/br_documents/ie/pe.rb +10 -10
  40. data/lib/br_documents/ie/pi.rb +2 -1
  41. data/lib/br_documents/ie/pr.rb +3 -2
  42. data/lib/br_documents/ie/rj.rb +3 -2
  43. data/lib/br_documents/ie/rn.rb +19 -16
  44. data/lib/br_documents/ie/ro.rb +17 -14
  45. data/lib/br_documents/ie/rr.rb +4 -3
  46. data/lib/br_documents/ie/rs.rb +3 -2
  47. data/lib/br_documents/ie/sc.rb +3 -2
  48. data/lib/br_documents/ie/se.rb +3 -2
  49. data/lib/br_documents/ie/sp.rb +17 -14
  50. data/lib/br_documents/ie/to.rb +6 -5
  51. data/lib/br_documents/ie_validator.rb +16 -15
  52. data/lib/br_documents/suframa/suframa.rb +60 -0
  53. data/lib/br_documents/suframa_validator.rb +8 -0
  54. data/lib/br_documents/version.rb +1 -1
  55. data/spec/cnpj_cpf/cnpj_spec.rb +21 -21
  56. data/spec/cnpj_cpf/cpf_spec.rb +21 -21
  57. data/spec/cnpj_validator_spec.rb +12 -12
  58. data/spec/cpf_validator_spec.rb +12 -12
  59. data/spec/gtin/gtin_spec.rb +49 -0
  60. data/spec/gtin_validator_spec.rb +47 -0
  61. data/spec/ie/ac_spec.rb +16 -16
  62. data/spec/ie/al_spec.rb +14 -14
  63. data/spec/ie/am_spec.rb +16 -16
  64. data/spec/ie/ap_spec.rb +14 -14
  65. data/spec/ie/ba_spec.rb +28 -28
  66. data/spec/ie/ce_spec.rb +9 -9
  67. data/spec/ie/df_spec.rb +16 -16
  68. data/spec/ie/es_spec.rb +9 -9
  69. data/spec/ie/factory_spec.rb +88 -89
  70. data/spec/ie/go_spec.rb +16 -16
  71. data/spec/ie/ma_spec.rb +9 -9
  72. data/spec/ie/mg_spec.rb +16 -16
  73. data/spec/ie/ms_spec.rb +9 -9
  74. data/spec/ie/mt_spec.rb +17 -17
  75. data/spec/ie/pa_spec.rb +16 -16
  76. data/spec/ie/pb_spec.rb +9 -9
  77. data/spec/ie/pe_spec.rb +27 -27
  78. data/spec/ie/pi_spec.rb +9 -9
  79. data/spec/ie/pr_spec.rb +14 -14
  80. data/spec/ie/rj_spec.rb +16 -16
  81. data/spec/ie/rn_spec.rb +27 -27
  82. data/spec/ie/ro_spec.rb +27 -27
  83. data/spec/ie/rr_spec.rb +16 -16
  84. data/spec/ie/rs_spec.rb +16 -16
  85. data/spec/ie/sc_spec.rb +16 -16
  86. data/spec/ie/se_spec.rb +9 -9
  87. data/spec/ie/shared_examples_for_pattern1.rb +11 -11
  88. data/spec/ie/shared_examples_for_to_remove_all_masks.rb +7 -8
  89. data/spec/ie/sp_spec.rb +19 -19
  90. data/spec/ie/to_spec.rb +35 -35
  91. data/spec/ie_validator_spec.rb +43 -31
  92. data/spec/spec_helper.rb +9 -6
  93. data/spec/suframa/suframa_spec.rb +109 -0
  94. data/spec/suframa_validator_spec.rb +36 -0
  95. data/spec/support/utilities.rb +2 -2
  96. metadata +94 -25
  97. data/.travis.yml +0 -6
  98. data/circle.yml +0 -3
@@ -1,9 +1,10 @@
1
- require_relative "pattern1"
1
+ require_relative 'pattern1'
2
2
 
3
3
  module BrDocuments
4
4
  module IE
5
5
  class MS < Pattern1
6
6
  private
7
+
7
8
  def format_ie(number)
8
9
  number
9
10
  end
@@ -1,4 +1,4 @@
1
- require_relative "pattern1"
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 = ("0" * 11).slice(just_numbers.length, 11)
18
- yield "#{zeros}" if block_given?
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})/, "\\1-\\2")
23
+ number.sub(/(\d{10})(\d{1})/, '\\1-\\2')
23
24
  end
24
25
  end
25
26
  end
@@ -1,4 +1,4 @@
1
- require_relative "pattern1"
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})/, "\\1-\\2-\\3")
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 "base"
2
- require_relative "../commons/mod11"
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
- private
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 valid_digital_check?
22
- calculated_check_digit = generate_digital_check(only_numbers, @weight).to_s
23
- check_digit_valid = check_digit.eql? calculated_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 "base"
2
- require_relative "../commons/mod11"
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
- private
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 valid_digital_check?
22
- @number.gsub!(/[\.\/-]/, "")
22
+ def valid_check_digit?
23
+ @number.gsub!(/[\.\/-]/, '')
23
24
 
24
- digital_check1 = generate_digital_check(@number, @weight1)
25
- digital_check2 = generate_digital_check(@number, @weight2)
25
+ digital_check1 = generate_check_digit(@number, @weight1)
26
+ digital_check2 = generate_check_digit(@number, @weight2)
26
27
 
27
- @number[-2, 2].eql? "#{digital_check1}#{digital_check2}"
28
+ @number[-2, 2] == "#{digital_check1}#{digital_check2}"
28
29
  end
29
30
  end
30
31
  end
@@ -1,11 +1,12 @@
1
- require_relative "pattern1"
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})/, "\\1-\\2")
9
+ number.sub(/(\d{8})(\d{1})/, '\\1-\\2')
9
10
  end
10
11
  end
11
12
  end
@@ -1,15 +1,11 @@
1
- require_relative "pattern1"
2
- require_relative "pattern2"
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
- if number.gsub(/[\.\/-]/, "").length <= 9
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})/, "\\1.\\2.\\3.\\4-\\5")
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})/, "\\1-\\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
@@ -1,9 +1,10 @@
1
- require_relative "pattern1"
1
+ require_relative 'pattern1'
2
2
 
3
3
  module BrDocuments
4
4
  module IE
5
5
  class PI < Pattern1
6
6
  private
7
+
7
8
  def format_ie(number)
8
9
  number
9
10
  end
@@ -1,4 +1,4 @@
1
- require_relative "pattern2"
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})/, "\\1-\\2")
16
+ number.sub(/(\d{8})(\d{2})/, '\\1-\\2')
16
17
  end
17
18
  end
18
19
  end
@@ -1,4 +1,4 @@
1
- require_relative "pattern1"
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})/, "\\1.\\2.\\3-\\4")
15
+ number.sub(/(\d{2})(\d{3})(\d{2})(\d{1})/, '\\1.\\2.\\3-\\4')
15
16
  end
16
17
  end
17
18
  end
@@ -1,17 +1,18 @@
1
- require_relative "base"
2
- require_relative "../commons/mod11"
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
- private
9
+ protected
10
+
10
11
  def format_ie(number)
11
- if number.gsub(/(\.)|(\-)/, "").length == 9
12
- number.sub(/(\d{2})(\d{3})(\d{3})(\d{1})/, "\\1.\\2.\\3-\\4")
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})/, "\\1.\\2.\\3.\\4-\\5")
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 valid_digital_check?
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
@@ -1,17 +1,18 @@
1
- require_relative "base"
2
- require_relative "../commons/mod11"
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
- include Commons::Mod11
7
+ include Commons::Mod14
8
+
9
+ protected
8
10
 
9
- private
10
11
  def format_ie(number)
11
- if number.gsub(/(\.)|(\-)/, "").length == 9
12
- number.sub(/(\d{3})(\d{5})(\d{1})/, "\\1.\\2-\\3")
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})/, "\\1-\\2")
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].eql? generate_digital_check(@number[-6, 6], weight).to_s
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].eql? generate_digital_check(@number, weight).to_s
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
@@ -1,4 +1,4 @@
1
- require_relative "pattern1"
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})/, "\\1-\\2")
15
+ number.sub(/(\d{8})(\d{1})/, '\\1-\\2')
15
16
  end
16
17
 
17
- def generate_digital_check(values, weights)
18
+ def generate_check_digit(values, weights)
18
19
  sum = reduce_weights(values, weights)
19
20
  sum % 9
20
21
  end
@@ -1,4 +1,4 @@
1
- require_relative "pattern1"
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})/, "\\1/\\2")
15
+ number.sub(/(\d{3})(\d{7})/, '\\1/\\2')
15
16
  end
16
17
  end
17
18
  end
@@ -1,4 +1,4 @@
1
- require_relative "pattern1"
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})/, "\\1.\\2.\\3")
14
+ number.sub(/(\d{3})(\d{3})(\d{3})/, '\\1.\\2.\\3')
14
15
  end
15
16
  end
16
17
  end
@@ -1,11 +1,12 @@
1
- require_relative "pattern1"
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})/, "\\1-\\2")
9
+ number.sub(/(\d{8})(\d{1})/, '\\1-\\2')
9
10
  end
10
11
  end
11
12
  end