simple_cpf_cnpj 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c48ec6a7277ae88f6940a5f497f016ab1fb40722
4
+ data.tar.gz: 8705d45f357935c0f251ccbe6d5025c5d077761e
5
+ SHA512:
6
+ metadata.gz: 398e74ab0a04215fb60246520fd45d922122543ec5a70b6f333f584633d9c97217a42841bf0ce43d71bb77a5cf45473f033b5a026cc5fddd37a681817d66be70
7
+ data.tar.gz: b91dedda38533dad0f18d1e27125d9396e44bffeb285bec3b69b54e609817574776b47a8d7e739359c8ad461d48e95cf11861a837631e05724e3e062e84a543d
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.8.7-p374
4
+ - 1.9.2
5
+ - 1.9.3
6
+ - 2.0.0
7
+ - 2.1
8
+ - 2.2
9
+ - jruby
10
+ - ree
11
+ before_install: gem install bundler -v 1.10.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cpf_cnpj.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 David Santos
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
data/README.md ADDED
@@ -0,0 +1,119 @@
1
+ # simple_cpf_cnpj
2
+
3
+ The gem `simple_cpf_cnpj` provides the module `CpfCnpj`, with utility
4
+ methods for dealing with CPFs and CNPJs.
5
+
6
+ A CPF is a Brazilian federal ID number issued to natural persons, and a CNPJ
7
+ is its counterpart for juridic persons (companies and the like).
8
+
9
+ The module provides methods for differentiating a CPF from a CNPJ, formatting,
10
+ and validating the numbers by verifying the check digits.
11
+
12
+ ### Expected format
13
+
14
+ The methods in this module expect CPFs/CNPJs as strings, with only numbers. If
15
+ the string is formatted, it's up to you to remove the punctuation.
16
+
17
+ For example:
18
+
19
+ ```ruby
20
+ CpfCnpj.valid_cpf?(formatted_cpf.gsub(/\D/, ''))
21
+ ```
22
+
23
+ ## Compatibility
24
+
25
+ The code has been tested with MRI Ruby (1.8.7, 1.9.2, 1.9.3, 2.0.0, 2.1, and 2.2), JRuby, and Ruby Enterprise Edition 1.8.7.
26
+
27
+ ## Installation
28
+
29
+ Add this line to your application's Gemfile:
30
+
31
+ ```ruby
32
+ gem 'simple_cpf_cnpj'
33
+ ```
34
+
35
+ And then execute:
36
+
37
+ ```
38
+ $ bundle
39
+ ```
40
+
41
+ Or install it yourself as:
42
+
43
+ ```
44
+ $ gem install simple_cpf_cnpj
45
+ ```
46
+
47
+ ## Usage
48
+
49
+ The full documentation can be found at: http://docs.dsantosdev.com/simple_cpf_cnpj/
50
+
51
+ If necessary for your installation, add this to your code in the appropriate place:
52
+
53
+ ```ruby
54
+ require 'simple_cpf_cnpj'
55
+ ```
56
+
57
+ ### Validate
58
+ Use CpfCnpj.valid_cpf?, CpfCnpj.valid_cnpj?, or CpfCnpj.valid_cpf_cnpj? to
59
+ check if a number is valid.
60
+
61
+ ```ruby
62
+ CpfCnpj.valid_cpf?("12345678900") # invalid
63
+ # => false
64
+ CpfCnpj.valid_cpf?("11026822840") # valid
65
+ # => true
66
+ CpfCnpj.valid_cnpj?("12345678000100") # invalid
67
+ # => false
68
+ CpfCnpj.valid_cnpj?("63871464000193") # valid
69
+ # => true
70
+ CpfCnpj.valid_cpf_cnpj?("11026822840") # valid CPF
71
+ # => true
72
+ CpfCnpj.valid_cpf_cnpj?("63871464000193") # valid CNPJ
73
+ # => true
74
+ ```
75
+
76
+ ### Format
77
+ Use CpfCnpj.format to format a CPF or CNPJ.
78
+
79
+ ```ruby
80
+ CpfCnpj.format("12345678987") # 11 characters (CPF)
81
+ # => "123.456.789-87"
82
+ CpfCnpj.format("01234567000198") # 14 characters (CNPJ)
83
+ # => "01.234.567/0001-98"
84
+ ```
85
+
86
+ ### Identify the type
87
+ Use CpfCnpj.type_of to determine if a number is a CPF or a CNPJ. Note that
88
+ this check is only based on the length of the string. The actual characters in
89
+ the string aren't checked at all.
90
+
91
+ ```ruby
92
+ CpfCnpj.type_of("12345678987") # 11 characters
93
+ # => :cpf
94
+ CpfCnpj.type_of("123456789876") # 12 characters
95
+ # => nil
96
+ CpfCnpj.type_of("01234567000198") # 14 characters
97
+ # => :cnpj
98
+ ```
99
+
100
+ ## Development
101
+
102
+ **Note:** this is not necessary in order to use the gem, only to make changes
103
+ or run the included tests.
104
+
105
+ After checking out the repo, run `bin/setup` to install dependencies. Then,
106
+ run `rake test` to run the tests. You can also run `bin/console` for an
107
+ interactive prompt that will allow you to experiment.
108
+
109
+ ## Contributing
110
+
111
+ Bug reports and pull requests are welcome on GitHub at
112
+ https://github.com/davidsantos-br/simple_cpf_cnpj.
113
+
114
+ ## License and copyright
115
+
116
+ Copyright (c) 2015 David Santos
117
+
118
+ This software is released under the MIT license, which can be found in the
119
+ file [LICENSE](rdoc-ref:LICENSE).
data/Rakefile ADDED
@@ -0,0 +1,31 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+ require "rdoc/task"
4
+
5
+ Rake::TestTask.new(:test) do |t|
6
+ t.libs << "test"
7
+ t.libs << "lib"
8
+ t.test_files = FileList['test/**/*_test.rb']
9
+ end
10
+
11
+ RDoc::Task.new(:doc) do |rd|
12
+ rd.main = "README.md"
13
+ rd.rdoc_files.include("README.md", "LICENSE", "lib/**/*.rb")
14
+ rd.rdoc_dir = "doc/v#{CpfCnpj::VERSION}"
15
+ end
16
+
17
+ desc "Prepare the documentation for publishing"
18
+ task 'doc:prepare' => [:doc] do |t|
19
+ File.write("doc/index.html", <<-EOT)
20
+ <html>
21
+ <head>
22
+ <meta http-equiv='content-type' content='text/html; charset=utf-8' />
23
+ <meta http-equiv='refresh' content='0; ./v#{CpfCnpj::VERSION}/index.html' />
24
+ <title>Redirecting&hellip;</title>
25
+ </head>
26
+ <body>Redirecting&hellip;</body>
27
+ </html>
28
+ EOT
29
+ end
30
+
31
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "simple_cpf_cnpj"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/cpf_cnpj.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'simple_cpf_cnpj/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "simple_cpf_cnpj"
8
+ spec.version = CpfCnpj::VERSION
9
+ spec.authors = ["David Santos"]
10
+ spec.email = ["david@dsantosdev.com"]
11
+
12
+ spec.summary = %q{Provides the module CpfCnpj, with functions to detect, format, and validate CPFs and CNPJs.}
13
+ spec.description = %q{Provides the module CpfCnpj, with functions to detect, validate, and format CPFs and CNPJs (Brazilian federal ID numbers).}
14
+ spec.homepage = "https://github.com/davidsantos-br/simple_cpf_cnpj"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+ spec.required_ruby_version = '>= 1.8.7'
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.10"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "minitest"
26
+ spec.add_development_dependency "rdoc"
27
+ end
@@ -0,0 +1,3 @@
1
+ module CpfCnpj # :nodoc:
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,140 @@
1
+ require "simple_cpf_cnpj/version"
2
+
3
+ # +CpfCnpj+ is a module that provides utility methods for dealing with CPFs and CNPJs.
4
+ #
5
+ # A CPF is a Brazilian federal ID number issued to natural persons, and a CNPJ is its counterpart
6
+ # for juridic persons (companies and the like).
7
+ #
8
+ # The module provides methods for differentiating a CPF from a CNPJ, formatting, and
9
+ # validating the numbers by verifying the check digits.
10
+ #
11
+ # *Important:* The methods in this module expect CPFs/CNPJs as strings, with only numbers.
12
+ # If the string is formatted, it's up to you to remove the punctuation.
13
+ #
14
+ # One way to do that is, for example:
15
+ # CpfCnpj.valid_cpf?(formatted_cpf.gsub(/\D/, ''))
16
+ module CpfCnpj
17
+ # Checks the length of +cpf_or_cnpj+ to determine if it's a CPF or a CNPJ.
18
+ #
19
+ # CpfCnpj.type_of("12345678987") # 11 characters
20
+ # # => :cpf
21
+ # CpfCnpj.type_of("123456789876") # 12 characters
22
+ # # => nil
23
+ # CpfCnpj.type_of("01234567000198") # 14 characters
24
+ # # => :cnpj
25
+ # CpfCnpj.type_of(nil)
26
+ # # => nil
27
+ # CpfCnpj.type_of(12345678987)
28
+ # # ArgumentError: argument must be a string or nil
29
+ def self.type_of(cpf_cnpj) # :arg: cpf_or_cnpj
30
+ return nil if cpf_cnpj.nil?
31
+ unless cpf_cnpj.is_a?(String)
32
+ raise(ArgumentError, "argument must be a string or nil")
33
+ end
34
+ case cpf_cnpj.length
35
+ when 11 ; :cpf
36
+ when 14 ; :cnpj
37
+ else ; nil
38
+ end
39
+ end
40
+
41
+ # Formats +cpf_or_cnpj+ as a CPF or a CNPJ.
42
+ #
43
+ # CpfCnpj.format("12345678987") # 11 characters (CPF)
44
+ # # => "123.456.789-87"
45
+ # CpfCnpj.format("123456789876") # 12 characters
46
+ # # => "123456789876"
47
+ # CpfCnpj.format("01234567000198") # 14 characters (CNPJ)
48
+ # # => "01.234.567/0001-98"
49
+ # CpfCnpj.format(nil)
50
+ # # => nil
51
+ # CpfCnpj.format(12345678987)
52
+ # # ArgumentError: argument must be a string or nil
53
+ def self.format(cpf_cnpj) # :arg: cpf_or_cnpj
54
+ case type_of(cpf_cnpj)
55
+ when :cpf
56
+ "#{cpf_cnpj[0..2]}.#{cpf_cnpj[3..5]}.#{cpf_cnpj[6..8]}-#{cpf_cnpj[9..10]}"
57
+ when :cnpj
58
+ "#{cpf_cnpj[0..1]}.#{cpf_cnpj[2..4]}.#{cpf_cnpj[5..7]}/#{cpf_cnpj[8..11]}-#{cpf_cnpj[12..13]}"
59
+ else
60
+ cpf_cnpj
61
+ end
62
+ end
63
+
64
+ # Validates +cpf+ as a CPF by verifying that the check digits match.
65
+ #
66
+ # CpfCnpj.valid_cpf?("12345678900") # invalid
67
+ # # => false
68
+ # CpfCnpj.valid_cpf?("11026822840") # valid
69
+ # # => true
70
+ # CpfCnpj.valid_cpf?("63871464000193") # valid CNPJ
71
+ # # => false
72
+ # CpfCnpj.valid_cpf?(nil)
73
+ # # => false
74
+ # CpfCnpj.valid_cpf?(11026822840)
75
+ # # ArgumentError: argument must be a string or nil
76
+ def self.valid_cpf?(cpf)
77
+ return type_of(cpf) == :cpf && _mod11_check(cpf, 11)
78
+ end
79
+
80
+ # Validates +cnpj+ as a CNPJ by verifying that the check digits match.
81
+ #
82
+ # CpfCnpj.valid_cnpj?("12345678000100") # invalid
83
+ # # => false
84
+ # CpfCnpj.valid_cnpj?("63871464000193") # valid
85
+ # # => true
86
+ # CpfCnpj.valid_cnpj?("11026822840") # valid CPF
87
+ # # => false
88
+ # CpfCnpj.valid_cnpj?(nil)
89
+ # # => false
90
+ # CpfCnpj.valid_cnpj?(63871464000193)
91
+ # # ArgumentError: argument must be a string or nil
92
+ def self.valid_cnpj?(cnpj)
93
+ return type_of(cnpj) == :cnpj && _mod11_check(cnpj, 9)
94
+ end
95
+
96
+ # Validates +cnpj_or_cnpj+ as either a CPF or a CNPJ by verifying that the check digits match.
97
+ #
98
+ # CpfCnpj.valid_cpf_cnpj?("12345678900") # invalid CPF
99
+ # # => false
100
+ # CpfCnpj.valid_cpf_cnpj?("12345678000100") # invalid CNPJ
101
+ # # => false
102
+ # CpfCnpj.valid_cpf_cnpj?("11026822840") # valid CPF
103
+ # # => true
104
+ # CpfCnpj.valid_cpf_cnpj?("63871464000193") # valid CNPJ
105
+ # # => true
106
+ # CpfCnpj.valid_cpf_cnpj?(nil)
107
+ # # => false
108
+ # CpfCnpj.valid_cpf_cnpj?(63871464000193)
109
+ # # ArgumentError: argument must be a string or nil
110
+ def self.valid_cpf_cnpj?(cpf_cnpj) # :arg: cpf_or_cnpj
111
+ return valid_cpf?(cpf_cnpj) || valid_cnpj?(cpf_cnpj)
112
+ end
113
+
114
+ def self._mod11_check_digit(digits, mult_max) # :nodoc:
115
+ sum = 0
116
+ mult = 2
117
+ digits[0..-2].reverse.each_char do |digit|
118
+ digit = _int_value(digit)
119
+ return false unless digit.between?(0, 9)
120
+ sum += mult * digit
121
+ mult += 1
122
+ mult = 2 if mult > mult_max
123
+ end
124
+ sum = 11 - (sum % 11)
125
+ sum = 0 if sum > 9
126
+ _int_value(digits[-1]) == sum
127
+ end
128
+
129
+ def self._mod11_check(digits, mult_max) # :nodoc:
130
+ _mod11_check_digit(digits[0..-2], mult_max) && _mod11_check_digit(digits, mult_max)
131
+ end
132
+
133
+ # ruby 1.8.7 doesn't have String#ord, and in versions after 1.8.7 String#[num] returns a
134
+ # one-character string instead of the byte value of the character.
135
+ # Since the return of this is always used to perform what is essentially an unclamped to_i
136
+ # on the first character, do it right here.
137
+ def self._int_value(str) # :nodoc:
138
+ (str.respond_to?(:ord) ? str.ord : str[0]) - 48
139
+ end
140
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_cpf_cnpj
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - David Santos
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-07-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rdoc
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Provides the module CpfCnpj, with functions to detect, validate, and
70
+ format CPFs and CNPJs (Brazilian federal ID numbers).
71
+ email:
72
+ - david@dsantosdev.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - LICENSE
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - cpf_cnpj.gemspec
86
+ - lib/simple_cpf_cnpj.rb
87
+ - lib/simple_cpf_cnpj/version.rb
88
+ homepage: https://github.com/davidsantos-br/simple_cpf_cnpj
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: 1.8.7
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.4.8
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Provides the module CpfCnpj, with functions to detect, format, and validate
112
+ CPFs and CNPJs.
113
+ test_files: []