br-cnpj 0.1.1

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.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Bruno Coimbra
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,17 @@
1
+ = br-cnpj
2
+
3
+ Calcules and validates Brazilian CNPJ
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 Bruno Coimbra. See LICENSE for details.
data/ext/CNPJ/cnpj.c ADDED
@@ -0,0 +1,72 @@
1
+ #include <stdlib.h>
2
+ #include "cnpj.h"
3
+
4
+ #define DIGITO1 0
5
+ #define DIGITO2 1
6
+
7
+ long long calcula_digito_cnpj(int raiz, int filial){
8
+ return (long long)raiz*1000000 + filial*100 + calcula_digito(raiz, filial);
9
+ }
10
+
11
+ int calcula_digito(int raiz, int filial){
12
+ int digito1=0, digito2=0, i;
13
+ int vet[14];
14
+ int pesos[][13] = {{5,4,3,2,9,8,7,6,5,4,3,2,0},
15
+ {6,5,4,3,2,9,8,7,6,5,4,3,2}};
16
+ long long cnpj;
17
+ div_t divisao;
18
+
19
+ cnpj = (long long)raiz*1000000 + filial*100;
20
+ for (i=0;i<14;i++){
21
+ lldiv_t divisao;
22
+
23
+ divisao = lldiv(cnpj,10);
24
+ vet[13-i] = (int) divisao.rem;
25
+ cnpj = divisao.quot;
26
+ }
27
+ for(i=0;i<12;i++)
28
+ digito1 += vet[i] * pesos[DIGITO1][i];
29
+ divisao = div(digito1,11);
30
+ digito1 = divisao.rem<2 ? 0 : 11-divisao.rem;
31
+ vet[12]=digito1;
32
+ for(i=0;i<13;i++)
33
+ digito2 += vet[i] * pesos[DIGITO2][i];
34
+ divisao = div(digito2,11);
35
+ digito2 = divisao.rem<2 ? 0 : 11- divisao.rem;
36
+
37
+ return digito1*10 + digito2;
38
+ }
39
+
40
+ int e_cnpj(long long cnpj_l){
41
+ int raiz, filial;
42
+ lldiv_t divisao;
43
+
44
+ divisao = lldiv(cnpj_l,1000000);
45
+ raiz = divisao.quot;
46
+ divisao = lldiv(divisao.rem,100);
47
+ filial=divisao.quot;
48
+ return cnpj_l == calcula_digito_cnpj(raiz, filial);
49
+ }
50
+
51
+ int e_cgc(long long cgc_l){
52
+ if(e_cnpj(cgc_l)){
53
+ int pesos[]={2,1,2,1,2,1,2,1};
54
+ int acumulador=0, raiz=0, i=0;
55
+ lldiv_t divisao;
56
+
57
+ divisao = lldiv(cgc_l, 1000000);
58
+ raiz = (int)divisao.quot;
59
+ for (i=0;i<8;i++){
60
+ int produto=0;
61
+
62
+ divisao = lldiv(raiz,10);
63
+ produto = divisao.rem*pesos[7-i];
64
+ acumulador += (produto>9) ? produto-9 : produto;
65
+ raiz = divisao.quot;
66
+ }
67
+
68
+ divisao=lldiv(acumulador, 10);
69
+ return !divisao.rem;
70
+ }
71
+ return 0;
72
+ }
data/ext/CNPJ/cnpj.h ADDED
@@ -0,0 +1,17 @@
1
+ #ifndef _CNPJ_H
2
+ #define _CNPJ_H
3
+
4
+ #ifdef __cplusplus
5
+ extern "C" {
6
+ #endif
7
+
8
+ long long calcula_digito_cnpj(int raiz, int filial);
9
+ int calcula_digito(int raiz, int filial);
10
+ int e_cnpj(long long cnpj_l);
11
+ int e_cgc(long long cgc_l);
12
+
13
+ #ifdef __cplusplus
14
+ }
15
+ #endif
16
+
17
+ #endif /* _CNPJ_H */
@@ -0,0 +1,7 @@
1
+ require 'mkmf'
2
+
3
+ dir_config('cnpj')
4
+
5
+ have_header('stdlib.h')
6
+
7
+ create_makefile('CNPJ')
@@ -0,0 +1,71 @@
1
+ #include "ruby.h"
2
+ #include "cnpj.h"
3
+
4
+ static VALUE t_init_cnpj(int argc, VALUE *argv, VALUE self)
5
+ {
6
+ int raiz = 0;
7
+ int filial = 0;
8
+ int verif = 0;
9
+ VALUE valid = Qnil;
10
+
11
+ if (argc == 2) {
12
+ raiz = FIX2INT(argv[0]);
13
+ if( raiz >= 100000000 || raiz < 0 )
14
+ rb_raise(rb_eArgError, "raiz should be greater than -1 or lesser than 10_000_000");
15
+ filial = NUM2INT(argv[1]);
16
+ if( filial >= 10000 || filial < 1)
17
+ rb_raise(rb_eArgError, "filial should be greater than 0 or lesser than 10_000");
18
+ verif = calcula_digito(raiz,filial);
19
+ valid = Qtrue;
20
+ }
21
+ else if (argc == 1) {
22
+ lldiv_t v;
23
+ long long cnpj = 0;
24
+
25
+ if (rb_class_of(argv[0]) == rb_cString)
26
+ cnpj = NUM2LL(rb_str_to_inum(argv[0], 10, 0));
27
+ else
28
+ if (rb_class_of(argv[0]) == rb_cFixnum ||
29
+ rb_class_of(argv[0]) == rb_cBignum)
30
+ cnpj = NUM2LL(argv[0]);
31
+
32
+ v = lldiv(cnpj, (long long)100);
33
+ verif = (int)v.rem;
34
+ v = lldiv(v.quot, (long long)10000);
35
+ filial = (int)v.rem;
36
+ raiz = (int)v.quot;
37
+ valid = verif == calcula_digito(raiz,filial) ? Qtrue : Qfalse;
38
+ }
39
+
40
+ rb_iv_set(self, "@raiz", INT2FIX(raiz));
41
+ rb_iv_set(self, "@filial", INT2FIX(filial));
42
+ rb_iv_set(self, "@verif", INT2FIX(verif));
43
+ rb_iv_set(self, "@valid", valid);
44
+
45
+ return self;
46
+ }
47
+
48
+ static VALUE is_cgc(VALUE self){
49
+ long long raiz=0;
50
+ int filial = 0, dv = 0;
51
+ raiz = NUM2INT(rb_iv_get(self, "@raiz"));
52
+ filial = NUM2INT(rb_iv_get(self, "@filial"));
53
+ dv = NUM2INT(rb_iv_get(self, "@verif"));
54
+ return e_cgc((long long)raiz * 1000000 + filial * 100 + dv) ? Qtrue : Qfalse;
55
+ }
56
+
57
+ static VALUE valid_cnpj(VALUE self, VALUE ruby_cnpj){
58
+ long long cnpj=0;
59
+ cnpj = (long long)NUM2LL(ruby_cnpj);
60
+ if(cnpj == 0) return Qfalse;
61
+ return e_cnpj(cnpj) ? Qtrue : Qfalse;
62
+ }
63
+
64
+ VALUE mBR, cCNPJ;
65
+ void Init_CNPJ(){
66
+ mBR = rb_define_module("BR");
67
+ cCNPJ = rb_define_class_under(mBR, "CNPJ", rb_cObject);
68
+ rb_define_method(cCNPJ, "initialize", t_init_cnpj, -1);
69
+ rb_define_singleton_method(cCNPJ, "valid?", valid_cnpj, 1);
70
+ rb_define_method(cCNPJ, "is_cgc?", is_cgc, 0);
71
+ }
data/lib/br/cnpj.rb ADDED
@@ -0,0 +1,25 @@
1
+ require File.join(File.dirname(__FILE__), '../CNPJ')
2
+
3
+ module BR
4
+ class CNPJ
5
+ class << self
6
+ alias_method :orig_valid?, :valid?
7
+ end
8
+
9
+ def self.valid?(cnpj)
10
+ if cnpj.is_a? String
11
+ orig_valid?(cnpj.to_i)
12
+ else
13
+ orig_valid?(cnpj)
14
+ end
15
+ end
16
+
17
+ def to_i
18
+ @raiz * 1_000_000 + @filial * 100 + @verif
19
+ end
20
+
21
+ def to_s
22
+ "%014d" % to_i
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,7 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "BrCnpj" do
4
+ it "fails" do
5
+ fail "No specs defined yet"
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'br-cnpj'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: br-cnpj
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 1
9
+ version: 0.1.1
10
+ platform: ruby
11
+ authors:
12
+ - Bruno Coimbra
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-04-09 00:00:00 -03:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 2
30
+ - 9
31
+ version: 1.2.9
32
+ type: :development
33
+ version_requirements: *id001
34
+ description: no descripton yet
35
+ email: bbcoimbra@gmail.com
36
+ executables: []
37
+
38
+ extensions:
39
+ - ext/CNPJ/extconf.rb
40
+ extra_rdoc_files:
41
+ - LICENSE
42
+ - README.rdoc
43
+ files:
44
+ - ext/CNPJ/cnpj.c
45
+ - ext/CNPJ/cnpj.h
46
+ - ext/CNPJ/extconf.rb
47
+ - ext/CNPJ/ruby_cnpj.c
48
+ - lib/br/cnpj.rb
49
+ - LICENSE
50
+ - README.rdoc
51
+ has_rdoc: true
52
+ homepage: http://github.com/bbcoimbra/br-cnpj
53
+ licenses: []
54
+
55
+ post_install_message:
56
+ rdoc_options:
57
+ - --charset=UTF-8
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ segments:
65
+ - 0
66
+ version: "0"
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ requirements: []
75
+
76
+ rubyforge_project:
77
+ rubygems_version: 1.3.6
78
+ signing_key:
79
+ specification_version: 3
80
+ summary: Calcules and validates given CNPJ
81
+ test_files:
82
+ - spec/spec_helper.rb
83
+ - spec/br-cnpj_spec.rb