tooke_utils 0.1.2
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 +7 -0
- data/.gitignore +14 -0
- data/.rspec +2 -0
- data/.travis.yml +8 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +70 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/tooke_utils.rb +4 -0
- data/lib/tooke_utils/cnpj.rb +60 -0
- data/lib/tooke_utils/cpf.rb +59 -0
- data/lib/tooke_utils/general.rb +21 -0
- data/lib/tooke_utils/version.rb +3 -0
- data/tooke_utils.gemspec +29 -0
- metadata +102 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4857dbdc4922ffdd59980dd550839e01006ef484
|
4
|
+
data.tar.gz: 3b4633f35394a7312f85afeb686292a8a7f063e9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 929fb37d34a664b6b06f1033fa8bf03f82906ca2b0508479b2ef357782e1d81a4a0a1a28a79f4d110c09dc3083c74dbcb9573621ad915de5d7438e2c37f005b7
|
7
|
+
data.tar.gz: da0e9b15dd00ab9efaab90d03263bc3b5d1af95c2be084a4355cbe18aeef5c51b4a18cba02207baa54b751e4507ca5b8db8077d6fc7e04bc75573898015c1927
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Renato Kano
|
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
|
13
|
+
all 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
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
# TookeUtils
|
2
|
+
[](https://travis-ci.org/renatokano/tooke_utils) [](https://gemnasium.com/github.com/renatokano/tooke_utils) [](https://badge.fury.io/rb/tooke_utils)
|
3
|
+
|
4
|
+
Welcome to TookeUtils!
|
5
|
+
|
6
|
+
In this version, this toolkit allows the generation, validation and formating of CPF's and CNPJ's. For now, the use is exclusive to Brazil.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'tooke_utils'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install tooke_utils
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
Generate a valid CPF:
|
27
|
+
|
28
|
+
$ cpf = TookeUtils::CPF.generate # Generates a valid CPF like "267.573.018-14"
|
29
|
+
|
30
|
+
Check if 'cpf' is valid:
|
31
|
+
|
32
|
+
$ cpf = "267.573.018-14"
|
33
|
+
|
34
|
+
$ TookeUtils::CPF.is_valid? cpf # True
|
35
|
+
|
36
|
+
Apply a valid mask to cpf:
|
37
|
+
|
38
|
+
$ cpf = "26757301814"
|
39
|
+
|
40
|
+
$ TookeUtils::CPF.mask cpf # Results in "267.573.018-14"
|
41
|
+
|
42
|
+
Generate a valid CNPJ:
|
43
|
+
|
44
|
+
$ cnpj = TookeUtils::CNPJ.generate # Generates a valid CNPJ like "41.737.186/0001-43"
|
45
|
+
|
46
|
+
Check if 'cnpj' is valid:
|
47
|
+
|
48
|
+
$ cnpj = "41737186000143"
|
49
|
+
|
50
|
+
$ TookeUtils::CNPJ.is_valid? cnpj # True
|
51
|
+
|
52
|
+
Apply a valid mask to cnpj:
|
53
|
+
|
54
|
+
$ cnpj = "41737186000143"
|
55
|
+
|
56
|
+
$ TookeUtils::CNPJ.mask cnpj # Results in "41.737.186/0001-43"
|
57
|
+
|
58
|
+
## Future Implementations
|
59
|
+
|
60
|
+
- ZipCode validation (Brazil)
|
61
|
+
- Get 'Address', 'City' and 'State' using ZipCode (WebService)
|
62
|
+
- GoogleMaps Integration (API)
|
63
|
+
|
64
|
+
## Contributing
|
65
|
+
|
66
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/renatokano/tooke_utils.
|
67
|
+
|
68
|
+
## License
|
69
|
+
|
70
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "tooke_utils"
|
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(__FILE__)
|
data/bin/setup
ADDED
data/lib/tooke_utils.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
require "tooke_utils/version"
|
2
|
+
require "tooke_utils/general"
|
3
|
+
|
4
|
+
module TookeUtils
|
5
|
+
class CNPJ
|
6
|
+
def self.is_cnpj? str
|
7
|
+
TookeUtils::General.only_numbers(str).size == 14
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.generate
|
11
|
+
cnpj = (rand(0)*(10**12)).round.to_s
|
12
|
+
while cnpj.size != 12
|
13
|
+
cnpj += "0"
|
14
|
+
end
|
15
|
+
digits = (calculate_digits cnpj)
|
16
|
+
cnpj += digits[0].to_s + digits[1].to_s
|
17
|
+
cnpj = mask cnpj
|
18
|
+
return cnpj
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.mask str
|
22
|
+
if is_cnpj? str
|
23
|
+
blc = /([0-9]{2})([0-9]{3})([0-9]{3})([0-9]{4})([0-9]{2})/.match(str)
|
24
|
+
blc[1] + "." + blc[2] + "." + blc[3] + "/" + blc[4] + "-" + blc[5]
|
25
|
+
else
|
26
|
+
nil
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.calculate_digits str
|
31
|
+
sum1,sum2 = 0,0
|
32
|
+
multipliers = [6,5,4,3,2,9,8,7,6,5,4,3,2]
|
33
|
+
|
34
|
+
for i in 0..11 do
|
35
|
+
sum1 += multipliers[i+1] * str[i].to_i
|
36
|
+
sum2 += multipliers[i] * str[i].to_i
|
37
|
+
end
|
38
|
+
|
39
|
+
# Digit 1
|
40
|
+
digit_1 = sum1.remainder 11
|
41
|
+
digit_1 < 2 ? digit_1 = 0 : digit_1 = 11 - digit_1
|
42
|
+
|
43
|
+
# Digit 2
|
44
|
+
sum2 += multipliers[12] * digit_1
|
45
|
+
digit_2 = sum2.remainder 11
|
46
|
+
digit_2 < 2 ? digit_2 = 0 : digit_2 = 11 - digit_2
|
47
|
+
|
48
|
+
digits = [digit_1, digit_2]
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.is_valid? str
|
52
|
+
if is_cnpj? str
|
53
|
+
str = TookeUtils::General.only_numbers str
|
54
|
+
digits = calculate_digits str
|
55
|
+
# Verification
|
56
|
+
digits[0] == str[-2].to_i && digits[1] == str[-1].to_i
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require "tooke_utils/version"
|
2
|
+
require "tooke_utils/general"
|
3
|
+
|
4
|
+
module TookeUtils
|
5
|
+
class CPF
|
6
|
+
def self.is_cpf? str
|
7
|
+
TookeUtils::General.only_numbers(str).size == 11
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.generate
|
11
|
+
cpf = (rand(0)*(10**9)).round.to_s
|
12
|
+
while cpf.size != 9
|
13
|
+
cpf += "0"
|
14
|
+
end
|
15
|
+
digits = (calculate_digits cpf)
|
16
|
+
cpf += digits[0].to_s + digits[1].to_s
|
17
|
+
cpf = mask cpf
|
18
|
+
return cpf
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.mask str
|
22
|
+
if TookeUtils::General.only_numbers(str).size == 11
|
23
|
+
blc = /([0-9]{3})([0-9]{3})([0-9]{3})([0-9]{2})/.match(str)
|
24
|
+
blc[1] + "." + blc[2] + "." + blc[3] + "-" + blc[4]
|
25
|
+
else
|
26
|
+
nil
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.calculate_digits str
|
31
|
+
sum1,sum2 = 0,0
|
32
|
+
|
33
|
+
for i in 0..8 do
|
34
|
+
sum1 += str[i].to_i * (10-i)
|
35
|
+
sum2 += str[i].to_i * (11-i)
|
36
|
+
end
|
37
|
+
|
38
|
+
# Digit 1
|
39
|
+
digit_1 = (sum1 * 10) % 11
|
40
|
+
digit_1 == 10 ? digit_1 = 0 : digit_1
|
41
|
+
|
42
|
+
# Digit 2
|
43
|
+
sum2 += digit_1 * 2
|
44
|
+
digit_2 = (sum2 * 10) % 11
|
45
|
+
digit_2 == 10 ? digit_2 = 0 : digit_2
|
46
|
+
|
47
|
+
digits = [digit_1, digit_2]
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.is_valid? str
|
51
|
+
if is_cpf? str
|
52
|
+
str = TookeUtils::General.only_numbers str
|
53
|
+
digits = calculate_digits str
|
54
|
+
# Verification
|
55
|
+
digits[0] == str[-2].to_i && digits[1] == str[-1].to_i
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require "tooke_utils/version"
|
2
|
+
require "tooke_utils/cpf"
|
3
|
+
require "tooke_utils/cnpj"
|
4
|
+
|
5
|
+
module TookeUtils
|
6
|
+
class General
|
7
|
+
def self.to_block_of_strings str
|
8
|
+
if CPF.is_cpf? str
|
9
|
+
/([0-9]{3})\.([0-9]{3})\.([0-9]{3})\-([0-9]{2})/.match str
|
10
|
+
elsif CNPJ.is_cnpj? str
|
11
|
+
/([0-9]{2})\.([0-9]{3})\.([0-9]{3})\/([0-9]{4})\-([0-9]{2})/.match str
|
12
|
+
else
|
13
|
+
nil
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.only_numbers str
|
18
|
+
str.gsub(/[^0-9]/, '')
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/tooke_utils.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "tooke_utils/version"
|
5
|
+
|
6
|
+
name = "tooke_utils"
|
7
|
+
|
8
|
+
Gem::Specification.new do |spec|
|
9
|
+
spec.name = name
|
10
|
+
spec.version = TookeUtils::VERSION
|
11
|
+
spec.authors = ["Renato Kano"]
|
12
|
+
spec.email = ["renatokano16@gmail.com"]
|
13
|
+
|
14
|
+
spec.summary = "TookeUtils"
|
15
|
+
spec.description = "In this version, this toolkit allows the generation, validation and formating of CPF's and CNPJ's. For now, the use is exclusive to Brazil."
|
16
|
+
spec.homepage = "https://github.com/renatokano/#{name}"
|
17
|
+
spec.license = "MIT"
|
18
|
+
|
19
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
20
|
+
f.match(%r{^(test|spec|features)/})
|
21
|
+
end
|
22
|
+
spec.bindir = "exe"
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
+
spec.require_paths = ["lib"]
|
25
|
+
|
26
|
+
spec.add_development_dependency "bundler", "~> 1.15"
|
27
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
28
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tooke_utils
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Renato Kano
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-09-12 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.15'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.15'
|
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: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: In this version, this toolkit allows the generation, validation and formating
|
56
|
+
of CPF's and CNPJ's. For now, the use is exclusive to Brazil.
|
57
|
+
email:
|
58
|
+
- renatokano16@gmail.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".rspec"
|
65
|
+
- ".travis.yml"
|
66
|
+
- Gemfile
|
67
|
+
- LICENSE.txt
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- bin/console
|
71
|
+
- bin/setup
|
72
|
+
- lib/tooke_utils.rb
|
73
|
+
- lib/tooke_utils/cnpj.rb
|
74
|
+
- lib/tooke_utils/cpf.rb
|
75
|
+
- lib/tooke_utils/general.rb
|
76
|
+
- lib/tooke_utils/version.rb
|
77
|
+
- tooke_utils.gemspec
|
78
|
+
homepage: https://github.com/renatokano/tooke_utils
|
79
|
+
licenses:
|
80
|
+
- MIT
|
81
|
+
metadata: {}
|
82
|
+
post_install_message:
|
83
|
+
rdoc_options: []
|
84
|
+
require_paths:
|
85
|
+
- lib
|
86
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
requirements: []
|
97
|
+
rubyforge_project:
|
98
|
+
rubygems_version: 2.6.12
|
99
|
+
signing_key:
|
100
|
+
specification_version: 4
|
101
|
+
summary: TookeUtils
|
102
|
+
test_files: []
|