digit_checksum 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +24 -7
- data/examples/brazilian_cnpj.rb +5 -0
- data/examples/brazilian_cpf.rb +6 -0
- data/examples/h4ck.rb +8 -2
- data/lib/digit_checksum/base_document.rb +18 -1
- data/lib/digit_checksum/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 175e4e3a36c8e9303127d13650327da966e13629
|
4
|
+
data.tar.gz: 474383c0dd57977a2038c667de810a3a1603d9fc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 71834735b4c85da42ff05ef937a24a039d21ce4d733ac5ddcd4ba2050446656176d5de3b3031a14296032a41db238f399e7f633e588284c33e2367aeba57ba8f
|
7
|
+
data.tar.gz: b53f86b1bdd6a7d06f1ef77950b6aa14e23cb4d5cae061de9cb2d0e4b0a6454d04a144576e4d0632d2a6d621fe30c44848532947b62dba9ded031de6baaf8972
|
data/README.md
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
# DigitCheckSum
|
2
2
|
|
3
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).
|
4
|
+
The aim of this gem is to allow **any kind** of document to be validated e generated through calculation of [**Check Digit/Digit Checksum**](https://en.wikipedia.org/wiki/Check_digit).
|
5
5
|
|
6
|
-
What this mean? This mean that you can
|
6
|
+
What this mean? This mean that you can **validate** and **generate fake numbers** of 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
7
|
|
8
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
9
|
|
@@ -29,7 +29,10 @@ Or install it yourself as:
|
|
29
29
|
|
30
30
|
## Usage
|
31
31
|
|
32
|
-
This gem by yourself don't do anything unless you create a class that inherit from `DigitChecksum::BaseDocument` class,
|
32
|
+
This gem by yourself don't do anything unless you create a class that inherit from `DigitChecksum::BaseDocument` class, but when properly inherited and configured, believe me, you gain **awesomeness**!
|
33
|
+
|
34
|
+
Don't you believe me? See for yourself an example:
|
35
|
+
|
33
36
|
|
34
37
|
```ruby
|
35
38
|
require 'digit_checksum'
|
@@ -49,12 +52,26 @@ class CNPJ < DigitChecksum::BaseDocument
|
|
49
52
|
# match format such as: 99.999.999/9999-99 | 99-999-999/9999-99 | 99999999/999999 | 99999999999999
|
50
53
|
valid_format_regexp %r{(\d{2})[-.]?(\d{3})[-.]?(\d{3})[\/]?(\d{4})[-.]?(\d{2})}
|
51
54
|
|
55
|
+
# mask utilized to prettify doc number
|
52
56
|
pretty_format_mask %(%s.%s.%s/%s-%s)
|
57
|
+
|
58
|
+
# numbers sampled to generate new document numbers
|
59
|
+
generator_numbers (0..9).to_a
|
53
60
|
end
|
54
61
|
```
|
55
62
|
|
56
63
|
The example below it's intent to validated brazilian `CNPJ` documents, equivalent to `Corporate Taxpayer Registry Number`, so this can be used to:
|
57
64
|
|
65
|
+
#### Generate fake document numbers
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
CNPJ.generate # "79.552.921/0786-55"
|
69
|
+
|
70
|
+
CNPJ.generate(false) # 85215313606778 -- without pretty formating
|
71
|
+
|
72
|
+
```
|
73
|
+
|
74
|
+
|
58
75
|
#### Calculate check digits
|
59
76
|
```ruby
|
60
77
|
# valid format
|
@@ -82,13 +99,13 @@ CNPJ.valid?(12345678000195) # true
|
|
82
99
|
|
83
100
|
```ruby
|
84
101
|
# belows returns [1, 2, 3, 4, 5, 6, 7, 8, 0, 0, 0, 1, 9, 5]
|
85
|
-
CNPJ.
|
102
|
+
CNPJ.normalize_number("123.456.78/0001-95")
|
86
103
|
|
87
|
-
CNPJ.
|
104
|
+
CNPJ.normalize_number_to_s("123.456.78/0001-95") # "12345678000195"
|
88
105
|
|
89
|
-
CNPJ.pretty_formatted("123456780001") # "123.456.78/0001-95"
|
106
|
+
CNPJ.pretty_formatted("123456780001") # "123.456.78/0001-95" -- also aliased as CNPJ.pretty(number) or CNPJ.formatted(number)
|
90
107
|
|
91
|
-
CNPJ.
|
108
|
+
CNPJ.clear_number("123.456.78/0001-95") # "12345678000195" -- also aliased as CNPJ.stripped(number)
|
92
109
|
```
|
93
110
|
|
94
111
|
See `examples/`for more detailed samples.
|
data/examples/brazilian_cnpj.rb
CHANGED
@@ -12,8 +12,13 @@ class CNPJ < DigitChecksum::BaseDocument
|
|
12
12
|
valid_format_regexp %r{(\d{2})[-.]?(\d{3})[-.]?(\d{3})[\/]?(\d{4})[-.]?(\d{2})}
|
13
13
|
|
14
14
|
pretty_format_mask %(%s.%s.%s/%s-%s)
|
15
|
+
|
16
|
+
# numbers sampled to generate new document numbers
|
17
|
+
generator_numbers (0..9).to_a
|
15
18
|
end
|
16
19
|
|
20
|
+
CNPJ.generate
|
21
|
+
CNPJ.valid?(CNPJ.generate)
|
17
22
|
|
18
23
|
CNPJ.valid?(nil) # false
|
19
24
|
CNPJ.valid?(69739073000104) # true
|
data/examples/brazilian_cpf.rb
CHANGED
@@ -12,8 +12,14 @@ class CPF < DigitChecksum::BaseDocument
|
|
12
12
|
valid_format_regexp %r{(\d{3})[-.]?(\d{3})[-.]?(\d{3})[-.]?(\d{2})}
|
13
13
|
|
14
14
|
pretty_format_mask %(%s.%s.%s-%s)
|
15
|
+
|
16
|
+
# numbers sampled to generate new document numbers
|
17
|
+
generator_numbers (0..9).to_a
|
15
18
|
end
|
16
19
|
|
20
|
+
CPF.generate
|
21
|
+
CPF.valid?(CPF.generate)
|
22
|
+
|
17
23
|
CPF.valid?(nil) # false
|
18
24
|
CPF.valid?(31777259185) # true
|
19
25
|
CPF.valid?("315.844.227-26") # true
|
data/examples/h4ck.rb
CHANGED
@@ -23,9 +23,13 @@ class H4ck < DigitChecksum::BaseDocument
|
|
23
23
|
|
24
24
|
# 0101&&1111(0x01)||[16]<07>!29
|
25
25
|
# Ex: 0101&&1111(0x01)||[16]<07>!29-110
|
26
|
-
valid_format_regexp %r{(\d{4})
|
26
|
+
valid_format_regexp %r{(\d{4})(\d{4})\(?[0x]?(\d{2})\)?\|?\|?\[?(\d{2})\]?\<?(\d{2})\>?\!?(\d{2})\-?(\d{3})}
|
27
27
|
|
28
|
-
|
28
|
+
# XXXX&&XXXX(0xZZ)||[YY]<MM>!DD-VVV;
|
29
|
+
pretty_format_mask %(%s&&%s(0x%s)||[%s]<%s>!%s-%s;)
|
30
|
+
|
31
|
+
# numbers sampled to generate new document numbers
|
32
|
+
generator_numbers (0..9).to_a
|
29
33
|
|
30
34
|
def self.favorite_language(document_number)
|
31
35
|
document_number = normalize_document_number(document_number)
|
@@ -39,6 +43,8 @@ root_doc_number = "0101&&1111(0x01)||[16]<07>!29"
|
|
39
43
|
valid_doc_number = "0101&&1111(0x01)||[16]<07>!29-840"
|
40
44
|
invalid_doc_number = "0101&&1111(0x01)||[16]<07>!29-841"
|
41
45
|
|
46
|
+
H4ck.generate
|
47
|
+
H4ck.valid?(H4ck.generate)
|
42
48
|
H4ck.normalize_document_number_to_s(root_doc_number) # "0101111101160729"
|
43
49
|
H4ck.calculate_verify_digits(root_doc_number) # [8,4,0]
|
44
50
|
H4ck.valid?(root_doc_number) # false
|
@@ -7,10 +7,20 @@ module DigitChecksum
|
|
7
7
|
:valid_format_regexp,
|
8
8
|
:clear_number_regexp,
|
9
9
|
:pretty_format_mask,
|
10
|
+
:generator_numbers
|
10
11
|
]
|
11
12
|
|
12
13
|
class << self
|
13
|
-
def
|
14
|
+
def generate(formatted = true)
|
15
|
+
doc_numbers = root_document_digits_count.times.map { get_generator_numbers.sample }
|
16
|
+
doc_numbers.concat(calculate_verify_digits(doc_numbers))
|
17
|
+
|
18
|
+
normalized_number = normalize_number_to_s(doc_numbers)
|
19
|
+
|
20
|
+
formatted ? pretty_formatted(normalized_number) : normalized_number
|
21
|
+
end
|
22
|
+
|
23
|
+
def valid?(document_number)
|
14
24
|
# remove all non digits and return an array to be matched with mask
|
15
25
|
normalized_document = normalize_document_number(document_number)
|
16
26
|
|
@@ -131,6 +141,13 @@ module DigitChecksum
|
|
131
141
|
normalize_document_number(document_number).size == root_document_digits_count + verify_digits_count
|
132
142
|
end
|
133
143
|
|
144
|
+
alias :normalize_number_to_s :normalize_document_number_to_s
|
145
|
+
alias :normalize_number :normalize_document_number
|
146
|
+
alias :clear_number :clear_document_number
|
147
|
+
alias :stripped :clear_document_number
|
148
|
+
alias :formatted :pretty_formatted
|
149
|
+
alias :pretty :pretty_formatted
|
150
|
+
|
134
151
|
CONSTANTS_MAP.each do |const_identifier|
|
135
152
|
define_method "get_#{const_identifier}" do
|
136
153
|
const_name = const_identifier.to_s.upcase
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: digit_checksum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rafael Fidelis
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-07-
|
11
|
+
date: 2016-07-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|