digit_checksum 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.editorconfig +20 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +113 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/digit_checksum.gemspec +27 -0
- data/examples/brazilian_cnpj.rb +24 -0
- data/examples/brazilian_cpf.rb +23 -0
- data/examples/h4ck.rb +49 -0
- data/lib/digit_checksum/base_document.rb +149 -0
- data/lib/digit_checksum/version.rb +3 -0
- data/lib/digit_checksum.rb +5 -0
- metadata +118 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 197f09d394623c37323a0a06b9436a75ae3e8d29
|
4
|
+
data.tar.gz: 517ca82d155c67af243c561445fd65eaf7e88f4d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c316d80d8711f4a79aae222905f999dde0e0615593d59f06e1a615ef0e0e82711865d417babe2d30c0930a4054c217ad1456597eed0d2d210f4c8e69e3ecf2d4
|
7
|
+
data.tar.gz: c4b81cb5c9d8f1aa23b9b84ce063782138ac9bf57ac688cca32005fe4a4aa7bd98da984052693b8190171193c421c3c6e956d6c155d630ac994350ef36f80d4f
|
data/.editorconfig
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# editorconfig.org
|
2
|
+
root = true
|
3
|
+
|
4
|
+
[*]
|
5
|
+
indent_style = tab
|
6
|
+
end_of_line = lf
|
7
|
+
charset = utf-8
|
8
|
+
trim_trailing_whitespace = true
|
9
|
+
insert_final_newline = true
|
10
|
+
|
11
|
+
[*.md]
|
12
|
+
trim_trailing_whitespace = false
|
13
|
+
|
14
|
+
[*.rb]
|
15
|
+
indent_style = space
|
16
|
+
indent_size = 2
|
17
|
+
|
18
|
+
[Gemfile]
|
19
|
+
indent_style = space
|
20
|
+
indent_size = 2
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at rafa_fidelis@yahoo.com.br. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Rafael Fidelis
|
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,113 @@
|
|
1
|
+
# DigitCheckSum
|
2
|
+
|
3
|
+
Hi there, I'm glad you're looking in this gem!
|
4
|
+
The aim of this gem is to allow **any kind** of document to be validated through calculation of [**Check Digit/Digit Checksum**](https://en.wikipedia.org/wiki/Check_digit).
|
5
|
+
|
6
|
+
What this mean? This mean that you can validated any kind of documents such: **Passport numbers**, **Federal ID number**, **Books ISBN**, or even **create your own document** number, check `examples/` for more details.
|
7
|
+
|
8
|
+
**Tip**: Check [`examples/h4ck.rb`](examples/h4ck.rb) to see `h4ck` document specification, this is a sample document who can be manipulated using this library!
|
9
|
+
|
10
|
+
---
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
Add this line to your application's Gemfile:
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
gem 'digit_checksum'
|
18
|
+
```
|
19
|
+
|
20
|
+
And then execute:
|
21
|
+
|
22
|
+
$ bundle
|
23
|
+
|
24
|
+
Or install it yourself as:
|
25
|
+
|
26
|
+
$ gem install digit_checksum
|
27
|
+
|
28
|
+
---
|
29
|
+
|
30
|
+
## Usage
|
31
|
+
|
32
|
+
This gem by yourself don't do anything unless you create a class that inherit from `DigitChecksum::BaseDocument` class, example:
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
require 'digit_checksum'
|
36
|
+
|
37
|
+
class CNPJ < DigitChecksum::BaseDocument
|
38
|
+
# masks that will be used to calculate each check digit
|
39
|
+
digits_verify_mask first: %w(5 4 3 2 9 8 7 6 5 4 3 2),
|
40
|
+
second: %w(6 5 4 3 2 9 8 7 6 5 4 3 2)
|
41
|
+
|
42
|
+
# remove any non digit from document number
|
43
|
+
# this is the default value, feel free to override
|
44
|
+
clear_number_regexp %r{[^(\d+)]}
|
45
|
+
|
46
|
+
# use modulo 11 as algorithm (can be any value)
|
47
|
+
division_factor_modulo 11
|
48
|
+
|
49
|
+
# match format such as: 99.999.999/9999-99 | 99-999-999/9999-99 | 99999999/999999 | 99999999999999
|
50
|
+
valid_format_regexp %r{(\d{2})[-.]?(\d{3})[-.]?(\d{3})[\/]?(\d{4})[-.]?(\d{2})}
|
51
|
+
|
52
|
+
pretty_format_mask %(%s.%s.%s/%s-%s)
|
53
|
+
end
|
54
|
+
```
|
55
|
+
|
56
|
+
The example below it's intent to validated brazilian `CNPJ` documents, equivalent to `Corporate Taxpayer Registry Number`, so this can be used to:
|
57
|
+
|
58
|
+
#### Calculate check digits
|
59
|
+
```ruby
|
60
|
+
# valid format
|
61
|
+
CNPJ.calculate_verify_digits("123.456.78/0001") # [9,5]
|
62
|
+
|
63
|
+
# invalid format
|
64
|
+
CNPJ.calculate_verify_digits("123.456.78/00001") # []
|
65
|
+
```
|
66
|
+
|
67
|
+
#### Validate documents numbers
|
68
|
+
```ruby
|
69
|
+
# convenience methods to check if document is valid
|
70
|
+
|
71
|
+
# invalid format
|
72
|
+
CNPJ.valid?("123.456.78/0001") # false
|
73
|
+
CNPJ.invalid?("123.456.78/0001") # true
|
74
|
+
|
75
|
+
# valid format
|
76
|
+
CNPJ.valid?("123.456.78/0001-95") # true
|
77
|
+
CNPJ.valid?(12345678000195) # true
|
78
|
+
|
79
|
+
```
|
80
|
+
|
81
|
+
#### Normalize and format documents number
|
82
|
+
|
83
|
+
```ruby
|
84
|
+
# belows returns [1, 2, 3, 4, 5, 6, 7, 8, 0, 0, 0, 1, 9, 5]
|
85
|
+
CNPJ.normalize_document_number("123.456.78/0001-95")
|
86
|
+
|
87
|
+
CNPJ.normalize_document_number_to_s("123.456.78/0001-95") # "12345678000195"
|
88
|
+
|
89
|
+
CNPJ.pretty_formatted("123456780001") # "123.456.78/0001-95"
|
90
|
+
|
91
|
+
CNPJ.clear_document_number("123.456.78/0001-95") # "12345678000195"
|
92
|
+
```
|
93
|
+
|
94
|
+
See `examples/`for more detailed samples.
|
95
|
+
|
96
|
+
|
97
|
+
## Development
|
98
|
+
|
99
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
100
|
+
|
101
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
102
|
+
To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
103
|
+
|
104
|
+
## Contributing
|
105
|
+
|
106
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/fidelisrafael/digit_checksum.
|
107
|
+
This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
108
|
+
|
109
|
+
|
110
|
+
## License
|
111
|
+
|
112
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
113
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "digit_checksum"
|
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,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
require 'digit_checksum/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = "digit_checksum"
|
9
|
+
spec.version = DigitChecksum::VERSION
|
10
|
+
spec.authors = ["Rafael Fidelis"]
|
11
|
+
spec.email = ["rafa_fidelis@yahoo.com.br"]
|
12
|
+
|
13
|
+
spec.summary = %q{Ruby implementation of check digits algorithm for generic documents numbers}
|
14
|
+
spec.description = %q{Ruby implementation of check digits algorithm for generic documents numbers}
|
15
|
+
spec.homepage = "https://github.com/fidelisrafael/digit_checksum"
|
16
|
+
spec.license = "MIT"
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
19
|
+
spec.bindir = "exe"
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
26
|
+
spec.add_development_dependency "pry", "~> 0.10.3"
|
27
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
class CNPJ < DigitChecksum::BaseDocument
|
2
|
+
digits_verify_mask first: %w(5 4 3 2 9 8 7 6 5 4 3 2),
|
3
|
+
second: %w(6 5 4 3 2 9 8 7 6 5 4 3 2)
|
4
|
+
|
5
|
+
# MOD 11
|
6
|
+
division_factor_modulo 11
|
7
|
+
|
8
|
+
# remove any non digit from document number
|
9
|
+
clear_number_regexp %r{[^(\d+)]}
|
10
|
+
|
11
|
+
# match format such as: 99.999.999/9999-99 | 99-999-999/9999-99 | 99999999/999999 | 99999999999999
|
12
|
+
valid_format_regexp %r{(\d{2})[-.]?(\d{3})[-.]?(\d{3})[\/]?(\d{4})[-.]?(\d{2})}
|
13
|
+
|
14
|
+
pretty_format_mask %(%s.%s.%s/%s-%s)
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
CNPJ.valid?(nil) # false
|
19
|
+
CNPJ.valid?(69739073000104) # true
|
20
|
+
CNPJ.valid?("38.485.271/0001-57") # true
|
21
|
+
|
22
|
+
CNPJ.calculate_verify_digits("423.819.53/0001") # [9,7]
|
23
|
+
CNPJ.pretty_formatted("69739073000104") # "69.739.073/0001-04"
|
24
|
+
CNPJ.clear_document_number("38.485.271/0001-57") # 38485271000157
|
@@ -0,0 +1,23 @@
|
|
1
|
+
class CPF < DigitChecksum::BaseDocument
|
2
|
+
digits_verify_mask first: %w(10 9 8 7 6 5 4 3 2),
|
3
|
+
second: %w(11 10 9 8 7 6 5 4 3 2)
|
4
|
+
|
5
|
+
# MOD 11
|
6
|
+
division_factor_modulo 11
|
7
|
+
|
8
|
+
# remove any non digit from document number
|
9
|
+
clear_number_regexp %r{[^(\d+)]}
|
10
|
+
|
11
|
+
# match format such as: 999.999.999-99 | 999-999-999-99 | 99999999999
|
12
|
+
valid_format_regexp %r{(\d{3})[-.]?(\d{3})[-.]?(\d{3})[-.]?(\d{2})}
|
13
|
+
|
14
|
+
pretty_format_mask %(%s.%s.%s-%s)
|
15
|
+
end
|
16
|
+
|
17
|
+
CPF.valid?(nil) # false
|
18
|
+
CPF.valid?(31777259185) # true
|
19
|
+
CPF.valid?("315.844.227-26") # true
|
20
|
+
|
21
|
+
CPF.calculate_verify_digits("315.844.227") # [2, 6]
|
22
|
+
CPF.pretty_formatted("31777259185") # "315.844.227-26"
|
23
|
+
CPF.clear_document_number("315.844.227-26")
|
data/examples/h4ck.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# This is one document type created to ilustrated how this library can
|
2
|
+
# be fully apropriate to manipulate custom documents types
|
3
|
+
class H4ck < DigitChecksum::BaseDocument
|
4
|
+
|
5
|
+
PROGRAMMINGS_LANGUAGES = {
|
6
|
+
'01' => 'C#',
|
7
|
+
'02' => 'Javascript',
|
8
|
+
'03' => 'Lua',
|
9
|
+
'04' => 'Python',
|
10
|
+
'05' => 'Ruby'
|
11
|
+
}
|
12
|
+
|
13
|
+
PROGRAMMINGS_LANGUAGES.default = 'Unknown'
|
14
|
+
|
15
|
+
division_factor_modulo 11
|
16
|
+
|
17
|
+
digits_verify_mask first: %w(17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2),
|
18
|
+
second: %w(18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2),
|
19
|
+
last: %w(19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2)
|
20
|
+
|
21
|
+
# remove any non digit and 0x character from document number
|
22
|
+
clear_number_regexp %r{(0x)|[^\d+]}
|
23
|
+
|
24
|
+
# 0101&&1111(0x01)||[16]<07>!29
|
25
|
+
# Ex: 0101&&1111(0x01)||[16]<07>!29-110
|
26
|
+
valid_format_regexp %r{(\d{4})\&\&(\d{4})\(0x(\d{2})\)\|\|\[(\d{2})\]\<(\d{2})\>\!(\d{2})\-(\d{3})}
|
27
|
+
|
28
|
+
pretty_format_mask %(%s&&%s(0x%s)||[%s]<%s>!%s-VVV;)
|
29
|
+
|
30
|
+
def self.favorite_language(document_number)
|
31
|
+
document_number = normalize_document_number(document_number)
|
32
|
+
languange_identifier = document_number[8..9].join
|
33
|
+
|
34
|
+
PROGRAMMINGS_LANGUAGES[languange_identifier]
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
root_doc_number = "0101&&1111(0x01)||[16]<07>!29"
|
39
|
+
valid_doc_number = "0101&&1111(0x01)||[16]<07>!29-840"
|
40
|
+
invalid_doc_number = "0101&&1111(0x01)||[16]<07>!29-841"
|
41
|
+
|
42
|
+
H4ck.normalize_document_number_to_s(root_doc_number) # "0101111101160729"
|
43
|
+
H4ck.calculate_verify_digits(root_doc_number) # [8,4,0]
|
44
|
+
H4ck.valid?(root_doc_number) # false
|
45
|
+
H4ck.valid?(valid_doc_number) # true
|
46
|
+
H4ck.valid?(invalid_doc_number) # false
|
47
|
+
|
48
|
+
H4ck.favorite_language(valid_doc_number) # C#
|
49
|
+
H4ck.favorite_language("0101&&1111(0x04)||[16]<07>!29-840") # Python
|
@@ -0,0 +1,149 @@
|
|
1
|
+
module DigitChecksum
|
2
|
+
class BaseDocument
|
3
|
+
|
4
|
+
CONSTANTS_MAP = [
|
5
|
+
:digits_verify_mask,
|
6
|
+
:division_factor_modulo,
|
7
|
+
:valid_format_regexp,
|
8
|
+
:clear_number_regexp,
|
9
|
+
:pretty_format_mask,
|
10
|
+
]
|
11
|
+
|
12
|
+
class << self
|
13
|
+
def valid?(document_number, stop = true)
|
14
|
+
# remove all non digits and return an array to be matched with mask
|
15
|
+
normalized_document = normalize_document_number(document_number)
|
16
|
+
|
17
|
+
# if document is empty
|
18
|
+
return false if normalized_document.nil? || normalized_document.empty?
|
19
|
+
|
20
|
+
# if document dont match the exact size, it's invalid
|
21
|
+
return false unless valid_length?(document_number)
|
22
|
+
|
23
|
+
# remove last two digits to be verified
|
24
|
+
last_digits = normalized_document.slice!(-verify_digits_count,verify_digits_count).map(&:to_i)
|
25
|
+
digits = calculate_verify_digits(normalized_document)
|
26
|
+
|
27
|
+
last_digits == digits
|
28
|
+
end
|
29
|
+
|
30
|
+
def invalid?(document_number)
|
31
|
+
!valid?(document_number)
|
32
|
+
end
|
33
|
+
|
34
|
+
def calculate_verify_digits(document_number)
|
35
|
+
return [] unless correct_length_based_on_first_mask?(document_number)
|
36
|
+
|
37
|
+
division_modulo = get_division_factor_modulo
|
38
|
+
digits = []
|
39
|
+
|
40
|
+
get_digits_verify_mask.each do |position, mask|
|
41
|
+
document_number = root_document_number(document_number, mask)
|
42
|
+
|
43
|
+
verify_digit = calculate_verify_digit(document_number, mask, division_modulo)
|
44
|
+
|
45
|
+
digits << verify_digit
|
46
|
+
document_number << verify_digit
|
47
|
+
end
|
48
|
+
|
49
|
+
digits
|
50
|
+
end
|
51
|
+
|
52
|
+
def root_document_number(document_number, mask = nil)
|
53
|
+
mask ||= get_digits_verify_mask.values[0]
|
54
|
+
|
55
|
+
normalize_document_number(document_number, mask.size)
|
56
|
+
end
|
57
|
+
|
58
|
+
def root_document_number_to_s(document_number, mask = nil)
|
59
|
+
root_document_number(document_number, mask).join
|
60
|
+
end
|
61
|
+
|
62
|
+
def normalize_document_number(document_number, length = nil)
|
63
|
+
document_number = clear_document_number(document_number).split(//).map(&:to_i)
|
64
|
+
|
65
|
+
length.nil? ? document_number : document_number[0, length]
|
66
|
+
end
|
67
|
+
|
68
|
+
def normalize_document_number_to_s(document_number, length = nil)
|
69
|
+
normalize_document_number(document_number, length).join
|
70
|
+
end
|
71
|
+
|
72
|
+
def clear_document_number(document_number)
|
73
|
+
document_number.to_s.gsub(get_clear_number_regexp, '')
|
74
|
+
end
|
75
|
+
|
76
|
+
def pretty_formatted(document_number)
|
77
|
+
return "" if normalize_document_number(document_number).empty?
|
78
|
+
|
79
|
+
numbers = document_number.to_s.scan(get_valid_format_regexp).flatten
|
80
|
+
|
81
|
+
# document has a value but it's not valid
|
82
|
+
return "" if numbers.empty?
|
83
|
+
|
84
|
+
get_pretty_format_mask % numbers
|
85
|
+
end
|
86
|
+
|
87
|
+
def calculate_verify_digit(document_number, mask, division_factor)
|
88
|
+
digits = calculate_digits_data(document_number, mask, division_factor)
|
89
|
+
|
90
|
+
digits[:verify_digit].to_i
|
91
|
+
end
|
92
|
+
|
93
|
+
def calculate_digits_data(document_number, mask, division_factor)
|
94
|
+
sum = calculate_digits_sum(document_number, mask)
|
95
|
+
quotient = (sum / division_factor)
|
96
|
+
rest = (sum % division_factor)
|
97
|
+
|
98
|
+
{ sum: sum, quotient: quotient, rest: rest, verify_digit: digit_verify(rest, division_factor) }
|
99
|
+
end
|
100
|
+
|
101
|
+
def calculate_digits_sum(document_number, mask)
|
102
|
+
normalized_document_number = normalize_document_number(document_number)
|
103
|
+
|
104
|
+
normalized_document_number.each_with_index.map {|n,i| n.to_i * mask[i].to_i }.reduce(:+).to_f
|
105
|
+
end
|
106
|
+
|
107
|
+
def digit_verify(quotient_rest, division_factor)
|
108
|
+
# thats the rule, if quotient_rest < 2, so its became 0
|
109
|
+
return 0 if quotient_rest < 2
|
110
|
+
|
111
|
+
(division_factor - quotient_rest).to_i
|
112
|
+
end
|
113
|
+
|
114
|
+
def first_verify_mask
|
115
|
+
get_digits_verify_mask.values[0]
|
116
|
+
end
|
117
|
+
|
118
|
+
def verify_digits_count
|
119
|
+
get_digits_verify_mask.size
|
120
|
+
end
|
121
|
+
|
122
|
+
def root_document_digits_count
|
123
|
+
first_verify_mask.size
|
124
|
+
end
|
125
|
+
|
126
|
+
def correct_length_based_on_first_mask?(document_number)
|
127
|
+
normalize_document_number(document_number).size == root_document_digits_count
|
128
|
+
end
|
129
|
+
|
130
|
+
def valid_length?(document_number)
|
131
|
+
normalize_document_number(document_number).size == root_document_digits_count + verify_digits_count
|
132
|
+
end
|
133
|
+
|
134
|
+
CONSTANTS_MAP.each do |const_identifier|
|
135
|
+
define_method "get_#{const_identifier}" do
|
136
|
+
const_name = const_identifier.to_s.upcase
|
137
|
+
|
138
|
+
self.const_get(const_identifier.upcase)
|
139
|
+
end
|
140
|
+
|
141
|
+
define_method const_identifier do |arg|
|
142
|
+
const_name = const_identifier.to_s.upcase
|
143
|
+
|
144
|
+
self.const_set(const_name, arg)
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
metadata
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: digit_checksum
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Rafael Fidelis
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-07-01 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.12'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.12'
|
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
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.10.3
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.10.3
|
69
|
+
description: Ruby implementation of check digits algorithm for generic documents numbers
|
70
|
+
email:
|
71
|
+
- rafa_fidelis@yahoo.com.br
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".editorconfig"
|
77
|
+
- ".gitignore"
|
78
|
+
- ".rspec"
|
79
|
+
- ".travis.yml"
|
80
|
+
- CODE_OF_CONDUCT.md
|
81
|
+
- Gemfile
|
82
|
+
- LICENSE.txt
|
83
|
+
- README.md
|
84
|
+
- Rakefile
|
85
|
+
- bin/console
|
86
|
+
- bin/setup
|
87
|
+
- digit_checksum.gemspec
|
88
|
+
- examples/brazilian_cnpj.rb
|
89
|
+
- examples/brazilian_cpf.rb
|
90
|
+
- examples/h4ck.rb
|
91
|
+
- lib/digit_checksum.rb
|
92
|
+
- lib/digit_checksum/base_document.rb
|
93
|
+
- lib/digit_checksum/version.rb
|
94
|
+
homepage: https://github.com/fidelisrafael/digit_checksum
|
95
|
+
licenses:
|
96
|
+
- MIT
|
97
|
+
metadata: {}
|
98
|
+
post_install_message:
|
99
|
+
rdoc_options: []
|
100
|
+
require_paths:
|
101
|
+
- lib
|
102
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
requirements: []
|
113
|
+
rubyforge_project:
|
114
|
+
rubygems_version: 2.5.1
|
115
|
+
signing_key:
|
116
|
+
specification_version: 4
|
117
|
+
summary: Ruby implementation of check digits algorithm for generic documents numbers
|
118
|
+
test_files: []
|