cpf_cnpj 0.3.0 → 0.4.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d13c9e2acfdc0d60eb9ffeb2c9008fdd6bad5d0f
4
- data.tar.gz: bf59d4b1eb0c251086bc8d241b2811b61f7cefec
3
+ metadata.gz: 060f6cfefb56f74570849d75281ab0f5d00fe500
4
+ data.tar.gz: 52d7c441db65b19ce5175aa82c168a670e708f06
5
5
  SHA512:
6
- metadata.gz: 778b36870ff6afc4ad0b4c5ba65aa148aef45ba8b8005c2e277808c5559d8e7b7ef4b8e06ff1036aef81b188ce765f5befea46901cbedf84e940a85d672755b2
7
- data.tar.gz: c5a4bd47257e4afa53b44bbf514b17bb4c8b2b0726b8cb4157895f42184db1713201ace9c75550a0d566efb4bf65d9497479f1d134c678d5ffae36197177631b
6
+ metadata.gz: 21a524afa4993d82d12b1abe9ee15f957a786acf5ba89704dc5916f35946e475d50842ce40f78e56bd979c4ec66afdd90376da8d011b28e41e5203a31e8a5530
7
+ data.tar.gz: 9c25eb4c0c480eb29b040b8595d1b1a9b9e768f21dd689f4c24adc0b4f22be4051624013398a4259c400985c1f843e7a35859681409b0b3a9205f0f8f53866e5
@@ -0,0 +1,59 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.3
3
+
4
+ Include:
5
+ - '**/*.gemspec'
6
+ - '**/*.podspec'
7
+ - '**/*.jbuilder'
8
+ - '**/*.rake'
9
+ - '**/*.opal'
10
+ - '**/config.ru'
11
+ - '**/Gemfile'
12
+ - '**/Rakefile'
13
+ - '**/Capfile'
14
+ - '**/Guardfile'
15
+ - '**/Podfile'
16
+ - '**/Thorfile'
17
+ - '**/Vagrantfile'
18
+ - '**/Berksfile'
19
+ - '**/Cheffile'
20
+ - '**/Vagabondfile'
21
+
22
+ Style/Documentation:
23
+ Enabled: false
24
+
25
+ Style/StringLiterals:
26
+ EnforcedStyle: double_quotes
27
+
28
+ Layout/SpaceInsideBlockBraces:
29
+ EnforcedStyle: space
30
+ EnforcedStyleForEmptyBraces: space
31
+ SpaceBeforeBlockParameters: false
32
+
33
+ Layout/SpaceInsideHashLiteralBraces:
34
+ EnforcedStyle: no_space
35
+ EnforcedStyleForEmptyBraces: no_space
36
+
37
+ Layout/FirstArrayElementLineBreak:
38
+ Enabled: true
39
+
40
+ Layout/FirstHashElementLineBreak:
41
+ Enabled: true
42
+
43
+ Style/SymbolArray:
44
+ Enabled: true
45
+
46
+ Style/PercentLiteralDelimiters:
47
+ PreferredDelimiters:
48
+ '%': '[]'
49
+ '%i': '[]'
50
+ '%q': '[]'
51
+ '%Q': '[]'
52
+ '%r': '[]'
53
+ '%s': '[]'
54
+ '%w': '[]'
55
+ '%W': '[]'
56
+ '%x': '[]'
57
+
58
+ Metrics/LineLength:
59
+ Max: 100
@@ -1,7 +1,8 @@
1
1
  language: ruby
2
2
  sudo: false
3
3
  cache: bundler
4
+ notifications:
5
+ email: false
4
6
  rvm:
5
- - '2.1.8'
6
- - '2.2.3'
7
- - '2.3.0'
7
+ - '2.4.2'
8
+ - '2.3.5'
data/Gemfile CHANGED
@@ -1,2 +1,4 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source "http://rubygems.org"
2
4
  gemspec
data/Rakefile CHANGED
@@ -1,11 +1,13 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "bundler/gem_tasks"
2
4
  require "rake/testtask"
3
5
 
4
6
  Rake::TestTask.new(:test) do |t|
5
- t.libs << 'test'
6
- t.test_files = FileList['test/**/*_test.rb']
7
+ t.libs << "test"
8
+ t.test_files = FileList["test/**/*_test.rb"]
7
9
  t.verbose = true
8
10
  t.warning = false
9
11
  end
10
12
 
11
- task :default => :test
13
+ task default: :test
data/bin/cnpj CHANGED
@@ -1,4 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
2
4
  $LOAD_PATH.unshift "#{__dir__}/../lib"
3
5
  require "cpf_cnpj"
4
6
  CpfCnpj::CLI.new(CNPJ, ARGV, STDIN, STDOUT, STDERR).start
data/bin/cpf CHANGED
@@ -1,4 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
2
4
  $LOAD_PATH.unshift "#{__dir__}/../lib"
3
5
  require "cpf_cnpj"
4
6
  CpfCnpj::CLI.new(CPF, ARGV, STDIN, STDOUT, STDERR).start
@@ -1,5 +1,6 @@
1
- # -*- encoding: utf-8 -*-
2
- require File.expand_path("../lib/cpf_cnpj/version", __FILE__)
1
+ # frozen_string_literal: true
2
+
3
+ require "./lib/cpf_cnpj/version"
3
4
 
4
5
  Gem::Specification.new do |gem|
5
6
  gem.authors = ["Nando Vieira"]
@@ -7,9 +8,9 @@ Gem::Specification.new do |gem|
7
8
  gem.description = "Validate, generate and format CPF/CNPJ numbers. Include command-line tools."
8
9
  gem.summary = gem.description
9
10
 
10
- gem.files = `git ls-files`.split($\)
11
- gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
12
- gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
11
+ gem.files = `git ls-files`.split($OUTPUT_RECORD_SEPARATOR)
12
+ gem.executables = gem.files.grep(%r[^bin/]).map {|f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r[^(test|spec|features)/])
13
14
  gem.name = "cpf_cnpj"
14
15
  gem.require_paths = ["lib"]
15
16
  gem.version = CPF::VERSION
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class CNPJ
2
4
  require "cpf_cnpj"
3
5
  require "cnpj/formatter"
@@ -6,7 +8,8 @@ class CNPJ
6
8
  attr_reader :number
7
9
  attr_reader :strict
8
10
 
9
- REGEX = /\A\d{2}\.\d{3}.\d{3}\/\d{4}-\d{2}\Z/
11
+ REGEX = %r[\A\d{2}\.\d{3}.\d{3}/\d{4}-\d{2}\Z]
12
+ VALIDATION_SIZE_REGEX = /^\d{14}$/
10
13
  NUMBER_SIZE = 12
11
14
 
12
15
  BLACKLIST = %w[
@@ -19,7 +22,7 @@ class CNPJ
19
22
  66666666666666
20
23
  77777777777777
21
24
  88888888888888
22
- 9999999999999
25
+ 99999999999999
23
26
  ].freeze
24
27
 
25
28
  def self.valid?(number, strict: false)
@@ -53,7 +56,7 @@ class CNPJ
53
56
  end
54
57
 
55
58
  def valid?
56
- return unless stripped.size == 14
59
+ return unless stripped =~ VALIDATION_SIZE_REGEX
57
60
  return if BLACKLIST.include?(stripped)
58
61
 
59
62
  digits = numbers[0...12]
@@ -1,6 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class CNPJ
2
4
  class Formatter
3
- STRICT_REGEX = /[\/.-]/
5
+ STRICT_REGEX = %r[[/.-]]
4
6
  LOOSE_REGEX = /[^\d]/
5
7
 
6
8
  def self.format(number)
@@ -1,10 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class CNPJ
2
4
  class VerifierDigit
3
5
  def self.generate(numbers)
4
6
  index = 2
5
7
 
6
- sum = numbers.reverse.reduce(0) do |sum, number|
7
- (sum + number * index).tap do
8
+ sum = numbers.reverse.reduce(0) do |buffer, number|
9
+ (buffer + number * index).tap do
8
10
  index = index == 9 ? 2 : index + 1
9
11
  end
10
12
  end
data/lib/cpf.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class CPF
2
4
  require "cpf_cnpj"
3
5
  require "cpf/formatter"
@@ -7,6 +9,7 @@ class CPF
7
9
  attr_reader :strict
8
10
 
9
11
  REGEX = /\A\d{3}\.\d{3}\.\d{3}-\d{2}\Z/
12
+ VALIDATION_SIZE_REGEX = /^[0-9]{11}$/
10
13
  NUMBER_SIZE = 9
11
14
 
12
15
  BLACKLIST = %w[
@@ -21,6 +24,7 @@ class CPF
21
24
  88888888888
22
25
  99999999999
23
26
  12345678909
27
+ 01234567890
24
28
  ].freeze
25
29
 
26
30
  def self.valid?(number, strict: false)
@@ -53,8 +57,8 @@ class CPF
53
57
  @formatted ||= Formatter.format(number)
54
58
  end
55
59
 
56
- def valid?(strict: false)
57
- return unless stripped.size == 11
60
+ def valid?
61
+ return unless stripped =~ VALIDATION_SIZE_REGEX
58
62
  return if BLACKLIST.include?(stripped)
59
63
 
60
64
  digits = numbers[0...9]
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class CPF
2
4
  class Formatter
3
5
  STRICT_REGEX = /[.-]/
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class CPF
2
4
  class VerifierDigit
3
5
  def self.generate(numbers)
@@ -1,6 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "English"
2
4
  require "optparse"
3
5
  require "cpf_cnpj/generator"
4
6
  require "cpf_cnpj/cli"
7
+ require "cpf_cnpj/version"
5
8
  require "cpf"
6
9
  require "cnpj"
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module CpfCnpj
2
4
  class CLI
3
5
  attr_reader :document_class, :arguments, :stdin, :stdout, :stderr
@@ -18,9 +20,12 @@ module CpfCnpj
18
20
  document_class.name
19
21
  end
20
22
 
21
- def start
22
- options = {}
23
+ def options
24
+ @options ||= {}
25
+ end
23
26
 
27
+ # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
28
+ def process_command
24
29
  opts.banner = "Usage: #{bin_name} [options] [#{document_name} number]"
25
30
  opts.separator ""
26
31
  opts.separator "Specific options:"
@@ -47,21 +52,27 @@ module CpfCnpj
47
52
  end
48
53
 
49
54
  opts.on_tail("-h", "--help", "Show help") do
50
- help
51
- exit
55
+ help(true)
52
56
  end
53
57
 
54
58
  opts.parse!(arguments)
55
59
  opts.permute!(arguments)
60
+ end
61
+ # rubocop:enable Metrics/MethodLength, Metrics/AbcSize
62
+
63
+ def input
64
+ stdin.tty? ? arguments.first : stdin.read
65
+ end
56
66
 
57
- help if options.empty?
58
- generate(options) if options[:generate]
59
- input = stdin.tty? ? arguments.first : stdin.read
67
+ def start
68
+ process_command
69
+ help
70
+ generate(options)
60
71
  document = document_class.new(input)
61
72
  validate(document)
62
- format(document) if options[:format]
63
- strip(document) if options[:strip]
64
- check(document) if options[:check]
73
+ format(document)
74
+ strip(document)
75
+ check(document)
65
76
  end
66
77
 
67
78
  def validate(document)
@@ -76,28 +87,38 @@ module CpfCnpj
76
87
  end
77
88
 
78
89
  def generate(options)
90
+ return unless options[:generate]
91
+
79
92
  document = document_class.new(document_class.generate)
80
93
 
81
- if options[:strip]
82
- stdout << document.stripped
83
- else
84
- stdout << document.formatted
85
- end
94
+ output = if options[:strip]
95
+ document.stripped
96
+ else
97
+ document.formatted
98
+ end
99
+
100
+ stdout << output
86
101
 
87
102
  exit
88
103
  end
89
104
 
90
105
  def format(document)
106
+ return unless options[:format]
107
+
91
108
  stdout << document.formatted
92
109
  exit
93
110
  end
94
111
 
95
112
  def strip(document)
113
+ return unless options[:strip]
114
+
96
115
  stdout << document.stripped
97
116
  exit
98
117
  end
99
118
 
100
- def help
119
+ def help(run = options.empty?)
120
+ return unless run
121
+
101
122
  stderr << opts.to_s
102
123
  exit 1
103
124
  end
@@ -1,9 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module CpfCnpj
2
4
  class Generator
3
5
  NUMBERS = [*0..9].freeze
4
6
 
5
7
  def self.generate(size, verifier_digit_generator)
6
- numbers = size.times.map { NUMBERS.sample }
8
+ numbers = Array.new(size) { NUMBERS.sample }
7
9
  numbers << verifier_digit_generator.generate(numbers)
8
10
  numbers << verifier_digit_generator.generate(numbers)
9
11
  numbers.join("")
@@ -1,5 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module CpfCnpj
2
- VERSION = "0.3.0"
4
+ VERSION = "0.4.1"
3
5
  end
4
6
 
5
7
  class CPF
@@ -1,3 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "timeout"
4
+
1
5
  module CaptureSyscall
2
6
  def capture_syscall
3
7
  exit_status = nil
@@ -1,5 +1,6 @@
1
- require "bundler"
2
- Bundler.setup
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/setup"
3
4
  Bundler.require
4
5
 
5
6
  require "minitest/utils"
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "test_helper"
2
4
 
3
5
  module CNPJCli
@@ -84,7 +86,7 @@ module CNPJCli
84
86
  end
85
87
 
86
88
  assert_equal 0, exit_status
87
- assert_match /\A\d{14}\Z/, stdout
89
+ assert_match(/\A\d{14}\Z/, stdout)
88
90
  end
89
91
  end
90
92
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "test_helper"
2
4
 
3
5
  class CNPJFormatterTest < Minitest::Test
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "test_helper"
2
4
 
3
5
  class CNPJGeneratorTest < Minitest::Test
@@ -1,7 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "test_helper"
2
4
 
3
5
  class CnpjTest < Minitest::Test
4
6
  test "blacklists common numbers" do
7
+ refute CNPJ.valid?("00000000000000")
5
8
  refute CNPJ.valid?("11111111111111")
6
9
  refute CNPJ.valid?("22222222222222")
7
10
  refute CNPJ.valid?("33333333333333")
@@ -41,6 +44,14 @@ class CnpjTest < Minitest::Test
41
44
  end
42
45
 
43
46
  test "generates stripped number" do
44
- assert_match /\A\d{14}\z/, CNPJ.generate
47
+ assert_match(/\A\d{14}\z/, CNPJ.generate)
48
+ end
49
+
50
+ test "rejects strings" do
51
+ refute CNPJ.valid?("aa.bb.ccc/dddd-ee")
52
+ end
53
+
54
+ test "rejects strings (strict)" do
55
+ refute CNPJ.valid?("aa.bb.ccc/dddd-ee", strict: true)
45
56
  end
46
57
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "test_helper"
2
4
 
3
5
  module CPFCli
@@ -54,7 +56,7 @@ module CPFCli
54
56
 
55
57
  %w[-v --version].each do |switch|
56
58
  test "outputs version [using #{switch}]" do
57
- exit_status, stdout, stderr = capture_syscall do
59
+ exit_status, stdout, _stderr = capture_syscall do
58
60
  system "./bin/cpf #{switch}"
59
61
  end
60
62
 
@@ -84,7 +86,7 @@ module CPFCli
84
86
  end
85
87
 
86
88
  assert_equal 0, exit_status
87
- assert_match /\A\d{11}\Z/, stdout
89
+ assert_match(/\A\d{11}\Z/, stdout)
88
90
  end
89
91
  end
90
92
 
@@ -103,8 +105,6 @@ module CPFCli
103
105
  end
104
106
 
105
107
  test "formats argument using stdin" do
106
- stdin = StringIO.new("76616598837")
107
-
108
108
  exit_status, stdout = capture_syscall do
109
109
  system "echo 76616598837 | ./bin/cpf --format"
110
110
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "test_helper"
2
4
 
3
5
  class CPFFormatterTest < Minitest::Test
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "test_helper"
2
4
 
3
5
  class CPFGeneratorTest < Minitest::Test
@@ -1,7 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "test_helper"
2
4
 
3
5
  class CpfTest < Minitest::Test
4
6
  test "blacklists common numbers" do
7
+ refute CPF.valid?("01234567890")
5
8
  refute CPF.valid?("11111111111")
6
9
  refute CPF.valid?("22222222222")
7
10
  refute CPF.valid?("33333333333")
@@ -18,6 +21,10 @@ class CpfTest < Minitest::Test
18
21
  refute CPF.valid?("")
19
22
  end
20
23
 
24
+ test "rejects strings" do
25
+ refute CPF.valid?("aaa.bbb.ccc-dd")
26
+ end
27
+
21
28
  test "rejects nil values" do
22
29
  refute CPF.valid?(nil)
23
30
  end
@@ -43,6 +50,10 @@ class CpfTest < Minitest::Test
43
50
  assert CPF.valid?("29537995593", strict: true)
44
51
  end
45
52
 
53
+ test "rejects strings (strict)" do
54
+ refute CPF.valid?("aaa.bbb.ccc-dd", strict: true)
55
+ end
56
+
46
57
  test "returns stripped number" do
47
58
  cpf = CPF.new("295.379.955-93")
48
59
  assert_equal "29537995593", cpf.stripped
@@ -54,11 +65,11 @@ class CpfTest < Minitest::Test
54
65
  end
55
66
 
56
67
  test "generates formatted number" do
57
- assert_match /\A\d{3}\.\d{3}\.\d{3}-\d{2}\z/, CPF.generate(true)
68
+ assert_match(/\A\d{3}\.\d{3}\.\d{3}-\d{2}\z/, CPF.generate(true))
58
69
  end
59
70
 
60
71
  test "generates stripped number" do
61
- assert_match /\A\d{11}\z/, CPF.generate
72
+ assert_match(/\A\d{11}\z/, CPF.generate)
62
73
  end
63
74
 
64
75
  test "invalidates memoization cache" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cpf_cnpj
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nando Vieira
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-11-01 00:00:00.000000000 Z
11
+ date: 2017-10-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry-meta
@@ -63,6 +63,7 @@ extensions: []
63
63
  extra_rdoc_files: []
64
64
  files:
65
65
  - ".gitignore"
66
+ - ".rubocop.yml"
66
67
  - ".travis.yml"
67
68
  - Gemfile
68
69
  - LICENSE
@@ -110,7 +111,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
110
111
  version: '0'
111
112
  requirements: []
112
113
  rubyforge_project:
113
- rubygems_version: 2.5.1
114
+ rubygems_version: 2.6.13
114
115
  signing_key:
115
116
  specification_version: 4
116
117
  summary: Validate, generate and format CPF/CNPJ numbers. Include command-line tools.