validates_cnpj 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/.rspec +1 -0
- data/.rvmrc +1 -0
- data/Gemfile +4 -0
- data/README.rdoc +32 -0
- data/Rakefile +7 -0
- data/lib/validates_cnpj.rb +8 -0
- data/lib/validates_cnpj/cnpj.rb +52 -0
- data/lib/validates_cnpj/version.rb +3 -0
- data/spec/fake_app/admin.rb +2 -0
- data/spec/fake_app/company.rb +3 -0
- data/spec/fake_app/db/create_admins.rb +11 -0
- data/spec/fake_app/db/create_companies.rb +11 -0
- data/spec/spec_helper.rb +11 -0
- data/spec/validates_cnpj/cnpj_spec.rb +91 -0
- data/spec/validates_cnpj_spec.rb +38 -0
- data/validates_cnpj.gemspec +28 -0
- metadata +127 -0
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--colour --format documentation
|
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use 1.9.3-p0@validates_cnpj
|
data/Gemfile
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
= ValidatesCNPJ {<img src="https://secure.travis-ci.org/plribeiro3000/validates_cnpj.png" />}[http://travis-ci.org/plribeiro3000/validates_cnpj]
|
2
|
+
|
3
|
+
Rails gem to validate CNPJ
|
4
|
+
|
5
|
+
== Install
|
6
|
+
|
7
|
+
gem install validates_cnpj
|
8
|
+
|
9
|
+
== Usage
|
10
|
+
|
11
|
+
Lets say you have a model with "cnpj" string column that you want to be a valid CNPJ. Just add this to your model:
|
12
|
+
|
13
|
+
class User < ActiveRecord::Base
|
14
|
+
validates :cnpj, :cnpj => true
|
15
|
+
end
|
16
|
+
|
17
|
+
== Test
|
18
|
+
|
19
|
+
Only Rspec supported at the moment. The gem already have macros for shoulda-macros and remarkable.
|
20
|
+
|
21
|
+
If you are using shoulda-matchers, add this line to your spec_helper.rb :
|
22
|
+
require "validates_cnpj/shoulda-matchers/validate_as_cnpj_matcher"
|
23
|
+
If you are using remarkable, add this line to your spec_helper.rb :
|
24
|
+
require "validates_cnpj/remarkable/validate_as_cnpj_matcher"
|
25
|
+
|
26
|
+
=== How?
|
27
|
+
|
28
|
+
You should use validates_as_cnpj(:attribute) just like any other shoulda matcher.
|
29
|
+
|
30
|
+
== Special Thanks
|
31
|
+
|
32
|
+
This project is based on brcpfcnpj gem and his intention it to mantain a cleaner code to validate CNPJ and easy macros to test it.
|
data/Rakefile
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
class CNPJ
|
2
|
+
def initialize(number)
|
3
|
+
number =~ /^(\d{2}\.?\d{3}\.?\d{3}\/?\d{4})-?(\d{2})$/
|
4
|
+
@number = number
|
5
|
+
@pure_number = $1
|
6
|
+
@result = $2
|
7
|
+
@cleaned_number = @pure_number.nil? ? nil : @number.gsub(/[\.\/-]/, "")
|
8
|
+
format_number! if @pure_number
|
9
|
+
end
|
10
|
+
|
11
|
+
def valid?
|
12
|
+
return true if @number.nil?
|
13
|
+
return false unless @pure_number
|
14
|
+
check_cnpj
|
15
|
+
end
|
16
|
+
|
17
|
+
def number
|
18
|
+
@number
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def check_cnpj
|
24
|
+
return false if @cleaned_number.length != 14 or @cleaned_number.scan(/\d/).uniq.length == 1
|
25
|
+
@result == first_digit_verifier + second_digit_verifier
|
26
|
+
end
|
27
|
+
|
28
|
+
def first_digit_verifier
|
29
|
+
sum = multiply_and_sum([5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2], @pure_number)
|
30
|
+
digit_verifier(sum%11).to_s
|
31
|
+
end
|
32
|
+
|
33
|
+
def second_digit_verifier
|
34
|
+
sum = multiply_and_sum([6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2], @pure_number + first_digit_verifier)
|
35
|
+
digit_verifier(sum%11).to_s
|
36
|
+
end
|
37
|
+
|
38
|
+
def multiply_and_sum(array, number)
|
39
|
+
multiplied = []
|
40
|
+
number.scan(/\d{1}/).each_with_index { |e, i| multiplied[i] = e.to_i * array[i] }
|
41
|
+
multiplied.inject { |s,e| s + e }
|
42
|
+
end
|
43
|
+
|
44
|
+
def digit_verifier(rest)
|
45
|
+
rest < 2 ? 0 : 11 - rest
|
46
|
+
end
|
47
|
+
|
48
|
+
def format_number!
|
49
|
+
@cleaned_number =~ /(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})/
|
50
|
+
@number = "#{$1}.#{$2}.#{$3}/#{$4}-#{$5}"
|
51
|
+
end
|
52
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "rspec"
|
3
|
+
require "active_record"
|
4
|
+
|
5
|
+
Dir.glob(File.dirname(__FILE__) + "/../lib/**/*.rb").each { |file| require file }
|
6
|
+
Dir.glob(File.dirname(__FILE__) + "/fake_app/**/*.rb").each { |file| require file }
|
7
|
+
|
8
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
9
|
+
|
10
|
+
CreateCompanies.migrate(:up)
|
11
|
+
CreateAdmins.migrate(:up)
|
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe CNPJ do
|
4
|
+
context "should be invalid with" do
|
5
|
+
it "blank number" do
|
6
|
+
CNPJ.new('').should_not be_valid
|
7
|
+
end
|
8
|
+
|
9
|
+
it "04.22A.284/0001-11 as number" do
|
10
|
+
CNPJ.new('04.22A.284/0001-11').should_not be_valid
|
11
|
+
end
|
12
|
+
|
13
|
+
it "04.222-284.0001-11 as number" do
|
14
|
+
CNPJ.new('04.222-284.0001-11').should_not be_valid
|
15
|
+
end
|
16
|
+
|
17
|
+
it "04222/284/0001-11 as number" do
|
18
|
+
CNPJ.new('04222/284/0001-11').should_not be_valid
|
19
|
+
end
|
20
|
+
|
21
|
+
it "69103604020160 as number" do
|
22
|
+
CNPJ.new('69103604020160').should_not be_valid
|
23
|
+
end
|
24
|
+
|
25
|
+
it "00000000000000 as number" do
|
26
|
+
CNPJ.new('00000000000000').should_not be_valid
|
27
|
+
end
|
28
|
+
|
29
|
+
it "69.103.604/0001-61 as number" do
|
30
|
+
CNPJ.new('69.103.604/0001-61').should_not be_valid
|
31
|
+
end
|
32
|
+
|
33
|
+
it "01618211000264 as number" do
|
34
|
+
CNPJ.new('01618211000264').should_not be_valid
|
35
|
+
end
|
36
|
+
|
37
|
+
it "691036040001-601 as number" do
|
38
|
+
CNPJ.new('691036040001-601').should_not be_valid
|
39
|
+
end
|
40
|
+
|
41
|
+
it "69103604000160a as number" do
|
42
|
+
CNPJ.new('69103604000160a').should_not be_valid
|
43
|
+
end
|
44
|
+
|
45
|
+
it "69103604000160ABC as number" do
|
46
|
+
CNPJ.new('69103604000160ABC').should_not be_valid
|
47
|
+
end
|
48
|
+
|
49
|
+
it "6910360400016000 as number" do
|
50
|
+
CNPJ.new('6910360400016000').should_not be_valid
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
context "should be valid with" do
|
55
|
+
it "nil as number" do
|
56
|
+
CNPJ.new(nil).should be_valid
|
57
|
+
end
|
58
|
+
|
59
|
+
it "69103604000160 as number" do
|
60
|
+
CNPJ.new('69103604000160').should be_valid
|
61
|
+
end
|
62
|
+
|
63
|
+
it "69.103.604/0001-60 as number" do
|
64
|
+
CNPJ.new('69.103.604/0001-60').should be_valid
|
65
|
+
end
|
66
|
+
|
67
|
+
it "01518211/000264 as number" do
|
68
|
+
CNPJ.new('01518211/000264').should be_valid
|
69
|
+
end
|
70
|
+
|
71
|
+
it "01.5182110002-64 as number" do
|
72
|
+
CNPJ.new('01.5182110002-64').should be_valid
|
73
|
+
end
|
74
|
+
|
75
|
+
it "00.000.000/1447-89 as number" do
|
76
|
+
CNPJ.new('00.000.000/1447-89').should be_valid
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
context "with a valid value" do
|
81
|
+
it "should return it formatted" do
|
82
|
+
CNPJ.new('69103604000160').number.should == '69.103.604/0001-60'
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
context "with an invalid value" do
|
87
|
+
it "should return as it was" do
|
88
|
+
CNPJ.new('123456').number.should == '123456'
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe CnpjValidator do
|
4
|
+
context "when cnpj is invalid" do
|
5
|
+
before :each do
|
6
|
+
@company = Company.new(:cnpj => "12345")
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should set object as invalid" do
|
10
|
+
@company.valid?.should be_false
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should set an error on attribute" do
|
14
|
+
@company.valid?
|
15
|
+
@company.errors[:cnpj].should == ['is invalid']
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
context "when cnpj is valid" do
|
20
|
+
before :each do
|
21
|
+
@company = Company.new(:cnpj => "37525685000108")
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should set object as valid" do
|
25
|
+
@company.valid?.should be_true
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should not set an error on attribute" do
|
29
|
+
@company.valid?
|
30
|
+
@company.errors[:cnpj].should be_blank
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should accept a nil value" do
|
35
|
+
@company = Company.new(:cnpj => nil)
|
36
|
+
@company.valid?.should be_true
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "validates_cnpj/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "validates_cnpj"
|
7
|
+
s.version = ValidatesCnpj::VERSION
|
8
|
+
s.authors = ["Paulo Henrique Lopes Ribeiro"]
|
9
|
+
s.email = %q{plribeiro3000@gmail.com}
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{CNPJ Validation GEM}
|
12
|
+
s.description = %q{Validates CNPJ and test it with macros in a simple way.}
|
13
|
+
|
14
|
+
s.add_dependency("activerecord", ">= 3.0.0")
|
15
|
+
|
16
|
+
s.rubyforge_project = "validates_cnpj"
|
17
|
+
|
18
|
+
s.files = `git ls-files`.split("\n")
|
19
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
20
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
21
|
+
s.require_paths = %w(lib)
|
22
|
+
|
23
|
+
s.add_development_dependency "rake"
|
24
|
+
s.add_development_dependency "rspec", ">= 2.0.0"
|
25
|
+
s.add_development_dependency "shoulda-matchers", ">= 1.0.0"
|
26
|
+
s.add_development_dependency "remarkable_activerecord", "= 4.0.0.alpha4"
|
27
|
+
s.add_development_dependency "sqlite3"
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: validates_cnpj
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Paulo Henrique Lopes Ribeiro
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-02-21 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activerecord
|
16
|
+
requirement: &10329240 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.0.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *10329240
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rake
|
27
|
+
requirement: &10327660 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *10327660
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rspec
|
38
|
+
requirement: &10327080 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 2.0.0
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *10327080
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: shoulda-matchers
|
49
|
+
requirement: &10326560 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 1.0.0
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *10326560
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: remarkable_activerecord
|
60
|
+
requirement: &10326060 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - =
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: 4.0.0.alpha4
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *10326060
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: sqlite3
|
71
|
+
requirement: &10325600 !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: *10325600
|
80
|
+
description: Validates CNPJ and test it with macros in a simple way.
|
81
|
+
email: plribeiro3000@gmail.com
|
82
|
+
executables: []
|
83
|
+
extensions: []
|
84
|
+
extra_rdoc_files: []
|
85
|
+
files:
|
86
|
+
- .gitignore
|
87
|
+
- .rspec
|
88
|
+
- .rvmrc
|
89
|
+
- Gemfile
|
90
|
+
- README.rdoc
|
91
|
+
- Rakefile
|
92
|
+
- lib/validates_cnpj.rb
|
93
|
+
- lib/validates_cnpj/cnpj.rb
|
94
|
+
- lib/validates_cnpj/version.rb
|
95
|
+
- spec/fake_app/admin.rb
|
96
|
+
- spec/fake_app/company.rb
|
97
|
+
- spec/fake_app/db/create_admins.rb
|
98
|
+
- spec/fake_app/db/create_companies.rb
|
99
|
+
- spec/spec_helper.rb
|
100
|
+
- spec/validates_cnpj/cnpj_spec.rb
|
101
|
+
- spec/validates_cnpj_spec.rb
|
102
|
+
- validates_cnpj.gemspec
|
103
|
+
homepage: ''
|
104
|
+
licenses: []
|
105
|
+
post_install_message:
|
106
|
+
rdoc_options: []
|
107
|
+
require_paths:
|
108
|
+
- lib
|
109
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
110
|
+
none: false
|
111
|
+
requirements:
|
112
|
+
- - ! '>='
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
|
+
none: false
|
117
|
+
requirements:
|
118
|
+
- - ! '>='
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
121
|
+
requirements: []
|
122
|
+
rubyforge_project: validates_cnpj
|
123
|
+
rubygems_version: 1.8.10
|
124
|
+
signing_key:
|
125
|
+
specification_version: 3
|
126
|
+
summary: CNPJ Validation GEM
|
127
|
+
test_files: []
|