br-cpf 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
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,31 @@
1
+ = BR::CPF
2
+
3
+ Lib implementada em C (gera uma lib compilada nativamente) que calcula e valida CPFs.
4
+
5
+ == Instalação
6
+
7
+ sudo gem install br-cpf
8
+
9
+ == Exemplos
10
+
11
+ require 'rubygems'
12
+ require 'br/cpf'
13
+
14
+ BR::CPF.valid? '00000000191'
15
+ => true
16
+
17
+ BR::CPF.valid? 12345678909
18
+ => true
19
+
20
+ BR::CPF.valid? 12345678900
21
+ => false
22
+
23
+ raiz = 0
24
+ BR::CPF.new(raiz).to_s
25
+ => '00000000191'
26
+
27
+
28
+
29
+ == Copyright
30
+
31
+ Copyright (c) 2010 Bruno Coimbra. Veja o arquivo LICENSE para mais detalhes.
data/ext/CPF/cpf.c ADDED
@@ -0,0 +1,34 @@
1
+ #include <stdlib.h>
2
+ #include "cpf.h"
3
+
4
+ long long calcula_digito_cpf(long long raiz){
5
+ int produto=0, i, digito;
6
+ long long aux=raiz, cpf;
7
+ lldiv_t divisao;
8
+
9
+ cpf=(long long)raiz*100;
10
+ for(i=0; i<=8; i++){
11
+ divisao = lldiv(aux, 10);
12
+ produto += divisao.rem * (i+2);
13
+ aux = divisao.quot;
14
+ }
15
+ digito = produto % 11 < 2 ? 0 : 11 - (produto % 11);
16
+ cpf += (digito*10);
17
+ produto = 0;
18
+ aux = (long long)cpf / 10;
19
+ for(i=0; i<=9; i++){
20
+ divisao = lldiv(aux, 10);
21
+ produto += divisao.rem * (i+2);
22
+ aux = divisao.quot;
23
+ }
24
+ digito = produto % 11 < 2 ? 0 : 11 - (produto % 11);
25
+ cpf += digito;
26
+ return cpf;
27
+ }
28
+
29
+
30
+ int e_cpf(long long cpf){
31
+ return cpf==calcula_digito_cpf((long long)cpf / 100);
32
+ }
33
+
34
+
data/ext/CPF/cpf.h ADDED
@@ -0,0 +1,15 @@
1
+ #ifndef _CPF_H
2
+ #define _CPF_H
3
+
4
+ #ifdef __cplusplus
5
+ extern "C" {
6
+ #endif
7
+
8
+ long long calcula_digito_cpf(long long raiz);
9
+ int e_cpf(long long cpf);
10
+
11
+ #ifdef __cplusplus
12
+ }
13
+ #endif
14
+
15
+ #endif /* _CPF_H */
@@ -0,0 +1,7 @@
1
+ require 'mkmf'
2
+
3
+ dir_config("CPF")
4
+
5
+ have_header("stdlib.h")
6
+
7
+ create_makefile("CPF")
@@ -0,0 +1,33 @@
1
+ #include <stdlib.h>
2
+ #include "ruby.h"
3
+ #include "cpf.h"
4
+
5
+ static VALUE t_init_cpf(VALUE self, VALUE ruby_cpf_raiz){
6
+ long long cpf = 0, raiz = 0;
7
+ VALUE ruby_cpf;
8
+
9
+ raiz = NUM2LL(ruby_cpf_raiz);
10
+ if(raiz < 1 || raiz > 999999999)
11
+ rb_raise(rb_eArgError, "raiz should be greather than 0 and lesser than 999_999_999");
12
+ cpf = calcula_digito_cpf((long long) raiz);
13
+ ruby_cpf = LL2NUM(cpf);
14
+ rb_iv_set(self, "@raiz", INT2FIX(raiz));
15
+ rb_iv_set(self, "@cpf", ruby_cpf);
16
+ rb_iv_set(self, "@valid", e_cpf(cpf) ? Qtrue : Qfalse );
17
+ return self;
18
+ }
19
+
20
+ static VALUE valid_cpf(VALUE self, VALUE ruby_cpf){
21
+ long long cpf = 0;
22
+ cpf = (long long)NUM2LL(ruby_cpf);
23
+ if(cpf == 0) return Qfalse;
24
+ return e_cpf(cpf) ? Qtrue : Qfalse;
25
+ }
26
+
27
+ VALUE mBr, cCpf;
28
+ void Init_CPF(){
29
+ mBr = rb_define_module("BR");
30
+ cCpf = rb_define_class_under(mBr, "CPF", rb_cObject);
31
+ rb_define_method(cCpf, "initialize", t_init_cpf, 1);
32
+ rb_define_singleton_method(cCpf, "valid?", valid_cpf, 1);
33
+ }
data/lib/br/cpf.rb ADDED
@@ -0,0 +1,25 @@
1
+ require File.join(File.dirname(__FILE__), '../CPF')
2
+
3
+ module BR
4
+ class CPF
5
+ class << self
6
+ alias_method :orig_valid?, :valid?
7
+ end
8
+
9
+ def self.valid?(cpf)
10
+ if cpf.is_a? String
11
+ orig_valid?(cpf.to_i)
12
+ else
13
+ orig_valid?(cpf)
14
+ end
15
+ end
16
+
17
+ def to_i
18
+ @raiz * 100 + @verif
19
+ end
20
+
21
+ def to_s
22
+ "%011d" % to_i
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,37 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe BR::CPF do
4
+
5
+ context "when Fixnum" do
6
+
7
+ it "should return true for valid cpfs" do
8
+ VALID_CPFS_INTEGER.each do |valid_cpf|
9
+ BR::CPF.valid?(valid_cpf).should be_true
10
+ end
11
+ end
12
+
13
+ it "should return false for invalid cpfs" do
14
+ INVALID_CPFS_INTEGER.each do |invalid_cpf|
15
+ BR::CPF.valid?(invalid_cpf).should be_false
16
+ end
17
+ end
18
+
19
+ end
20
+
21
+ context "when String" do
22
+
23
+ it "should return true for valid cpfs" do
24
+ VALID_CPFS_STRING.each do |valid_cpf|
25
+ BR::CPF.valid?(valid_cpf).should be_true
26
+ end
27
+ end
28
+
29
+ it "should return false for invalid cpfs" do
30
+ INVALID_CPFS_STRING.each do |invalid_cpf|
31
+ BR::CPF.valid?(invalid_cpf).should be_false
32
+ end
33
+ end
34
+
35
+ end
36
+
37
+ end
data/spec/faker_cpf.rb ADDED
@@ -0,0 +1,55 @@
1
+ module Faker
2
+ module CPF
3
+ VALID_CPFS_INTEGER = [
4
+ 574070532, 574104534, 574125531, 574232532, 574248536,
5
+ 574645535, 574768530, 574789537, 574875530, 574880534,
6
+ 574935533, 574940537, 584033532, 584049536, 584075537,
7
+ 584096534, 584156537, 584177534, 584242530, 584344538,
8
+ 584451539, 584467532, 584506538, 584511531, 584548532,
9
+ 584660537, 584817533, 584987536, 594022533, 594059534,
10
+ 594090539, 594150531, 594166535, 594187532, 594375533,
11
+ 594409535, 594414539, 594456533, 594482534, 594516536,
12
+ 594537533, 594563534, 594584531, 594670535, 594686539,
13
+ 594853532, 594908531, 594913535, 594955530, 594997534,
14
+ 604017537, 604043538, 604119534, 604187530, 604210531,
15
+ 604231539, 604247532, 604349530, 604380534, 604435533,
16
+ 604461534, 604482531, 604542534, 604563531, 604584539,
17
+ 604623534, 604670532, 604691530, 604704534, 604746539,
18
+ 604853530, 604929536, 604976534, 614113539, 614129532,
19
+ 614134536, 614181534, 614197538, 614343534, 614364531,
20
+ 614403537, 614471532, 614487536, 614505534, 614526531,
21
+ 614612535, 614649536, 614654530, 614696534, 614777534,
22
+ 614800536, 614918537, 614923530, 614986532, 624000532,
23
+ 624037533, 624123537, 624139530, 624170535, 624186539,
24
+ 18884997534, 18894160530, 18904777534, 18904858534, 18904939534,
25
+ 18934250534, 18974473534, 18974716534, 18984002534, 18994156534,
26
+ 19004990534, 19014872534, 19024550530, 19024592534, 19024673534,
27
+ 19044194534, 19044275534, 19054319534, 19054700530, 19054904534,
28
+ 19064381534, 19064705534, 19074778534, 19074859534, 19084145534,
29
+ 19084307534, 19084498534, 19084960530, 19094027534, 19094612534,
30
+ 19104740530, 19114230534, 19114745534, 19124031534, 19124112534,
31
+ 19124180530, 19124899534, 19134185534, 19134266534, 19144067534,
32
+ 19144148534, 19144490534, 19144610530, 19144652534, 19144814534,
33
+ 19154372534, 19154615534, 19164920534, 19174136534, 19174993534,
34
+ 19184018534, 19184590530, 19184603534, 19194595534, 19194919534,
35
+ 19204370530, 19214537534, 19224419534, 19224761534, 19224800530,
36
+ 19234058534, 19234520530, 19234643534, 19234996534, 19244797534,
37
+ 19254083534, 19254245534, 19254598534, 19254830534, 19264712534,
38
+ 19264984534, 19274270534, 19274351534, 19274513534, 19274785534,
39
+ 19284110530, 19284152534, 19284314534, 19284829534, 19294387534,
40
+ 19294891534, 19294930530, 19294972534, 19304447534, 19314167534,
41
+ 19314710530, 19324200534, 19324430530, 19324715534, 19334273534,
42
+ 19344236534, 19344740534, 19354541534, 19354690530, 19354703534,
43
+ 19354894534, 19354975534, 19374143534, 19384459534, 19384610534,
44
+ ]
45
+
46
+ INVALID_CPFS_INTEGER = VALID_CPFS_INTEGER.collect { |cpf| cpf + 1}
47
+
48
+ VALID_CPFS_STRING = VALID_CPFS_INTEGER.collect { |cpf| cpf.to_s }
49
+
50
+ INVALID_CPFS_STRING = INVALID_CPFS_INTEGER.collect { |cpf| cpf.to_s}
51
+
52
+ end
53
+ end
54
+
55
+ include Faker::CPF
@@ -0,0 +1,6 @@
1
+ $:.unshift(File.dirname(__FILE__))
2
+ $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'br/cpf'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+ require 'faker_cpf'
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: br-cpf
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Bruno Coimbra
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-04-14 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: Lib implemented in C that calculates and validates CPF.
35
+ email: bbcoimbra@gmail.com
36
+ executables: []
37
+
38
+ extensions:
39
+ - ext/CPF/extconf.rb
40
+ extra_rdoc_files:
41
+ - LICENSE
42
+ - README.rdoc
43
+ files:
44
+ - ext/CPF/cpf.c
45
+ - ext/CPF/cpf.h
46
+ - ext/CPF/extconf.rb
47
+ - ext/CPF/ruby_cpf.c
48
+ - lib/br/cpf.rb
49
+ - LICENSE
50
+ - README.rdoc
51
+ has_rdoc: true
52
+ homepage: http://github.com/bbcoimbra/br-cpf
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: Calculates and validates given CPF
81
+ test_files:
82
+ - spec/faker_cpf.rb
83
+ - spec/spec_helper.rb
84
+ - spec/br-cpf_spec.rb