brval 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +92 -0
- data/lib/brval.rb +34 -0
- data/lib/brval/cep/cep_service.rb +34 -0
- data/lib/brval/cep/postmon.rb +37 -0
- data/lib/brval/cep/via_cep.rb +31 -0
- data/lib/brval/cep/widenet.rb +37 -0
- data/lib/brval/cep_call.rb +40 -0
- data/lib/brval/cnh.rb +46 -0
- data/lib/brval/cnh_call.rb +11 -0
- data/lib/brval/cnpj.rb +31 -0
- data/lib/brval/cnpj_call.rb +11 -0
- data/lib/brval/cpf.rb +33 -0
- data/lib/brval/cpf_call.rb +11 -0
- data/lib/brval/credit_card.rb +41 -0
- data/lib/brval/credit_card_call.rb +11 -0
- data/lib/brval/lawsuit.rb +16 -0
- data/lib/brval/lawsuit_call.rb +11 -0
- data/lib/brval/pis.rb +21 -0
- data/lib/brval/pis_call.rb +11 -0
- data/lib/brval/renavam.rb +22 -0
- data/lib/brval/renavam_call.rb +11 -0
- data/lib/brval/te.rb +48 -0
- data/lib/brval/te_call.rb +11 -0
- data/lib/brval/val.rb +16 -0
- data/lib/brval/version.rb +3 -0
- metadata +193 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 4618b4783cbc5229fb9b7bc50ead9ec21789fef74a51b5ad998204912509af47
|
4
|
+
data.tar.gz: 020c85c74febb5e40a12db2c2e3977e604af7093dbb8a99dc8d67da31cb8db57
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2e0b08c639776a7667150ded10de430653cf103407419cfe809c5b4c7931c378b8e2bbf099428ccf790a8cbd08b1432b0d63330cc10697b00943ab911bb5fac1
|
7
|
+
data.tar.gz: ad0ae0eb5992eb6aeedd8190c50ff516971b4bfbc7c352c4f9ddfef58b7a3d1ac2b9d8eda502c10c43a49d3a5aef80f203e536afb8cf97fd9f36153ce25d44c8
|
data/README.md
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
# Brval
|
2
|
+
This is a gem to validate many Brazilian codes. Because it's common to use many gems, libs and even copy and paste methods found around the internet to do that.
|
3
|
+
|
4
|
+
So the idea of Brval gem is: reunite all those validations methods in one single gem, making our lives even "easier" to develop Ruby apps.
|
5
|
+
|
6
|
+
[Link para ler as instruções em português](https://github.com/LucasAndrad/brval/wiki/Instru%C3%A7%C3%B5es-em-portugu%C3%AAs) (link to read the instructions in portuguese)
|
7
|
+
|
8
|
+
NPM package for JS projects coming soon!
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your Gemfile
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'brval'
|
16
|
+
```
|
17
|
+
|
18
|
+
Or run it direct on terminal:
|
19
|
+
|
20
|
+
$ gem install brval
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
To use Brval gem is quite simple, just call the module `Brval` with the validation you wish.
|
25
|
+
Examples:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
# CPF valid
|
29
|
+
Brval.cpf_valid?('79411449050') # => true
|
30
|
+
|
31
|
+
# CPF invalid
|
32
|
+
Brval.cpf_valid?('79411400000') # => false
|
33
|
+
|
34
|
+
# CNH valid
|
35
|
+
Brval.cnh_valid?('30142868570') # => true
|
36
|
+
|
37
|
+
# Título de eleitor valid
|
38
|
+
Brval.te_valid?('264632480167') # => true
|
39
|
+
|
40
|
+
# Cep valid
|
41
|
+
Brval.cep_valid?('70297-400') # => true
|
42
|
+
```
|
43
|
+
|
44
|
+
### Lista da Funções
|
45
|
+
### Functions List
|
46
|
+
Above is the list with all the functions and validations avaible on Brval gem, after that a resume explaining about CEP validation:
|
47
|
+
|
48
|
+
| Function | Return | Description |
|
49
|
+
|-------------------------|--------------|-----------|
|
50
|
+
| Brval.cep_valid? |true/false | |
|
51
|
+
| Brval.cep_info? |json | Json with cep information |
|
52
|
+
| Brval.cnh_valid? |true/false | |
|
53
|
+
| Brval.cnpj_valid? |true/false | |
|
54
|
+
| Brval.cpf_valid? |true/false | |
|
55
|
+
| Brval.credit_card_valid?|true/false | Number of credit card with 16 digits |
|
56
|
+
| Brval.lawsuit_valid? |true/false | Number of judicial process|
|
57
|
+
| Brval.pis_valid? |true/false | Same validation for: PIS/PASEP/NIT |
|
58
|
+
| Brval.renavam_valid? |true/false | |
|
59
|
+
| Brval.te_valid? |true/false | te = "Título de eleitor" |
|
60
|
+
|
61
|
+
|
62
|
+
### CEP Validation
|
63
|
+
Brazilian postal code, CEP, doesn't has any formula or calculation to check is some CEP is valid or not, the only way to know that is searching in a database with all CEP's in Brazil
|
64
|
+
|
65
|
+
Apparently Correios doesn't provides any database with all CEP's of Brazil, so some devs developer their own services to search for a CEP info, some of those services are public API's and anyone can use it.
|
66
|
+
|
67
|
+
Considering this, Brval does the CEP validation following this flow:
|
68
|
+
|
69
|
+
- `Brval.cep_valid?('00000000')`
|
70
|
+
- The gem does a first query at [Via Cep API](https://viacep.com.br/)
|
71
|
+
- If the CEP is not found, the gem does a **second** query at [WideNet API](http://apps.widenet.com.br/busca-cep)
|
72
|
+
- If again, the CEP is not found the gem does third query to search the CEP at [Postmon API](https://postmon.com.br/)
|
73
|
+
- And finally, if the CEP is not found in any of these three API's the gem Brval will say that this CEP doesn't exists.
|
74
|
+
|
75
|
+
The gem uses the same procedure to get the info of some CEP with the function: `Brval.cep_info(00000000)`
|
76
|
+
|
77
|
+
**OBS:** the gem "only" search in three API's, so if a CEP exists but any of these API's doesn't have it, the gem will return a false negative.
|
78
|
+
|
79
|
+
|
80
|
+
## Contributing
|
81
|
+
|
82
|
+
If you miss any validation, create an Issue explaining about it, if possible, leave some links with the calculation or any formula example about the validation. I will be happy trying to implement that to help you.
|
83
|
+
|
84
|
+
If you want to contribute with some code create an issue so we can discuss about, if everyone agree with your idea, create a fork, create your branch and send your pull request explaining the details about your PR.
|
85
|
+
|
86
|
+
Don't forget the tests, Brval use Rspec to test. Update the README with the new info. Also, try to follow the gem pattern to implement your contribution.
|
87
|
+
|
88
|
+
Thanks!
|
89
|
+
|
90
|
+
## License
|
91
|
+
|
92
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/lib/brval.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'httparty'
|
2
|
+
require 'brval/version'
|
3
|
+
|
4
|
+
require 'brval/val'
|
5
|
+
require 'brval/cnh_call'
|
6
|
+
require 'brval/cnpj_call'
|
7
|
+
require 'brval/cpf_call'
|
8
|
+
require 'brval/credit_card_call'
|
9
|
+
require 'brval/lawsuit_call'
|
10
|
+
require 'brval/pis_call'
|
11
|
+
require 'brval/renavam_call'
|
12
|
+
require 'brval/te_call'
|
13
|
+
|
14
|
+
require 'brval/cep/cep_service'
|
15
|
+
require 'brval/cep/postmon'
|
16
|
+
require 'brval/cep/via_cep'
|
17
|
+
require 'brval/cep/widenet'
|
18
|
+
require 'brval/cep_call'
|
19
|
+
|
20
|
+
# main module
|
21
|
+
module Brval
|
22
|
+
extend CnhCall
|
23
|
+
extend CnpjCall
|
24
|
+
extend CpfCall
|
25
|
+
extend CreditCardCall
|
26
|
+
extend LawsuitCall
|
27
|
+
extend PisCall
|
28
|
+
extend RenavamCall
|
29
|
+
extend TeCall
|
30
|
+
|
31
|
+
extend CepCall
|
32
|
+
|
33
|
+
class Error < StandardError; end
|
34
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Cep
|
2
|
+
class CepService
|
3
|
+
attr_accessor :cep, :url
|
4
|
+
|
5
|
+
def initialize cep
|
6
|
+
@cep = cep.to_s.tr('^0-9', '')
|
7
|
+
end
|
8
|
+
|
9
|
+
def check
|
10
|
+
return false if @cep.nil? || @cep.length > 8
|
11
|
+
json = load_cep_json
|
12
|
+
json_valid?(json)
|
13
|
+
end
|
14
|
+
|
15
|
+
def info
|
16
|
+
return response_error if @cep.nil? || @cep.length > 8
|
17
|
+
json = load_cep_json
|
18
|
+
json_valid?(json) ? translate_params(json) : response_error
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def load_cep_json
|
24
|
+
response = HTTParty.get(@url)
|
25
|
+
return nil if response.code != 200
|
26
|
+
JSON.parse(response.body)
|
27
|
+
end
|
28
|
+
|
29
|
+
def response_error
|
30
|
+
{ 'error' => "Nenhum cep encontrado para: #{@cep}" }
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module Cep
|
2
|
+
class Postmon < CepService
|
3
|
+
|
4
|
+
# Thank you Postmon for this API
|
5
|
+
# https://postmon.com.br/
|
6
|
+
|
7
|
+
URL_HOME = 'https://api.postmon.com.br/v1/cep/'.freeze
|
8
|
+
|
9
|
+
def initialize cep
|
10
|
+
super
|
11
|
+
@url = URL_HOME + @cep
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def json_valid? json
|
17
|
+
return false if json.nil?
|
18
|
+
json.key?('bairro') && json.key?('logradouro') && json.key?('cidade') && json.key?('estado')
|
19
|
+
end
|
20
|
+
|
21
|
+
def translate_params json
|
22
|
+
json['cep'] = json['cep'].insert(5, '-')
|
23
|
+
json['address'] = json.delete 'logradouro'
|
24
|
+
json['complement'] = ''
|
25
|
+
json['neighborhood'] = json.delete 'bairro'
|
26
|
+
json['city'] = json.delete 'cidade'
|
27
|
+
json['state'] = json.delete 'estado'
|
28
|
+
json['unit'] = ''
|
29
|
+
json['ibge'] = json['cidade_info']['codigo_ibge']
|
30
|
+
json['gia'] = ''
|
31
|
+
json.delete 'estado_info'
|
32
|
+
json.delete 'cidade_info'
|
33
|
+
json
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Cep
|
2
|
+
class ViaCep < CepService
|
3
|
+
|
4
|
+
URL_HOME = 'https://viacep.com.br/ws/'.freeze
|
5
|
+
|
6
|
+
def initialize cep
|
7
|
+
super
|
8
|
+
@url = URL_HOME + @cep + '/json'
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
def json_valid? json
|
14
|
+
return false if json.key?('erro')
|
15
|
+
json.key?('logradouro') && json.key?('bairro') && json.key?('localidade') && json.key?('uf')
|
16
|
+
end
|
17
|
+
|
18
|
+
def translate_params json
|
19
|
+
json['address'] = json.delete 'logradouro'
|
20
|
+
json['complement'] = json.delete 'complemento'
|
21
|
+
json['neighborhood'] = json.delete 'bairro'
|
22
|
+
json['city'] = json.delete 'localidade'
|
23
|
+
json['state'] = json.delete 'uf'
|
24
|
+
json['unit'] = json.delete 'unidade'
|
25
|
+
json['ibge'] = json.delete 'ibge'
|
26
|
+
json['gia'] = json.delete 'gia'
|
27
|
+
json
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module Cep
|
2
|
+
class Widenet < CepService
|
3
|
+
|
4
|
+
# Thank you WideNet for this API
|
5
|
+
# apps.widenet.com.br/busca-cep/api-de-consulta
|
6
|
+
|
7
|
+
URL_HOME = 'http://apps.widenet.com.br/busca-cep/api/cep/'.freeze
|
8
|
+
|
9
|
+
def initialize cep
|
10
|
+
super
|
11
|
+
@url = URL_HOME + @cep + '.json'
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def json_valid? json
|
17
|
+
return false if json.key?('message')
|
18
|
+
json.key?('district') && json.key?('city') && json.key?('address') && json.key?('state')
|
19
|
+
end
|
20
|
+
|
21
|
+
def translate_params json
|
22
|
+
json.delete 'status'
|
23
|
+
json['cep'] = json.delete 'code'
|
24
|
+
json['address'] = json.delete 'address'
|
25
|
+
json['complement'] = ''
|
26
|
+
json['neighborhood'] = json.delete 'district'
|
27
|
+
json['city'] = json.delete 'city'
|
28
|
+
json['state'] = json.delete 'state'
|
29
|
+
json['unit'] = ''
|
30
|
+
json['ibge'] = ''
|
31
|
+
json['gia'] = ''
|
32
|
+
json
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Brval::CepCall
|
2
|
+
|
3
|
+
def cep_valid?(cep)
|
4
|
+
cep = cep.to_s.tr('^0-9', '')
|
5
|
+
return false if cep.nil? || cep.length > 8
|
6
|
+
call_apis_check(cep)
|
7
|
+
end
|
8
|
+
|
9
|
+
def cep_info(cep)
|
10
|
+
cep = cep.to_s.tr('^0-9', '')
|
11
|
+
return false if cep.nil? || cep.length > 8
|
12
|
+
call_apis_info(cep)
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def call_apis_check(cep)
|
18
|
+
via = Cep::ViaCep.new(cep).check
|
19
|
+
widenet = Cep::Widenet.new(cep).check if via != true
|
20
|
+
postmon = Cep::Postmon.new(cep).check if via != true && widenet != true
|
21
|
+
return true if (via == true || widenet == true || postmon == true)
|
22
|
+
false
|
23
|
+
end
|
24
|
+
|
25
|
+
def call_apis_info(cep)
|
26
|
+
via = Cep::ViaCep.new(cep).info
|
27
|
+
if via.key?('error')
|
28
|
+
widenet = Cep::Widenet.new(cep).info
|
29
|
+
else
|
30
|
+
return via
|
31
|
+
end
|
32
|
+
if via.key?('error') && widenet.key?('error')
|
33
|
+
postmon = Cep::Postmon.new(cep).info
|
34
|
+
else
|
35
|
+
return widenet
|
36
|
+
end
|
37
|
+
return postmon
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
data/lib/brval/cnh.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
module Brval
|
2
|
+
# Validate CNH
|
3
|
+
class Cnh < Val
|
4
|
+
|
5
|
+
private
|
6
|
+
|
7
|
+
def validate_code
|
8
|
+
return false if @code.length < 11
|
9
|
+
return false if @code[0] * 11 == @code
|
10
|
+
|
11
|
+
cnh_without_digits = @code[0..8]
|
12
|
+
cnh_digits = @code[-2..-1]
|
13
|
+
incr_dig2 = 0
|
14
|
+
|
15
|
+
sum = 0
|
16
|
+
mult = 9
|
17
|
+
(0..8).each do |i|
|
18
|
+
sum += cnh_without_digits[i].to_i * mult
|
19
|
+
mult -= 1
|
20
|
+
end
|
21
|
+
|
22
|
+
digit1 = sum % 11
|
23
|
+
incr_dig2 = -2 if digit1 == 10
|
24
|
+
digit1 = 0 if digit1 > 9
|
25
|
+
|
26
|
+
sum = 0
|
27
|
+
mult = 1
|
28
|
+
(0..8).each do |i|
|
29
|
+
sum += (cnh_without_digits[i].to_i * mult)
|
30
|
+
mult += 1
|
31
|
+
end
|
32
|
+
|
33
|
+
digit2 = if ((sum % 11) + incr_dig2) < 0
|
34
|
+
11 + (sum % 11) + incr_dig2
|
35
|
+
else
|
36
|
+
(sum % 11) + incr_dig2
|
37
|
+
end
|
38
|
+
digit2 = 0 if digit2 > 9
|
39
|
+
|
40
|
+
dig_to_compare = digit1.to_s + digit2.to_s
|
41
|
+
|
42
|
+
return true if cnh_digits == dig_to_compare
|
43
|
+
false
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
data/lib/brval/cnpj.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
module Brval
|
2
|
+
class Cnpj < Val
|
3
|
+
|
4
|
+
private
|
5
|
+
|
6
|
+
NULLS = %w{11111111111111 22222222222222 33333333333333 44444444444444 55555555555555 66666666666666 77777777777777 88888888888888 99999999999999 00000000000000}.freeze
|
7
|
+
|
8
|
+
# function from https://gist.github.com/lucascaton/1109488
|
9
|
+
def validate_code
|
10
|
+
return false if @code.nil?
|
11
|
+
|
12
|
+
value = @code.split('')
|
13
|
+
if value.length == 14
|
14
|
+
unless NULLS.include?(value.join)
|
15
|
+
value = value.collect{|x| x.to_i}
|
16
|
+
sum = value[0]*5+value[1]*4+value[2]*3+value[3]*2+value[4]*9+value[5]*8+value[6]*7+value[7]*6+value[8]*5+value[9]*4+value[10]*3+value[11]*2
|
17
|
+
sum = sum - (11*(sum/11))
|
18
|
+
result1 = (sum==0 || sum==1) ? 0 : 11 - sum
|
19
|
+
if result1 == value[12]
|
20
|
+
sum = value[0]*6+value[1]*5+value[2]*4+value[3]*3+value[4]*2+value[5]*9+value[6]*8+value[7]*7+value[8]*6+value[9]*5+value[10]*4+value[11]*3+value[12]*2
|
21
|
+
sum = sum - (11*(sum/11))
|
22
|
+
result2 = (sum == 0 || sum == 1) ? 0 : 11 - sum
|
23
|
+
return true if result2 == value[13] # valid CNPJ
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
false # invalid CNPJ
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
data/lib/brval/cpf.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
module Brval
|
2
|
+
class Cpf < Val
|
3
|
+
|
4
|
+
private
|
5
|
+
|
6
|
+
NULLS = %w{12345678909 11111111111 22222222222 33333333333 44444444444 55555555555 66666666666 77777777777 88888888888 99999999999 00000000000 01234567890}.freeze
|
7
|
+
|
8
|
+
# function from https://gist.github.com/lucascaton/1109488
|
9
|
+
def validate_code
|
10
|
+
# remote_mask(cpf)
|
11
|
+
# Add errors after initialize CPF this class if cpf has wrong value
|
12
|
+
return false if @code.nil?
|
13
|
+
|
14
|
+
value = @code.split('')
|
15
|
+
if value.length == 11
|
16
|
+
unless NULLS.include?(value.join)
|
17
|
+
value = value.collect{|x| x.to_i}
|
18
|
+
sum = 10*value[0]+9*value[1]+8*value[2]+7*value[3]+6*value[4]+5*value[5]+4*value[6]+3*value[7]+2*value[8]
|
19
|
+
sum = sum - (11 * (sum/11))
|
20
|
+
result1 = (sum == 0 or sum == 1) ? 0 : 11 - sum
|
21
|
+
if result1 == value[9]
|
22
|
+
sum = value[0]*11+value[1]*10+value[2]*9+value[3]*8+value[4]*7+value[5]*6+value[6]*5+value[7]*4+value[8]*3+value[9]*2
|
23
|
+
sum = sum - (11 * (sum/11))
|
24
|
+
result2 = (sum == 0 or sum == 1) ? 0 : 11 - sum
|
25
|
+
return true if result2 == value[10] # valid CPF
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
return false # invalid CPF
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module Brval
|
2
|
+
class CreditCard < Val
|
3
|
+
|
4
|
+
private
|
5
|
+
|
6
|
+
# Calc from: http://datagenetics.com/blog/july42013/index.html
|
7
|
+
def validate_code
|
8
|
+
raise ArgumentError, 'Credit card number must have 16 digits' if @code.length != 16
|
9
|
+
value = @code.split('')
|
10
|
+
if value.length == 16
|
11
|
+
sum = sum_nums(value[0..14])
|
12
|
+
sum_complete = treat_sum(sum.digits.reverse)
|
13
|
+
return true if sum_complete - sum == value[15].to_i
|
14
|
+
end
|
15
|
+
false
|
16
|
+
end
|
17
|
+
|
18
|
+
def sum_nums nums
|
19
|
+
sum = 0
|
20
|
+
nums.each_with_index do |n, i|
|
21
|
+
num = n.to_i
|
22
|
+
if i%2 != 0
|
23
|
+
sum += num
|
24
|
+
else
|
25
|
+
new_num = (num*2).digits.sum
|
26
|
+
sum += new_num
|
27
|
+
end
|
28
|
+
end
|
29
|
+
sum
|
30
|
+
end
|
31
|
+
|
32
|
+
def treat_sum nums
|
33
|
+
if nums.last != 0
|
34
|
+
nums[0] = nums[0]+1
|
35
|
+
nums[nums.count-1] = 0
|
36
|
+
end
|
37
|
+
nums.join.to_i
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Brval
|
2
|
+
class Lawsuit < Val
|
3
|
+
|
4
|
+
LENGTH = 20.freeze
|
5
|
+
|
6
|
+
private
|
7
|
+
|
8
|
+
def validate_code
|
9
|
+
return false if @code.length != LENGTH
|
10
|
+
dv = @code[7..8].to_i
|
11
|
+
num_calc = (@code[0..6]+@code[9..19]).to_i
|
12
|
+
dv_check = 98 - (num_calc*100).modulo(97)
|
13
|
+
dv == dv_check ? true : false
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/lib/brval/pis.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
module Brval
|
2
|
+
class Pis < Val
|
3
|
+
|
4
|
+
private
|
5
|
+
|
6
|
+
def validate_code
|
7
|
+
return false if @code.nil?
|
8
|
+
|
9
|
+
value = @code.split('')
|
10
|
+
if value.length == 11
|
11
|
+
value = value.collect{|x| x.to_i}
|
12
|
+
sum = 3*value[0] + 2*value[1] + 9*value[2] + 8*value[3] + 7*value[4] + 6*value[5] + 5*value[6] + 4*value[7] + 3*value[8] + 2*value[9]
|
13
|
+
result = 11 - sum.modulo(11)
|
14
|
+
return true if result == value[10]
|
15
|
+
return true if value[10] == 0 && (result == 10 || result == 11)
|
16
|
+
end
|
17
|
+
return false
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Brval
|
2
|
+
class Renavam < Val
|
3
|
+
|
4
|
+
private
|
5
|
+
|
6
|
+
def validate_code
|
7
|
+
@code = "00" + @code if @code.length == 9
|
8
|
+
ren_cut = @code[0..9]
|
9
|
+
ren_reverse = ren_cut.reverse[0..7]
|
10
|
+
sum = 0
|
11
|
+
(2..9).to_a.each do |i|
|
12
|
+
sum += ren_reverse[i-2].to_i * i
|
13
|
+
end
|
14
|
+
sum += @code[0].to_i*3 + @code[1].to_i*2
|
15
|
+
result1 = 11 - (sum % 11)
|
16
|
+
digit1 = (result1 == 10 || result1 == 11) ? 0 : result1
|
17
|
+
return true if @code[10].to_i == digit1
|
18
|
+
false
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
data/lib/brval/te.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# Te = Titulo Eleitoral
|
2
|
+
module Brval
|
3
|
+
class Te < Val
|
4
|
+
|
5
|
+
private
|
6
|
+
|
7
|
+
def validate_code
|
8
|
+
return false if @code.nil?
|
9
|
+
value = @code.split('')
|
10
|
+
if value.length == 12
|
11
|
+
value = value.collect{|x| x.to_i}
|
12
|
+
digit1 = calc_digit1(value)
|
13
|
+
digit2 = calc_digit2(value, digit1)
|
14
|
+
return true if value[10] == digit1 && value[11] == digit2
|
15
|
+
end
|
16
|
+
|
17
|
+
return false
|
18
|
+
end
|
19
|
+
|
20
|
+
def calc_digit1 nums
|
21
|
+
sum = nums[0]*9 + nums[1]*8 + nums[2]*7 + nums[3]*6 + nums[4]*5 + nums[5]*4 + nums[6]*3 + nums[7]*2
|
22
|
+
check_result(nums, sum)
|
23
|
+
end
|
24
|
+
|
25
|
+
def calc_digit2 nums, digit1
|
26
|
+
sum = nums[8]*4 + nums[9]*3 + digit1*2
|
27
|
+
check_result(nums, sum)
|
28
|
+
end
|
29
|
+
|
30
|
+
def check_result nums, sum
|
31
|
+
rest = sum.modulo(11)
|
32
|
+
if rest == 0 || rest == 1
|
33
|
+
if nums[8] == 0 && (nums[9] == 1 || nums[9] == 2)
|
34
|
+
if rest == 0
|
35
|
+
result = 1
|
36
|
+
else
|
37
|
+
result = 0
|
38
|
+
end
|
39
|
+
else
|
40
|
+
result = 0
|
41
|
+
end
|
42
|
+
else
|
43
|
+
result = 11 - rest
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
data/lib/brval/val.rb
ADDED
metadata
ADDED
@@ -0,0 +1,193 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: brval
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.7.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Lucas Andrade
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-01-20 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.17'
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 1.17.3
|
23
|
+
type: :development
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - "~>"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.17'
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 1.17.3
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: rake
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '10.0'
|
40
|
+
type: :development
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - "~>"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '10.0'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rspec
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '3.8'
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: 3.8.0
|
57
|
+
type: :development
|
58
|
+
prerelease: false
|
59
|
+
version_requirements: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - "~>"
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '3.8'
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: 3.8.0
|
67
|
+
- !ruby/object:Gem::Dependency
|
68
|
+
name: simplecov
|
69
|
+
requirement: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - "~>"
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: 0.16.1
|
74
|
+
type: :development
|
75
|
+
prerelease: false
|
76
|
+
version_requirements: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - "~>"
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: 0.16.1
|
81
|
+
- !ruby/object:Gem::Dependency
|
82
|
+
name: rubycritic
|
83
|
+
requirement: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - "~>"
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '3.5'
|
88
|
+
type: :development
|
89
|
+
prerelease: false
|
90
|
+
version_requirements: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - "~>"
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '3.5'
|
95
|
+
- !ruby/object:Gem::Dependency
|
96
|
+
name: rubocop
|
97
|
+
requirement: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - "~>"
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: 0.62.0
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - "~>"
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: 0.62.0
|
109
|
+
- !ruby/object:Gem::Dependency
|
110
|
+
name: json
|
111
|
+
requirement: !ruby/object:Gem::Requirement
|
112
|
+
requirements:
|
113
|
+
- - "~>"
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '2.1'
|
116
|
+
type: :development
|
117
|
+
prerelease: false
|
118
|
+
version_requirements: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - "~>"
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '2.1'
|
123
|
+
- !ruby/object:Gem::Dependency
|
124
|
+
name: httparty
|
125
|
+
requirement: !ruby/object:Gem::Requirement
|
126
|
+
requirements:
|
127
|
+
- - "~>"
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
version: 0.16.3
|
130
|
+
type: :runtime
|
131
|
+
prerelease: false
|
132
|
+
version_requirements: !ruby/object:Gem::Requirement
|
133
|
+
requirements:
|
134
|
+
- - "~>"
|
135
|
+
- !ruby/object:Gem::Version
|
136
|
+
version: 0.16.3
|
137
|
+
description: Gem to validates Brazilian codes, like CPF, CNPJ, CNH, PIS ...
|
138
|
+
email:
|
139
|
+
- lucasandrad@yandex.com
|
140
|
+
executables: []
|
141
|
+
extensions: []
|
142
|
+
extra_rdoc_files: []
|
143
|
+
files:
|
144
|
+
- README.md
|
145
|
+
- lib/brval.rb
|
146
|
+
- lib/brval/cep/cep_service.rb
|
147
|
+
- lib/brval/cep/postmon.rb
|
148
|
+
- lib/brval/cep/via_cep.rb
|
149
|
+
- lib/brval/cep/widenet.rb
|
150
|
+
- lib/brval/cep_call.rb
|
151
|
+
- lib/brval/cnh.rb
|
152
|
+
- lib/brval/cnh_call.rb
|
153
|
+
- lib/brval/cnpj.rb
|
154
|
+
- lib/brval/cnpj_call.rb
|
155
|
+
- lib/brval/cpf.rb
|
156
|
+
- lib/brval/cpf_call.rb
|
157
|
+
- lib/brval/credit_card.rb
|
158
|
+
- lib/brval/credit_card_call.rb
|
159
|
+
- lib/brval/lawsuit.rb
|
160
|
+
- lib/brval/lawsuit_call.rb
|
161
|
+
- lib/brval/pis.rb
|
162
|
+
- lib/brval/pis_call.rb
|
163
|
+
- lib/brval/renavam.rb
|
164
|
+
- lib/brval/renavam_call.rb
|
165
|
+
- lib/brval/te.rb
|
166
|
+
- lib/brval/te_call.rb
|
167
|
+
- lib/brval/val.rb
|
168
|
+
- lib/brval/version.rb
|
169
|
+
homepage: https://github.com/LucasAndrad/brval
|
170
|
+
licenses:
|
171
|
+
- MIT
|
172
|
+
metadata: {}
|
173
|
+
post_install_message:
|
174
|
+
rdoc_options: []
|
175
|
+
require_paths:
|
176
|
+
- lib
|
177
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
178
|
+
requirements:
|
179
|
+
- - ">="
|
180
|
+
- !ruby/object:Gem::Version
|
181
|
+
version: '0'
|
182
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
183
|
+
requirements:
|
184
|
+
- - ">="
|
185
|
+
- !ruby/object:Gem::Version
|
186
|
+
version: '0'
|
187
|
+
requirements: []
|
188
|
+
rubyforge_project:
|
189
|
+
rubygems_version: 2.7.6
|
190
|
+
signing_key:
|
191
|
+
specification_version: 4
|
192
|
+
summary: Gem to validates many Brazilian codes
|
193
|
+
test_files: []
|