br_documents 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/config.yml +11 -25
  3. data/.rubocop.yml +29 -21
  4. data/.tool-versions +1 -0
  5. data/Gemfile.lock +6 -3
  6. data/Rakefile +1 -1
  7. data/br_documents.gemspec +3 -0
  8. data/lib/br_documents/cnpj_cpf/document.rb +1 -1
  9. data/lib/br_documents/{gtin/gtin.rb → gtin.rb} +2 -2
  10. data/lib/br_documents/ie/ba.rb +1 -1
  11. data/lib/br_documents/ie_validator.rb +1 -1
  12. data/lib/br_documents/suframa.rb +46 -0
  13. data/lib/br_documents/version.rb +1 -1
  14. data/spec/{cnpj_cpf → br_documents/cnpj_cpf}/cnpj_spec.rb +13 -13
  15. data/spec/{cnpj_cpf → br_documents/cnpj_cpf}/cpf_spec.rb +13 -13
  16. data/spec/br_documents/cnpj_validator_spec.rb +36 -0
  17. data/spec/br_documents/cpf_validator_spec.rb +36 -0
  18. data/spec/{gtin → br_documents}/gtin_spec.rb +2 -2
  19. data/spec/{gtin_validator_spec.rb → br_documents/gtin_validator_spec.rb} +3 -3
  20. data/spec/{ie → br_documents/ie}/ac_spec.rb +3 -3
  21. data/spec/{ie → br_documents/ie}/al_spec.rb +2 -2
  22. data/spec/{ie → br_documents/ie}/am_spec.rb +3 -3
  23. data/spec/{ie → br_documents/ie}/ap_spec.rb +2 -2
  24. data/spec/{ie → br_documents/ie}/ba_spec.rb +4 -4
  25. data/spec/{ie → br_documents/ie}/ce_spec.rb +0 -0
  26. data/spec/{ie → br_documents/ie}/df_spec.rb +3 -3
  27. data/spec/{ie → br_documents/ie}/es_spec.rb +0 -0
  28. data/spec/{ie → br_documents/ie}/factory_spec.rb +28 -28
  29. data/spec/{ie → br_documents/ie}/go_spec.rb +3 -3
  30. data/spec/{ie → br_documents/ie}/ma_spec.rb +0 -0
  31. data/spec/{ie → br_documents/ie}/mg_spec.rb +3 -3
  32. data/spec/{ie → br_documents/ie}/ms_spec.rb +0 -0
  33. data/spec/{ie → br_documents/ie}/mt_spec.rb +3 -3
  34. data/spec/{ie → br_documents/ie}/pa_spec.rb +3 -3
  35. data/spec/{ie → br_documents/ie}/pb_spec.rb +0 -0
  36. data/spec/{ie → br_documents/ie}/pe_spec.rb +4 -4
  37. data/spec/{ie → br_documents/ie}/pi_spec.rb +0 -0
  38. data/spec/{ie → br_documents/ie}/pr_spec.rb +2 -2
  39. data/spec/{ie → br_documents/ie}/rj_spec.rb +3 -3
  40. data/spec/{ie → br_documents/ie}/rn_spec.rb +4 -4
  41. data/spec/{ie → br_documents/ie}/ro_spec.rb +4 -4
  42. data/spec/{ie → br_documents/ie}/rr_spec.rb +3 -3
  43. data/spec/{ie → br_documents/ie}/rs_spec.rb +3 -3
  44. data/spec/{ie → br_documents/ie}/sc_spec.rb +3 -3
  45. data/spec/{ie → br_documents/ie}/se_spec.rb +0 -0
  46. data/spec/{ie → br_documents/ie}/shared_examples_for_pattern1.rb +3 -3
  47. data/spec/{ie → br_documents/ie}/shared_examples_for_to_remove_all_masks.rb +0 -0
  48. data/spec/{ie → br_documents/ie}/sp_spec.rb +2 -2
  49. data/spec/{ie → br_documents/ie}/to_spec.rb +6 -6
  50. data/spec/{ie_validator_spec.rb → br_documents/ie_validator_spec.rb} +7 -6
  51. data/spec/br_documents/suframa_spec.rb +73 -0
  52. data/spec/{suframa_validator_spec.rb → br_documents/suframa_validator_spec.rb} +4 -4
  53. metadata +97 -82
  54. data/lib/br_documents/suframa/suframa.rb +0 -60
  55. data/spec/cnpj_validator_spec.rb +0 -36
  56. data/spec/cpf_validator_spec.rb +0 -36
  57. data/spec/suframa/suframa_spec.rb +0 -109
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 26654dc4bd73502f9cf3fadf4a33104f218a47fb81e02c71c8a23f9d78c75d91
4
- data.tar.gz: 5ec58159f531151f3b759ac907d3d16dfd269fb3a26091f559a6e23798bcce07
3
+ metadata.gz: b0a6d2e8fbb2f732b7eafcab92276a9e4504b69ac7c4073e079981dde8852c28
4
+ data.tar.gz: 0e7dc0f1c2d7512f6276ff2a4e53e7188260edb26c9e760f8ee7602e7e32bfbb
5
5
  SHA512:
6
- metadata.gz: 724248b106f9db144d555521a59728e100e881e77261d25d2399d630e10461694adc241b027f6b53577db2d3f68ab11806d6fecaf76261bf98401d0daef780a8
7
- data.tar.gz: 05b814e328dec4ef44f0ebe2f5af18a86ea35d920cb5537ca235e8ba30c2786a550577e4f86fc232d6760d9b589557a76c29f1c09f2848ad305ae1e8d184707b
6
+ metadata.gz: cbce964fa8b997275d73f14d884a8a7ce2523f6cfc70dc4571a257651b89721de258408fe852a6808d9bd22d7e49a652e1e024adeec634e78a490ea78900893b
7
+ data.tar.gz: 9831579ab419ef1272b28729a3baad5e6e86d7373fa057da6e63c16a0b0a977832f837e435dde1e4e1448b5875cc84d292c8e77250826220a5fa240867eae2c9
@@ -1,24 +1,25 @@
1
- name: Containers
1
+ name: Tests
2
2
 
3
3
  on: [push]
4
4
 
5
5
  jobs:
6
- build:
6
+ linter:
7
7
  runs-on: ubuntu-latest
8
8
 
9
9
  steps:
10
- - uses: actions/checkout@v2
11
10
 
12
- - uses: ruby/setup-ruby@v1
13
- with:
14
- ruby-version: 2.7.3
15
- bundler-cache: true
11
+ - uses: actions/checkout@v2
12
+
13
+ - uses: ruby/setup-ruby@v1
14
+ with:
15
+ ruby-version: 2.7.3
16
+ bundler-cache: true
17
+
18
+ - run: bundle exec rubocop --display-cop-names --parallel
16
19
 
17
20
  rspec:
18
21
  runs-on: ubuntu-latest
19
22
 
20
- needs: build
21
-
22
23
  steps:
23
24
  - uses: actions/checkout@v2
24
25
 
@@ -30,25 +31,10 @@ jobs:
30
31
  - run: |
31
32
  curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
32
33
  chmod +x ./cc-test-reporter
34
+
33
35
  - run: |
34
36
  ./cc-test-reporter before-build
35
37
  bundle exec rspec
36
38
  ./cc-test-reporter after-build -r $CC_TEST_REPORTER_ID --exit-code $?
37
39
  env:
38
40
  CC_TEST_REPORTER_ID: ${{secrets.CC_TEST_REPORTER_ID}}
39
-
40
- lint:
41
- runs-on: ubuntu-latest
42
-
43
- needs: build
44
-
45
- steps:
46
-
47
- - uses: actions/checkout@v2
48
-
49
- - uses: ruby/setup-ruby@v1
50
- with:
51
- ruby-version: 2.7.3
52
- bundler-cache: true
53
-
54
- - run: bundle exec rubocop --display-cop-names --parallel
data/.rubocop.yml CHANGED
@@ -1,3 +1,20 @@
1
+ # You can find more information about RuboCop pending cops policy
2
+ # https://docs.rubocop.org/rubocop/versioning.html#pending-cops
3
+ #
4
+ # These are the default options. The .rubocop.yml inherit and
5
+ # override all the default options.
6
+ # https://github.com/rubocop-hq/rubocop/blob/master/config/default.yml
7
+ require:
8
+ - rubocop-rails
9
+ - rubocop-performance
10
+ - rubocop-rspec
11
+ - rubocop-rake
12
+ AllCops:
13
+ TargetRubyVersion: 2.7.3
14
+ DisplayCopNames: true
15
+ DisplayStyleGuide: true
16
+ NewCops: enable
17
+
1
18
  # Offense count: 1
2
19
  # Configuration parameters: Include.
3
20
  # Include: **/*.gemspec
@@ -24,6 +41,18 @@ Metrics/BlockLength:
24
41
  Style/Documentation:
25
42
  Enabled: false
26
43
 
44
+ RSpec/NamedSubject:
45
+ Enabled: false # default: true
46
+
47
+ RSpec/MultipleExpectations:
48
+ Enabled: false # default: true
49
+
50
+ RSpec/NestedGroups:
51
+ Enabled: false # default: true
52
+
53
+ Style/RegexpLiteral:
54
+ Enabled: false # default: true
55
+
27
56
  Style/FrozenStringLiteralComment:
28
57
  EnforcedStyle: never # default: always
29
58
 
@@ -32,27 +61,6 @@ Style/FrozenStringLiteralComment:
32
61
  Style/RedundantRegexpEscape:
33
62
  Enabled: false
34
63
 
35
- # Offense count: 18
36
- # Cop supports --auto-correct.
37
- # Configuration parameters: EnforcedStyle, AllowInnerSlashes.
38
- # SupportedStyles: slashes, percent_r, mixed
39
- Style/RegexpLiteral:
40
- Exclude:
41
- - 'lib/br_documents/cnpj_cpf/cnpj.rb'
42
- - 'lib/br_documents/cnpj_cpf/document.rb'
43
- - 'lib/br_documents/ie/ac.rb'
44
- - 'lib/br_documents/ie/ba.rb'
45
- - 'lib/br_documents/ie/mg.rb'
46
- - 'lib/br_documents/ie/mt.rb'
47
- - 'lib/br_documents/ie/pattern1.rb'
48
- - 'lib/br_documents/ie/pattern2.rb'
49
- - 'lib/br_documents/ie/pe.rb'
50
- - 'lib/br_documents/ie/rn.rb'
51
- - 'lib/br_documents/ie/ro.rb'
52
- - 'lib/br_documents/ie/rs.rb'
53
- - 'lib/br_documents/ie/sp.rb'
54
- - 'spec/ie/shared_examples_for_to_remove_all_masks.rb'
55
-
56
64
  # Offense count: 1
57
65
  # Cop supports --auto-correct.
58
66
  # Configuration parameters: EnforcedStyle.
data/.tool-versions ADDED
@@ -0,0 +1 @@
1
+ ruby 2.7.4
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- br_documents (0.2.0)
4
+ br_documents (0.2.1)
5
5
  activemodel (>= 4.0.0)
6
6
  i18n (>= 0.6.5)
7
7
 
@@ -31,7 +31,7 @@ GEM
31
31
  ast (~> 2.4.1)
32
32
  rack (2.2.3)
33
33
  rainbow (3.0.0)
34
- rake (13.0.3)
34
+ rake (13.0.6)
35
35
  regexp_parser (2.1.1)
36
36
  rexml (3.2.5)
37
37
  rspec (3.10.0)
@@ -58,13 +58,15 @@ GEM
58
58
  unicode-display_width (>= 1.4.0, < 3.0)
59
59
  rubocop-ast (1.7.0)
60
60
  parser (>= 3.0.1.1)
61
- rubocop-performance (1.11.3)
61
+ rubocop-performance (1.11.4)
62
62
  rubocop (>= 1.7.0, < 2.0)
63
63
  rubocop-ast (>= 0.4.0)
64
64
  rubocop-rails (2.10.1)
65
65
  activesupport (>= 4.2.0)
66
66
  rack (>= 1.1)
67
67
  rubocop (>= 1.7.0, < 2.0)
68
+ rubocop-rake (0.5.1)
69
+ rubocop
68
70
  rubocop-rspec (2.3.0)
69
71
  rubocop (~> 1.0)
70
72
  rubocop-ast (>= 1.1.0)
@@ -93,6 +95,7 @@ DEPENDENCIES
93
95
  rubocop (~> 1.15.0)
94
96
  rubocop-performance (~> 1.11.3)
95
97
  rubocop-rails (~> 2.10.1)
98
+ rubocop-rake (>= 0.5.1)
96
99
  rubocop-rspec (~> 2.3.0)
97
100
  simplecov (>= 0.21.2)
98
101
 
data/Rakefile CHANGED
@@ -5,6 +5,6 @@ task default: :spec
5
5
  RSpec::Core::RakeTask.new
6
6
 
7
7
  desc "Open an irb session preloaded class's"
8
- task :console do
8
+ task console: :enviroment do
9
9
  sh 'irb -rubygems -I lib -r br_documents.rb'
10
10
  end
data/br_documents.gemspec CHANGED
@@ -2,6 +2,7 @@ lib = File.expand_path('lib', __dir__)
2
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
3
  require 'br_documents/version'
4
4
 
5
+ # rubocop:disable Metrics/BlockLength
5
6
  Gem::Specification.new do |spec|
6
7
  spec.name = 'br_documents'
7
8
  spec.version = BrDocuments::VERSION
@@ -29,6 +30,8 @@ Gem::Specification.new do |spec|
29
30
  spec.add_development_dependency 'rubocop', '~> 1.15.0'
30
31
  spec.add_development_dependency 'rubocop-performance', '~> 1.11.3'
31
32
  spec.add_development_dependency 'rubocop-rails', '~> 2.10.1'
33
+ spec.add_development_dependency 'rubocop-rake', '>= 0.5.1'
32
34
  spec.add_development_dependency 'rubocop-rspec', '~> 2.3.0'
33
35
  spec.add_development_dependency 'simplecov', '>= 0.21.2'
34
36
  end
37
+ # rubocop:enable Metrics/BlockLength
@@ -24,7 +24,7 @@ module BrDocuments
24
24
  end
25
25
 
26
26
  def sequence_of_equal_numbers?
27
- number_without_mask.split('').uniq.length == 1
27
+ number_without_mask.chars.uniq.length == 1
28
28
  end
29
29
 
30
30
  def valid_check_digit?
@@ -15,14 +15,14 @@ module BrDocuments
15
15
  end
16
16
 
17
17
  def valid_checksum?
18
- numbers = @code.chars[0, @code.length - 1].reverse
18
+ numbers = @code[0, @code.length - 1].chars.reverse
19
19
  odd = even = 0
20
20
 
21
21
  numbers.each_with_index do |number, i|
22
22
  (i + 1).even? ? (even += number.to_i) : (odd += number.to_i * 3)
23
23
  end
24
24
 
25
- @code.chars.last.to_i == ((10 - (odd + even)) % 10)
25
+ @code[-1].to_i == ((10 - (odd + even)) % 10)
26
26
  end
27
27
  end
28
28
  end
@@ -9,7 +9,7 @@ module BrDocuments
9
9
  protected
10
10
 
11
11
  def format_ie(number)
12
- if number.gsub(/-/, '').length == 8
12
+ if number.delete('-').length == 8
13
13
  number.sub(/(\d{6})(\d{2})/, '\\1-\\2')
14
14
  else
15
15
  number.sub(/(\d{7})(\d{2})/, '\\1-\\2')
@@ -13,7 +13,7 @@ class IeValidator < ActiveModel::EachValidator
13
13
  end
14
14
 
15
15
  def attribute_uf_was_configured_at_validator?(record)
16
- record.errors.add(:base, I18n.t('validator.ie.uf.no_configured')) unless options[:uf].present?
16
+ record.errors.add(:base, I18n.t('validator.ie.uf.no_configured')) if options[:uf].blank?
17
17
 
18
18
  options[:uf].present?
19
19
  end
@@ -0,0 +1,46 @@
1
+ require_relative './commons/mod11'
2
+
3
+ module BrDocuments
4
+ class Suframa
5
+ include BrDocuments::Commons::Mod11
6
+
7
+ def initialize(number)
8
+ @number = number
9
+ end
10
+
11
+ def formatted
12
+ valid? ? format_number : ''
13
+ end
14
+
15
+ def valid?
16
+ valid_pattern? &&
17
+ !sequence_of_equal_numbers? &&
18
+ valid_check_digit?
19
+ end
20
+
21
+ private
22
+
23
+ def valid_pattern?
24
+ /^(\d{2}\.\d{4}\.\d{3})$|^(\d{9})$/.match(@number).present?
25
+ end
26
+
27
+ def sequence_of_equal_numbers?
28
+ number_without_mask.chars.uniq.length == 1
29
+ end
30
+
31
+ def number_without_mask
32
+ @number_without_mask ||= @number.delete('.')
33
+ end
34
+
35
+ def valid_check_digit?
36
+ weights = [9, 8, 7, 6, 5, 4, 3, 2]
37
+
38
+ check_digit = generate_check_digit(number_without_mask, weights)
39
+ number_without_mask[8] == check_digit.to_s
40
+ end
41
+
42
+ def format_number
43
+ @number.sub(/(\d{2})(\d{4})(\d{3})/, '\\1.\\2.\\3')
44
+ end
45
+ end
46
+ end
@@ -1,3 +1,3 @@
1
1
  module BrDocuments
2
- VERSION = '0.2.0'.freeze
2
+ VERSION = '0.2.1'.freeze
3
3
  end
@@ -3,13 +3,13 @@ require 'spec_helper'
3
3
  describe BrDocuments::CnpjCpf::Cnpj do
4
4
  describe '#formatted' do
5
5
  it 'returns a formatted number' do
6
- cnpj = BrDocuments::CnpjCpf::Cnpj.new('04001155000101')
6
+ cnpj = described_class.new('04001155000101')
7
7
  expect(cnpj.formatted).to eq '04.001.155/0001-01'
8
8
  end
9
9
 
10
10
  context 'when is invalid' do
11
11
  it 'returns an empty string' do
12
- cnpj = BrDocuments::CnpjCpf::Cnpj.new('123')
12
+ cnpj = described_class.new('123')
13
13
  expect(cnpj.formatted).to eq ''
14
14
  end
15
15
  end
@@ -17,42 +17,42 @@ describe BrDocuments::CnpjCpf::Cnpj do
17
17
 
18
18
  it 'is invalid with malformed number' do
19
19
  ['242.2818/50001-54', '35.611.011/000192'].each do |number|
20
- cnpj = BrDocuments::CnpjCpf::Cnpj.new(number)
21
- expect(cnpj).to_not be_valid
20
+ cnpj = described_class.new(number)
21
+ expect(cnpj).not_to be_valid
22
22
  end
23
23
  end
24
24
 
25
25
  it 'is invalid with a number longer than 14' do
26
26
  ['24.228.185/0001-544', '936710360001881'].each do |number|
27
- cnpj = BrDocuments::CnpjCpf::Cnpj.new(number)
28
- expect(cnpj).to_not be_valid
27
+ cnpj = described_class.new(number)
28
+ expect(cnpj).not_to be_valid
29
29
  end
30
30
  end
31
31
 
32
32
  it 'is invalid with a number shorter than 14' do
33
33
  ['24.228.185/0001-5', '9367103600018'].each do |number|
34
- cnpj = BrDocuments::CnpjCpf::Cnpj.new(number)
35
- expect(cnpj).to_not be_valid
34
+ cnpj = described_class.new(number)
35
+ expect(cnpj).not_to be_valid
36
36
  end
37
37
  end
38
38
 
39
39
  it 'is invalid with a sequence of the same number' do
40
40
  ['11.111.111/1111-11', '22222222222222'].each do |number|
41
- cnpj = BrDocuments::CnpjCpf::Cnpj.new(number)
42
- expect(cnpj).to_not be_valid
41
+ cnpj = described_class.new(number)
42
+ expect(cnpj).not_to be_valid
43
43
  end
44
44
  end
45
45
 
46
46
  it 'is invalid with invalid check number' do
47
47
  ['04.001.155/0001-03', '85961757000101'].each do |number|
48
- cnpj = BrDocuments::CnpjCpf::Cnpj.new(number)
49
- expect(cnpj).to_not be_valid
48
+ cnpj = described_class.new(number)
49
+ expect(cnpj).not_to be_valid
50
50
  end
51
51
  end
52
52
 
53
53
  it 'is valid with correct number' do
54
54
  ['04.001.155/0001-01', '85961757000102'].each do |number|
55
- cnpj = BrDocuments::CnpjCpf::Cnpj.new(number)
55
+ cnpj = described_class.new(number)
56
56
  expect(cnpj).to be_valid
57
57
  end
58
58
  end
@@ -3,13 +3,13 @@ require 'spec_helper'
3
3
  describe BrDocuments::CnpjCpf::Cpf do
4
4
  describe '#formatted' do
5
5
  it 'returns a formatted number' do
6
- cpf = BrDocuments::CnpjCpf::Cpf.new('11144477735')
6
+ cpf = described_class.new('11144477735')
7
7
  expect(cpf.formatted).to eq '111.444.777-35'
8
8
  end
9
9
 
10
10
  context 'when is invalid' do
11
11
  it 'returns an empty string' do
12
- cpf = BrDocuments::CnpjCpf::Cpf.new('123')
12
+ cpf = described_class.new('123')
13
13
  expect(cpf.formatted).to eq ''
14
14
  end
15
15
  end
@@ -17,42 +17,42 @@ describe BrDocuments::CnpjCpf::Cpf do
17
17
 
18
18
  it 'is invalid with malformed number' do
19
19
  ['111.126.491.07', '482.261.15-663', '111444.777-35'].each do |number|
20
- cpf = BrDocuments::CnpjCpf::Cpf.new(number)
21
- expect(cpf).to_not be_valid
20
+ cpf = described_class.new(number)
21
+ expect(cpf).not_to be_valid
22
22
  end
23
23
  end
24
24
 
25
25
  it 'is invalid with a number longer than 11' do
26
26
  ['045.769.421-150', '308514217766'].each do |number|
27
- cpf = BrDocuments::CnpjCpf::Cpf.new(number)
28
- expect(cpf).to_not be_valid
27
+ cpf = described_class.new(number)
28
+ expect(cpf).not_to be_valid
29
29
  end
30
30
  end
31
31
 
32
32
  it 'is invalid with a number shorter than 11' do
33
33
  ['111.444.777-3', '1234567890'].each do |number|
34
- cpf = BrDocuments::CnpjCpf::Cpf.new(number)
35
- expect(cpf).to_not be_valid
34
+ cpf = described_class.new(number)
35
+ expect(cpf).not_to be_valid
36
36
  end
37
37
  end
38
38
 
39
39
  it 'is invalid with a sequence of the same number' do
40
40
  ['111.111.111-11', '22222222222'].each do |number|
41
- cpf = BrDocuments::CnpjCpf::Cpf.new(number)
42
- expect(cpf).to_not be_valid
41
+ cpf = described_class.new(number)
42
+ expect(cpf).not_to be_valid
43
43
  end
44
44
  end
45
45
 
46
46
  it 'is invalid with invalid check number' do
47
47
  ['111.111.111-22', '2222222233'].each do |number|
48
- cpf = BrDocuments::CnpjCpf::Cpf.new(number)
49
- expect(cpf).to_not be_valid
48
+ cpf = described_class.new(number)
49
+ expect(cpf).not_to be_valid
50
50
  end
51
51
  end
52
52
 
53
53
  it 'is valid with correct number' do
54
54
  ['111.444.777-35', '01233254120'].each do |number|
55
- cpf = BrDocuments::CnpjCpf::Cpf.new(number)
55
+ cpf = described_class.new(number)
56
56
  expect(cpf).to be_valid
57
57
  end
58
58
  end