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,8 +1,8 @@
1
1
  class CnpjValidator < ActiveModel::EachValidator
2
2
  def validate_each(record, attribute, value)
3
- if value.present?
4
- cnpj = BrDocuments::CnpjCpf::Cnpj.new(value)
5
- record.errors.add(attribute, :invalid) if not cnpj.valid?
6
- end
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
@@ -1,8 +1,7 @@
1
1
  module BrDocuments
2
2
  module Commons
3
3
  module Mod11
4
-
5
- def generate_digital_check(values, weights)
4
+ def generate_check_digit(values, weights)
6
5
  sum = reduce_weights(values, weights)
7
6
  mod = sum % 11
8
7
  mod < 2 ? 0 : (11 - mod)
@@ -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.present?
4
- cpf = BrDocuments::CnpjCpf::Cpf.new(value)
5
- record.errors.add(attribute, :invalid) if not cpf.valid?
6
- end
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
@@ -0,0 +1,8 @@
1
+ class GtinValidator < ActiveModel::EachValidator
2
+ def validate_each(record, attribute, value)
3
+ return if value.blank?
4
+
5
+ gtin = BrDocuments::Gtin.new(value)
6
+ record.errors.add(attribute, :invalid) unless gtin.valid?
7
+ end
8
+ end
@@ -1,4 +1,4 @@
1
- require_relative "pattern2"
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})/, "\\1.\\2.\\3/\\4-\\5")
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
@@ -1,4 +1,4 @@
1
- require_relative "pattern1"
1
+ require_relative 'pattern1'
2
2
 
3
3
  module BrDocuments
4
4
  module IE
@@ -9,6 +9,7 @@ module BrDocuments
9
9
  end
10
10
 
11
11
  private
12
+
12
13
  def format_ie(number)
13
14
  number
14
15
  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{3})(\d{3})(\d{1})/, "\\1.\\2.\\3-\\4")
14
+ number.sub(/(\d{2})(\d{3})(\d{3})(\d{1})/, '\\1.\\2.\\3-\\4')
14
15
  end
15
16
  end
16
17
  end
@@ -1,12 +1,13 @@
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 AP < Base
7
7
  include Commons::Mod11
8
8
 
9
- private
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 valid_digital_check?
20
+ def valid_check_digit?
20
21
  weight = [9, 8, 7, 6, 5, 4, 3, 2]
21
22
  detect_range_digits
22
- @number[-1].eql? generate_digital_check(@number, weight).to_s
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
- if number.to_i >= 3000001 and number.to_i <= 3017000
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
- else
31
- if number.to_i >= 3017001 and number.to_i <= 3019022
32
- @p = 9
33
- @d = 1
34
- else
35
- @p = 0
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 generate_digital_check(values, weights)
45
+ def generate_check_digit(values, weights)
42
46
  sum = reduce_weights(values, weights)
43
47
  sum += @p
44
48
  mod = sum % 11
@@ -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 BA < Base
7
7
  include Commons::Mod11
8
8
 
9
- private
9
+ protected
10
+
10
11
  def format_ie(number)
11
- if number.gsub(/-/, "").length == 8
12
- number.sub(/(\d{6})(\d{2})/, "\\1-\\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})/, "\\1-\\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 valid_9_digits_format
23
- regex_mask_1 = /^(\d{7}\-\d{2})$|^(\d{9})$/
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].eql? "#{digital_check1}#{digital_check2}"
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(/[\.\/-]/, "").length
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
- generate_digital_check_mod10(number, weight)
67
+ generate_check_digit_mod10(number, weight)
65
68
  else
66
- generate_digital_check(number, weight)
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 generate_digital_check_mod10(values, weights)
86
+ def generate_check_digit_mod10(values, weights)
84
87
  sum = reduce_weights(values, weights)
85
88
  mod = sum % 10
86
- mod == 0 ? 0 : (10 - mod)
89
+ mod.zero? ? 0 : (10 - mod)
87
90
  end
88
91
  end
89
92
  end
@@ -6,7 +6,7 @@ module BrDocuments
6
6
  end
7
7
 
8
8
  def valid?
9
- valid_format? and valid_digital_check?
9
+ valid_format? and valid_check_digit?
10
10
  end
11
11
 
12
12
  def formatted
@@ -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 CE < 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,4 +1,4 @@
1
- require_relative "pattern2"
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})/, "\\1-\\2")
14
+ number.sub(/(\d{11})(\d{2})/, '\\1-\\2')
14
15
  end
15
16
  end
16
17
  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 ES < Pattern1
6
6
  private
7
+
7
8
  def format_ie(number)
8
9
  number
9
10
  end
@@ -1,11 +1,11 @@
1
1
  module BrDocuments
2
2
  module IE
3
3
  class Factory
4
- def self.create(uf, number)
5
- klass = IE.const_get("#{uf}")
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("validator.ie.uf.invalid"))
8
+ raise(ArgumentError, I18n.t('validator.ie.uf.invalid'))
9
9
  end
10
10
  end
11
11
  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{3})(\d{3})(\d{1})/, "\\1.\\2.\\3-\\4")
14
+ number.sub(/(\d{2})(\d{3})(\d{3})(\d{1})/, '\\1.\\2.\\3-\\4')
14
15
  end
15
16
  end
16
17
  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 MA < Pattern1
6
6
  private
7
+
7
8
  def format_ie(number)
8
9
  number
9
10
  end
@@ -1,14 +1,15 @@
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 MG < Base
7
7
  include Commons::Mod11
8
8
 
9
- private
9
+ protected
10
+
10
11
  def format_ie(number)
11
- number.sub(/(\d{3})(\d{3})(\d{3})(\d{4})/, "\\1.\\2.\\3/\\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 valid_digital_check?
20
- @number.gsub!(/[\.\/-]/, "")
20
+ def valid_check_digit?
21
+ @number.gsub!(/[\.\/-]/, '')
21
22
 
22
- number1 = @number[0, 3] + "0" + @number[3..10]
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 = generate_digital_check(number2, weight2)
29
+ digital_check2 = generate_check_digit(number2, weight2)
29
30
 
30
- @number[-2, 2].eql? "#{digital_check1}#{digital_check2}"
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
- if number < 10
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