br-cnpj 0.1.6 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +6 -5
- data/ext/CNPJ/cnpj.c +43 -43
- data/ext/CNPJ/cnpj.h +4 -4
- data/ext/CNPJ/ruby_cnpj.c +17 -17
- data/lib/br/cnpj.rb +12 -8
- data/spec/br-cnpj_spec.rb +1 -1
- metadata +15 -5
data/README.rdoc
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
= br-cnpj
|
2
2
|
|
3
|
-
|
3
|
+
Ruby native extension that calculates and validades CNPJ (Cadastro Nacional de
|
4
|
+
Pessoas Jur�dicas)
|
4
5
|
|
5
|
-
==
|
6
|
+
== Install
|
6
7
|
|
7
8
|
sudo gem install br-cnpj
|
8
9
|
|
9
|
-
==
|
10
|
+
== Examples
|
10
11
|
|
11
12
|
require 'rubygems'
|
12
13
|
require 'br/cnpj'
|
@@ -20,9 +21,9 @@ Lib implementada em C (gera uma lib compilada nativamente) que calcula e valida
|
|
20
21
|
BR::CNPJ.valid? 192
|
21
22
|
=> false
|
22
23
|
|
23
|
-
|
24
|
+
radix = 0
|
24
25
|
filial = 1
|
25
|
-
BR::CNPJ.new(
|
26
|
+
BR::CNPJ.new(radix, filial).to_s
|
26
27
|
=> '00000000000191'
|
27
28
|
|
28
29
|
BR::CNPJ.new(191).valid?
|
data/ext/CNPJ/cnpj.c
CHANGED
@@ -1,72 +1,72 @@
|
|
1
1
|
#include <stdlib.h>
|
2
2
|
#include "cnpj.h"
|
3
3
|
|
4
|
-
#define
|
5
|
-
#define
|
4
|
+
#define DIGIT1 0
|
5
|
+
#define DIGIT2 1
|
6
6
|
|
7
|
-
long long
|
8
|
-
return (long long)
|
7
|
+
long long calculate_cnpj_digit(int radix, int filial){
|
8
|
+
return (long long)radix*1000000 + filial*100 + calculate_digit(radix, filial);
|
9
9
|
}
|
10
10
|
|
11
|
-
int
|
12
|
-
int
|
11
|
+
int calculate_digit(int radix, int filial){
|
12
|
+
int digit1=0, digit2=0, i;
|
13
13
|
int vet[14];
|
14
|
-
int
|
14
|
+
int weights[][13] = {{5,4,3,2,9,8,7,6,5,4,3,2,0},
|
15
15
|
{6,5,4,3,2,9,8,7,6,5,4,3,2}};
|
16
16
|
long long cnpj;
|
17
|
-
div_t
|
17
|
+
div_t division;
|
18
18
|
|
19
|
-
cnpj = (long long)
|
19
|
+
cnpj = (long long)radix*1000000 + filial*100;
|
20
20
|
for (i=0;i<14;i++){
|
21
|
-
lldiv_t
|
21
|
+
lldiv_t division;
|
22
22
|
|
23
|
-
|
24
|
-
vet[13-i] = (int)
|
25
|
-
cnpj =
|
23
|
+
division = lldiv(cnpj,10);
|
24
|
+
vet[13-i] = (int) division.rem;
|
25
|
+
cnpj = division.quot;
|
26
26
|
}
|
27
27
|
for(i=0;i<12;i++)
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
vet[12]=
|
28
|
+
digit1 += vet[i] * weights[DIGIT1][i];
|
29
|
+
division = div(digit1,11);
|
30
|
+
digit1 = division.rem<2 ? 0 : 11-division.rem;
|
31
|
+
vet[12]=digit1;
|
32
32
|
for(i=0;i<13;i++)
|
33
|
-
|
34
|
-
|
35
|
-
|
33
|
+
digit2 += vet[i] * weights[DIGIT2][i];
|
34
|
+
division = div(digit2,11);
|
35
|
+
digit2 = division.rem<2 ? 0 : 11- division.rem;
|
36
36
|
|
37
|
-
return
|
37
|
+
return digit1*10 + digit2;
|
38
38
|
}
|
39
39
|
|
40
|
-
int
|
41
|
-
int
|
42
|
-
lldiv_t
|
40
|
+
int is_cnpj(long long cnpj_l){
|
41
|
+
int radix, filial;
|
42
|
+
lldiv_t division;
|
43
43
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
filial=
|
48
|
-
return cnpj_l ==
|
44
|
+
division = lldiv(cnpj_l,1000000);
|
45
|
+
radix = division.quot;
|
46
|
+
division = lldiv(division.rem,100);
|
47
|
+
filial=division.quot;
|
48
|
+
return cnpj_l == calculate_cnpj_digit(radix, filial);
|
49
49
|
}
|
50
50
|
|
51
|
-
int
|
52
|
-
if(
|
53
|
-
int
|
54
|
-
int
|
55
|
-
lldiv_t
|
51
|
+
int is_cgc(long long cgc_l){
|
52
|
+
if(is_cnpj(cgc_l)){
|
53
|
+
int weights[]={2,1,2,1,2,1,2,1};
|
54
|
+
int sum=0, radix=0, i=0;
|
55
|
+
lldiv_t division;
|
56
56
|
|
57
|
-
|
58
|
-
|
57
|
+
division = lldiv(cgc_l, 1000000);
|
58
|
+
radix = (int)division.quot;
|
59
59
|
for (i=0;i<8;i++){
|
60
|
-
int
|
60
|
+
int product=0;
|
61
61
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
62
|
+
division = lldiv(radix,10);
|
63
|
+
product = division.rem*weights[7-i];
|
64
|
+
sum += (product>9) ? product-9 : product;
|
65
|
+
radix = division.quot;
|
66
66
|
}
|
67
67
|
|
68
|
-
|
69
|
-
return !
|
68
|
+
division=lldiv(sum, 10);
|
69
|
+
return !division.rem;
|
70
70
|
}
|
71
71
|
return 0;
|
72
72
|
}
|
data/ext/CNPJ/cnpj.h
CHANGED
@@ -5,10 +5,10 @@
|
|
5
5
|
extern "C" {
|
6
6
|
#endif
|
7
7
|
|
8
|
-
long long
|
9
|
-
int
|
10
|
-
int
|
11
|
-
int
|
8
|
+
long long calculate_cnpj_digit(int radix, int filial);
|
9
|
+
int calculate_digit(int radix, int filial);
|
10
|
+
int is_cnpj(long long cnpj_l);
|
11
|
+
int is_cgc(long long cgc_l);
|
12
12
|
|
13
13
|
#ifdef __cplusplus
|
14
14
|
}
|
data/ext/CNPJ/ruby_cnpj.c
CHANGED
@@ -3,19 +3,19 @@
|
|
3
3
|
|
4
4
|
static VALUE t_init_cnpj(int argc, VALUE *argv, VALUE self)
|
5
5
|
{
|
6
|
-
int
|
6
|
+
int radix = 0;
|
7
7
|
int filial = 0;
|
8
8
|
int verif = 0;
|
9
9
|
VALUE valid = Qnil;
|
10
10
|
|
11
11
|
if (argc == 2) {
|
12
|
-
|
13
|
-
if(
|
14
|
-
rb_raise(rb_eArgError, "
|
12
|
+
radix = FIX2INT(argv[0]);
|
13
|
+
if( radix >= 100000000 || radix < 0 )
|
14
|
+
rb_raise(rb_eArgError, "radix should be greater than -1 or lesser than 10_000_000");
|
15
15
|
filial = NUM2INT(argv[1]);
|
16
16
|
if( filial >= 10000 || filial < 1)
|
17
17
|
rb_raise(rb_eArgError, "filial should be greater than 0 or lesser than 10_000");
|
18
|
-
verif =
|
18
|
+
verif = calculate_digit(radix,filial);
|
19
19
|
valid = Qtrue;
|
20
20
|
}
|
21
21
|
else if (argc == 1) {
|
@@ -33,32 +33,32 @@ static VALUE t_init_cnpj(int argc, VALUE *argv, VALUE self)
|
|
33
33
|
verif = (int)v.rem;
|
34
34
|
v = lldiv(v.quot, (long long)10000);
|
35
35
|
filial = (int)v.rem;
|
36
|
-
|
37
|
-
valid = verif ==
|
36
|
+
radix = (int)v.quot;
|
37
|
+
valid = (verif == calculate_digit(radix,filial)) ? Qtrue : Qfalse;
|
38
38
|
}
|
39
39
|
|
40
|
-
rb_iv_set(self, "@
|
40
|
+
rb_iv_set(self, "@radix", INT2FIX(radix));
|
41
41
|
rb_iv_set(self, "@filial", INT2FIX(filial));
|
42
|
-
rb_iv_set(self, "@
|
42
|
+
rb_iv_set(self, "@digit", INT2FIX(verif));
|
43
43
|
rb_iv_set(self, "@valid", valid);
|
44
44
|
|
45
45
|
return self;
|
46
46
|
}
|
47
47
|
|
48
|
-
static VALUE
|
49
|
-
long long
|
50
|
-
int filial = 0,
|
51
|
-
|
48
|
+
static VALUE rb_is_cgc(VALUE self){
|
49
|
+
long long radix=0;
|
50
|
+
int filial = 0, verify_digit = 0;
|
51
|
+
radix = NUM2INT(rb_iv_get(self, "@radix"));
|
52
52
|
filial = NUM2INT(rb_iv_get(self, "@filial"));
|
53
|
-
|
54
|
-
return
|
53
|
+
verify_digit = NUM2INT(rb_iv_get(self, "@digit"));
|
54
|
+
return is_cgc((long long)radix * 1000000 + filial * 100 + verify_digit) ? Qtrue : Qfalse;
|
55
55
|
}
|
56
56
|
|
57
57
|
static VALUE valid_cnpj(VALUE self, VALUE ruby_cnpj){
|
58
58
|
long long cnpj=0;
|
59
59
|
cnpj = (long long)NUM2LL(ruby_cnpj);
|
60
60
|
if(cnpj == 0) return Qfalse;
|
61
|
-
return
|
61
|
+
return is_cnpj(cnpj) ? Qtrue : Qfalse;
|
62
62
|
}
|
63
63
|
|
64
64
|
VALUE mBR, cCNPJ;
|
@@ -67,5 +67,5 @@ void Init_CNPJ(){
|
|
67
67
|
cCNPJ = rb_define_class_under(mBR, "CNPJ", rb_cObject);
|
68
68
|
rb_define_method(cCNPJ, "initialize", t_init_cnpj, -1);
|
69
69
|
rb_define_singleton_method(cCNPJ, "valid?", valid_cnpj, 1);
|
70
|
-
rb_define_method(cCNPJ, "is_cgc?",
|
70
|
+
rb_define_method(cCNPJ, "is_cgc?", rb_is_cgc, 0);
|
71
71
|
}
|
data/lib/br/cnpj.rb
CHANGED
@@ -6,18 +6,20 @@ module BR
|
|
6
6
|
|
7
7
|
alias_method :orig_valid?, :valid?
|
8
8
|
|
9
|
-
#
|
9
|
+
# Format unformated Cnpj
|
10
10
|
#
|
11
|
-
# Cnpj.
|
11
|
+
# Cnpj.format(191) # => '00.000.000/0001-91'
|
12
12
|
# Cnpj.formatar(30553786000135) # => "30.553.786/0001-35"
|
13
13
|
#
|
14
14
|
def format(cnpj)
|
15
15
|
cnpj = cnpj.to_s.rjust(14, "0")
|
16
16
|
"%s.%s.%s/%s-%s" % [cnpj[0,2], cnpj[2,3], cnpj[5,3], cnpj[8,4], cnpj[12,2]]
|
17
17
|
end
|
18
|
+
|
19
|
+
### pt-BR method name
|
18
20
|
alias_method :formatar, :format
|
19
21
|
|
20
|
-
#
|
22
|
+
# Unformat formated Cnpj
|
21
23
|
#
|
22
24
|
# Cnpj.desformatar("02.716.485/0001-40") # => "02716485000140"
|
23
25
|
# Cnpj.unformat("00.086.001/0001-04") # => "00086001000104"
|
@@ -25,10 +27,12 @@ module BR
|
|
25
27
|
def unformat(cnpj)
|
26
28
|
cnpj.gsub(/\D/, '')
|
27
29
|
end
|
30
|
+
|
31
|
+
### pt-BR method name
|
28
32
|
alias_method :desformatar, :unformat
|
29
33
|
|
30
|
-
#
|
31
|
-
#
|
34
|
+
# Returns true if Cnpj is valid
|
35
|
+
# Retorns false if Cnpj is invalid
|
32
36
|
#
|
33
37
|
# CNPJ.valid?(191) # => true
|
34
38
|
# CNPJ.valid?(123456789101) # => false
|
@@ -42,18 +46,18 @@ module BR
|
|
42
46
|
end
|
43
47
|
|
44
48
|
end
|
45
|
-
attr_reader :filial, :valid, :
|
49
|
+
attr_reader :filial, :valid, :radix, :digit
|
46
50
|
|
47
51
|
# Return if the instance CNPJ is valid?
|
48
52
|
#
|
49
53
|
# Cnpj.new(191).valid? # => true
|
50
54
|
#
|
51
55
|
def valid?
|
52
|
-
valid
|
56
|
+
@valid
|
53
57
|
end
|
54
58
|
|
55
59
|
def to_i
|
56
|
-
@
|
60
|
+
@radix * 1_000_000 + @filial * 100 + @digit
|
57
61
|
end
|
58
62
|
|
59
63
|
# Return the CNPJ with 14 characters
|
data/spec/br-cnpj_spec.rb
CHANGED
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: br-cnpj
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
+
hash: 23
|
4
5
|
prerelease: false
|
5
6
|
segments:
|
6
|
-
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
|
8
|
+
- 0
|
9
|
+
- 0
|
10
|
+
version: 1.0.0
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- Bruno Coimbra
|
@@ -14,16 +15,18 @@ autorequire:
|
|
14
15
|
bindir: bin
|
15
16
|
cert_chain: []
|
16
17
|
|
17
|
-
date: 2010-
|
18
|
+
date: 2010-06-29 00:00:00 -03:00
|
18
19
|
default_executable:
|
19
20
|
dependencies:
|
20
21
|
- !ruby/object:Gem::Dependency
|
21
22
|
name: rspec
|
22
23
|
prerelease: false
|
23
24
|
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
24
26
|
requirements:
|
25
27
|
- - ">="
|
26
28
|
- !ruby/object:Gem::Version
|
29
|
+
hash: 13
|
27
30
|
segments:
|
28
31
|
- 1
|
29
32
|
- 2
|
@@ -48,6 +51,9 @@ files:
|
|
48
51
|
- lib/br/cnpj.rb
|
49
52
|
- LICENSE
|
50
53
|
- README.rdoc
|
54
|
+
- spec/spec_helper.rb
|
55
|
+
- spec/br-cnpj_spec.rb
|
56
|
+
- spec/faker_cnpj.rb
|
51
57
|
has_rdoc: true
|
52
58
|
homepage: http://github.com/bbcoimbra/br-cnpj
|
53
59
|
licenses: []
|
@@ -58,23 +64,27 @@ rdoc_options:
|
|
58
64
|
require_paths:
|
59
65
|
- lib
|
60
66
|
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
61
68
|
requirements:
|
62
69
|
- - ">="
|
63
70
|
- !ruby/object:Gem::Version
|
71
|
+
hash: 3
|
64
72
|
segments:
|
65
73
|
- 0
|
66
74
|
version: "0"
|
67
75
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
68
77
|
requirements:
|
69
78
|
- - ">="
|
70
79
|
- !ruby/object:Gem::Version
|
80
|
+
hash: 3
|
71
81
|
segments:
|
72
82
|
- 0
|
73
83
|
version: "0"
|
74
84
|
requirements: []
|
75
85
|
|
76
86
|
rubyforge_project:
|
77
|
-
rubygems_version: 1.3.
|
87
|
+
rubygems_version: 1.3.7
|
78
88
|
signing_key:
|
79
89
|
specification_version: 3
|
80
90
|
summary: Calcules and validates given CNPJ
|