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
data/spec/ie/sp_spec.rb CHANGED
@@ -1,45 +1,45 @@
1
- require "spec_helper"
2
- require_relative "shared_examples_for_to_remove_all_masks"
1
+ require 'spec_helper'
2
+ require_relative 'shared_examples_for_to_remove_all_masks'
3
3
 
4
4
  describe BrDocuments::IE::SP do
5
- describe "#formatted" do
6
- context "when having 12 digits" do
7
- it "returns a formatted ie" do
8
- ie = described_class.new("110042490114")
9
- expect(ie.formatted).to eq "110.042.490.114"
5
+ describe '#formatted' do
6
+ context 'when having 12 digits' do
7
+ it 'returns a formatted ie' do
8
+ ie = described_class.new('110042490114')
9
+ expect(ie.formatted).to eq '110.042.490.114'
10
10
  end
11
11
  end
12
12
 
13
- context "when having 13 digits" do
14
- it "returns a formatted ie" do
15
- ie = described_class.new("P011004243002")
16
- expect(ie.formatted).to eq "P-01100424.3/002"
13
+ context 'when having 13 digits' do
14
+ it 'returns a formatted ie' do
15
+ ie = described_class.new('P011004243002')
16
+ expect(ie.formatted).to eq 'P-01100424.3/002'
17
17
  end
18
18
  end
19
19
  end
20
20
 
21
- describe "#valid?" do
22
- it "is invalid with malformed number" do
23
- ["36.9107.395114", "527.08212.8110"].each do |number|
21
+ describe '#valid?' do
22
+ it 'is invalid with malformed number' do
23
+ ['36.9107.395114', '527.08212.8110'].each do |number|
24
24
  ie = described_class.new(number)
25
25
  expect(ie).to_not be_valid
26
26
  end
27
27
  end
28
28
 
29
- it "is invalid with invalid check number" do
30
- ["110042490113", "110.042.491.114", "P-01100424.4/002"].each do |number|
29
+ it 'is invalid with invalid check number' do
30
+ ['110042490113', '110.042.491.114', 'P-01100424.4/002'].each do |number|
31
31
  ie = described_class.new(number)
32
32
  expect(ie).to_not be_valid
33
33
  end
34
34
  end
35
35
 
36
- it "is valid with valid number" do
37
- ["110042490114", "110.042.490.114", "P-01100424.3/002"].each do |number|
36
+ it 'is valid with valid number' do
37
+ ['110042490114', '110.042.490.114', 'P-01100424.3/002'].each do |number|
38
38
  ie = described_class.new(number)
39
39
  expect(ie).to be_valid
40
40
  end
41
41
  end
42
42
 
43
- include_examples "for to remove all masks", "P-01100424.3/002"
43
+ include_examples 'for to remove all masks', 'P-01100424.3/002'
44
44
  end
45
45
  end
data/spec/ie/to_spec.rb CHANGED
@@ -1,100 +1,100 @@
1
- require "spec_helper"
2
- require_relative "shared_examples_for_to_remove_all_masks"
1
+ require 'spec_helper'
2
+ require_relative 'shared_examples_for_to_remove_all_masks'
3
3
 
4
4
  describe BrDocuments::IE::TO do
5
- describe "#formatted" do
6
- context "when having 9 digits" do
7
- subject { described_class.new("123456789") }
5
+ describe '#formatted' do
6
+ context 'when having 9 digits' do
7
+ subject { described_class.new('123456789') }
8
8
 
9
- it "returns a formatted ie" do
10
- expect(subject.formatted).to eq "12.345678-9"
9
+ it 'returns a formatted ie' do
10
+ expect(subject.formatted).to eq '12.345678-9'
11
11
  end
12
12
  end
13
13
 
14
- context "when having 11 digits" do
15
- subject { described_class.new("12345678901") }
14
+ context 'when having 11 digits' do
15
+ subject { described_class.new('12345678901') }
16
16
 
17
- it "returns a formatted ie" do
18
- expect(subject.formatted).to eq "12.34.567890-1"
17
+ it 'returns a formatted ie' do
18
+ expect(subject.formatted).to eq '12.34.567890-1'
19
19
  end
20
20
  end
21
21
  end
22
22
 
23
- describe "#valid?" do
24
- it "is invalid with malformed number" do
25
- ["2.903.593.362-3", "29.02.79268-45", "29.02.5AB420-5"].each do |number|
23
+ describe '#valid?' do
24
+ it 'is invalid with malformed number' do
25
+ ['2.903.593.362-3', '29.02.79268-45', '29.02.5AB420-5'].each do |number|
26
26
  ie = described_class.new(number)
27
27
  expect(ie).to_not be_valid
28
28
  end
29
29
  end
30
30
 
31
- it "is invalid with length different of 9 or 11" do
32
- ["12345678", "1234567890", "123456789012"].each do |number|
31
+ it 'is invalid with length different of 9 or 11' do
32
+ ['12345678', '1234567890', '123456789012'].each do |number|
33
33
  ie = described_class.new(number)
34
34
  expect(ie).to_not be_valid
35
35
  end
36
36
  end
37
37
 
38
- context "when having 9 digits" do
39
- it "is invalid with invalid check number" do
40
- ["290227835", "294083153"].each do |number|
38
+ context 'when having 9 digits' do
39
+ it 'is invalid with invalid check number' do
40
+ ['290227835', '294083153'].each do |number|
41
41
  ie = described_class.new(number)
42
42
  expect(ie).to_not be_valid
43
43
  end
44
44
  end
45
45
 
46
- it "is invalid with correct check number but invalid mask" do
47
- ["29-022783-6", "29.408315.4"].each do |number|
46
+ it 'is invalid with correct check number but invalid mask' do
47
+ ['29-022783-6', '29.408315.4'].each do |number|
48
48
  ie = described_class.new(number)
49
49
  expect(ie).to_not be_valid
50
50
  end
51
51
  end
52
52
 
53
- it "is valid with valid number" do
54
- ["290227836", "294083154"].each do |number|
53
+ it 'is valid with valid number' do
54
+ ['290227836', '294083154'].each do |number|
55
55
  ie = described_class.new(number)
56
56
  expect(ie).to be_valid
57
57
  end
58
58
  end
59
59
 
60
- it "is valid with valid masked number" do
61
- ["29.022783-6", "29.408315-4"].each do |number|
60
+ it 'is valid with valid masked number' do
61
+ ['29.022783-6', '29.408315-4'].each do |number|
62
62
  ie = described_class.new(number)
63
63
  expect(ie).to be_valid
64
64
  end
65
65
  end
66
66
  end
67
67
 
68
- context "when having 11 digits" do
69
- it "is invalid with invalid check number" do
70
- ["29023864406", "29023864409"].each do |number|
68
+ context 'when having 11 digits' do
69
+ it 'is invalid with invalid check number' do
70
+ ['29023864406', '29023864409'].each do |number|
71
71
  ie = described_class.new(number)
72
72
  expect(ie).to_not be_valid
73
73
  end
74
74
  end
75
75
 
76
- it "is invalid with correct check number but invalid mask" do
77
- ["29-99.200143-6", "29.99.231741.7"].each do |number|
76
+ it 'is invalid with correct check number but invalid mask' do
77
+ ['29-99.200143-6', '29.99.231741.7'].each do |number|
78
78
  ie = described_class.new(number)
79
79
  expect(ie).to_not be_valid
80
80
  end
81
81
  end
82
82
 
83
- it "is valid with valid number" do
84
- ["29992001436", "29992317417"].each do |number|
83
+ it 'is valid with valid number' do
84
+ ['29992001436', '29992317417'].each do |number|
85
85
  ie = described_class.new(number)
86
86
  expect(ie).to be_valid
87
87
  end
88
88
  end
89
89
 
90
- it "is valid with valid masked number" do
91
- ["29.99.200143-6", "29.99.231741-7"].each do |number|
90
+ it 'is valid with valid masked number' do
91
+ ['29.99.200143-6', '29.99.231741-7'].each do |number|
92
92
  ie = described_class.new(number)
93
93
  expect(ie).to be_valid
94
94
  end
95
95
  end
96
96
  end
97
97
 
98
- include_examples "for to remove all masks", "29.99.231741-7"
98
+ include_examples 'for to remove all masks', '29.99.231741-7'
99
99
  end
100
100
  end
@@ -1,73 +1,85 @@
1
- require "spec_helper"
1
+ require 'spec_helper'
2
2
 
3
- describe IeValidator do
4
- let(:record) { double("model") }
3
+ RSpec.describe IeValidator do
4
+ let(:record) { double('model') }
5
5
 
6
6
  before do
7
- allow(record).to receive(:uf) {"SC"}
8
- allow(record).to receive(:errors).and_return([])
7
+ allow(record).to receive(:uf) { 'SC' }
8
+ allow(record).to receive(:errors).and_return({})
9
9
  allow(record.errors).to receive(:messages).and_return({})
10
- allow(record.errors).to receive(:add) do | attribute, error |
11
- record.errors.messages[attribute] = [error]
10
+ allow(record.errors).to receive(:add) do |attribute, error|
11
+ record.errors[attribute] ||= []
12
+ record.errors[attribute] << error
13
+ record.errors.messages[attribute] = record.errors[attribute]
12
14
  end
13
15
  end
14
16
 
15
- subject { IeValidator.new(attributes: "ie", uf: "uf") }
17
+ subject { IeValidator.new(attributes: 'ie', uf: 'uf') }
16
18
 
17
19
  context "when IE's number is valid" do
18
- before { subject.validate_each(record, "ie", "253667852") }
20
+ before { subject.validate_each(record, 'ie', '253667852') }
19
21
 
20
- it "doesn't add errors in model" do
22
+ it 'does not add errors in model' do
21
23
  expect(record.errors.messages).to be_empty
22
24
  end
23
25
  end
24
26
 
25
- context "when IE is blank" do
26
- before { subject.validate_each(record, "ie", "") }
27
+ context 'when IE is blank' do
28
+ before { subject.validate_each(record, 'ie', '') }
27
29
 
28
- it "doesn't add errors in model" do
30
+ it 'does not add errors in model' do
29
31
  expect(record.errors.messages).to be_empty
30
32
  end
31
33
  end
32
34
 
33
- context "when IE is invalid" do
34
- before { subject.validate_each(record, "ie", "253667853") }
35
+ context 'when IE is invalid' do
36
+ it 'adds :invalid error in the model' do
37
+ subject.validate_each(record, 'ie', '253667853')
35
38
 
36
- it "adds errors in model" do
37
- expect(record.errors.messages).to_not be_empty
39
+ expect(record.errors.messages['ie']).to include(:invalid)
40
+ end
41
+
42
+ it 'does not override the previous message' do
43
+ record.errors.add('ie', 'another error message')
44
+ subject.validate_each(record, 'ie', '253667853')
45
+
46
+ expect(record.errors.messages['ie']).to include('another error message')
47
+ expect(record.errors.messages['ie']).to include(:invalid)
38
48
  end
39
49
  end
40
50
 
41
- context "when UF is invalid" do
51
+ context 'when UF is invalid' do
42
52
  before do
43
- allow(record).to receive(:uf){""}
44
- subject.validate_each(record, "ie", "253667852")
53
+ allow(record).to receive(:uf) { 'XX' }
54
+ subject.validate_each(record, 'ie', '253667852')
45
55
  end
46
56
 
47
- it "adds error in model" do
48
- expect(record.errors.messages["ie"]).to eql [t("validator.ie.uf.invalid")]
57
+ it 'adds error in model' do
58
+ expect(record.errors.messages['ie']).to include t('validator.ie.uf.invalid')
49
59
  end
50
60
  end
51
61
 
52
- context "when it can't find attribute uf in model" do
62
+ context 'when cannot be found attribute uf in model' do
53
63
  before do
54
64
  allow(record).to receive(:uf).and_raise(NoMethodError)
55
- subject.validate_each(record, "ie", "253667852")
65
+ subject.validate_each(record, 'ie', '253667852')
56
66
  end
57
67
 
58
- it "adds error in model" do
68
+ it 'adds error in model' do
59
69
  expect(record.errors.messages[:base]).to eql [
60
- t("validator.ie.uf.no_present", uf: "uf")]
70
+ t('validator.ie.uf.no_present', uf: 'uf')
71
+ ]
61
72
  end
62
73
  end
63
74
 
64
- context "when attribute uf is not defined" do
65
- subject { IeValidator.new(attributes: "ie") }
66
- before { subject.validate_each(record, "ie", "253667852") }
75
+ context 'when attribute uf is not defined' do
76
+ subject { IeValidator.new(attributes: 'ie') }
77
+ before { subject.validate_each(record, 'ie', '253667852') }
67
78
 
68
- it "adds error in model" do
79
+ it 'adds error in model' do
69
80
  expect(record.errors.messages[:base]).to eql [
70
- t("validator.ie.uf.no_configured")]
81
+ t('validator.ie.uf.no_configured')
82
+ ]
71
83
  end
72
84
  end
73
85
  end
data/spec/spec_helper.rb CHANGED
@@ -1,19 +1,22 @@
1
- require "simplecov"
1
+ require 'simplecov'
2
2
  SimpleCov.start
3
3
 
4
- require "bundler/setup"
4
+ require 'bundler/setup'
5
5
  Bundler.require(:default, :development)
6
6
 
7
- require "br_documents"
7
+ require 'br_documents'
8
8
 
9
- Dir[File.dirname(__FILE__) + "/support/*.rb"].each { |f| require f }
9
+ Dir["#{File.dirname(__FILE__)}/support/*.rb"].sort.each { |f| require f }
10
10
 
11
- I18n.default_locale = "pt-BR"
11
+ I18n.default_locale = 'pt-BR'
12
12
 
13
13
  RSpec.configure do |config|
14
14
  config.expect_with :rspec do |c|
15
15
  c.syntax = :expect
16
16
  end
17
17
 
18
- config.order = "random"
18
+ config.filter_run focus: true
19
+ config.run_all_when_everything_filtered = true
20
+
21
+ config.order = 'random'
19
22
  end
@@ -0,0 +1,109 @@
1
+ require 'spec_helper'
2
+
3
+ describe BrDocuments::Suframa do
4
+ subject { described_class.new }
5
+
6
+ describe '#formatted' do
7
+ it 'returns the formatted number' do
8
+ value = '100698107'
9
+
10
+ subject = described_class.new(value)
11
+
12
+ expect(subject.formatted).to eql '10.0698.107'
13
+ end
14
+
15
+ context 'when number is invalid' do
16
+ it 'returns a empty string' do
17
+ value = '123'
18
+
19
+ subject = described_class.new(value)
20
+
21
+ expect(subject.formatted).to eql ''
22
+ end
23
+ end
24
+
25
+ it 'is invalid if number is malformed' do
26
+ ['121.23.4123', '5.50.309.012', '5.503.090.12'].each do |number|
27
+ suframa = BrDocuments::Suframa.new(number)
28
+
29
+ expect(suframa).to_not be_valid
30
+ end
31
+ end
32
+
33
+ it 'is invalid with a number longer than 9' do
34
+ ['1234567890', '12.3456.7890'].each do |number|
35
+ suframa = BrDocuments::Suframa.new(number)
36
+
37
+ expect(suframa).to_not be_valid
38
+ end
39
+ end
40
+
41
+ it 'is invalid with a number shorter than 9' do
42
+ ['12345678', '12.3456.78'].each do |number|
43
+ suframa = BrDocuments::Suframa.new(number)
44
+
45
+ expect(suframa).to_not be_valid
46
+ end
47
+ end
48
+
49
+ it 'is invalid with a sequence of the same number' do
50
+ ['11.1111.111', '111111111'].each do |number|
51
+ suframa = BrDocuments::Suframa.new(number)
52
+
53
+ expect(suframa).to_not be_valid
54
+ end
55
+ end
56
+
57
+ it 'is invalid with invalid verifying digit' do
58
+ ['55.0309.010', '550309010'].each do |number|
59
+ suframa = BrDocuments::Suframa.new(number)
60
+
61
+ expect(suframa).to_not be_valid
62
+ end
63
+ end
64
+
65
+ it 'is valid with correct number' do
66
+ ['11.1279.100', '10.1040.105', '100628109', '100698107'].each do |number|
67
+ suframa = BrDocuments::Suframa.new(number)
68
+
69
+ expect(suframa).to be_valid
70
+ end
71
+ end
72
+
73
+ context 'activity sector' do
74
+ it 'is valid when activity_sector is in 01, 02, 10, 11, 20, 60' do
75
+ ['11.1279.100', '10.1040.105'].each do |number|
76
+ suframa = BrDocuments::Suframa.new(number)
77
+
78
+ expect(suframa).to be_valid
79
+ end
80
+ end
81
+
82
+ it 'is invalid when activity_sector is not in 01, 02, 10, 11, 20, 60' do
83
+ ['17.1279.107', '141161108'].each do |number|
84
+ suframa = BrDocuments::Suframa.new(number)
85
+
86
+ expect(suframa).to_not be_valid
87
+ end
88
+ end
89
+ end
90
+
91
+ context 'administration unit' do
92
+ it 'is valid when adm_unit is in 01, 10, 30' do
93
+ ['11.1279.100', '101160100'].each do |number|
94
+ suframa = BrDocuments::Suframa.new(number)
95
+
96
+ expect(suframa).to be_valid
97
+ end
98
+ end
99
+
100
+ it 'is invalid when adm_unit is not in 01, 10, 30' do
101
+ ['11.1279.124', '101160160'].each do |number|
102
+ suframa = BrDocuments::Suframa.new(number)
103
+
104
+ expect(suframa).to_not be_valid
105
+ end
106
+ end
107
+ end
108
+ end
109
+ end