cnpj_validator 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ .rvmrc
4
+ Gemfile.lock
5
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in cnpj_validator.gemspec
4
+ gemspec
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009-2011 Gabriel Sobrinho
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.
@@ -0,0 +1,17 @@
1
+ h1. CnpjValidator
2
+
3
+ CNPJ validation for ActiveModel
4
+
5
+ h2. Installation
6
+
7
+ <pre>gem "cnpj_validator"</pre>
8
+
9
+ h2. Usage
10
+
11
+ <pre>class Company < ActiveRecord::Base
12
+ validates :cnpj, :cnpj => true
13
+ end</pre>
14
+
15
+ h2. License
16
+
17
+ Copyright (c) 2009-2011 Gabriel Sobrinho, released under the MIT license
@@ -0,0 +1,11 @@
1
+ require 'bundler'
2
+ require 'rake/testtask'
3
+
4
+ Bundler::GemHelper.install_tasks
5
+
6
+ task :default => :test
7
+
8
+ Rake::TestTask.new(:test) do |t|
9
+ t.libs << 'test'
10
+ t.pattern = 'test/**/*_test.rb'
11
+ end
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+ Gem::Specification.new do |s|
3
+ s.name = "cnpj_validator"
4
+ s.version = "0.3.0"
5
+ s.platform = Gem::Platform::RUBY
6
+ s.authors = ["Gabriel Sobrinho"]
7
+ s.email = ["gabriel.sobrinho@gmail.com"]
8
+ s.homepage = "https://github.com/sobrinho/cnpj_validator"
9
+ s.summary = %q{CNPJ validation for ActiveModel}
10
+
11
+ s.files = `git ls-files`.split("\n")
12
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
13
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
14
+ s.require_paths = ["lib"]
15
+
16
+ s.add_dependency 'activemodel', '>= 3.0'
17
+ s.add_development_dependency 'rake', '>= 0.8.7'
18
+ end
@@ -0,0 +1,7 @@
1
+ class CnpjValidator < ActiveModel::EachValidator
2
+ autoload :Cnpj, 'cnpj_validator/cnpj'
3
+
4
+ def validate_each(record, attribute, value)
5
+ record.errors.add(attribute, options[:message]) unless Cnpj.valid?(value)
6
+ end
7
+ end
@@ -0,0 +1,53 @@
1
+ class CnpjValidator::Cnpj
2
+ def self.valid?(number)
3
+ new(number).valid?
4
+ end
5
+
6
+ attr_reader :number
7
+
8
+ def initialize(number)
9
+ @number = number
10
+ end
11
+
12
+ def digits
13
+ @digits ||= number.scan(/\d/).map(&:to_i)
14
+ end
15
+
16
+ def plain
17
+ @plain ||= digits.join
18
+ end
19
+
20
+ def valid?
21
+ formatted? && !black_listed? && digits_matches?
22
+ end
23
+
24
+ private
25
+
26
+ def formatted?
27
+ plain =~ /^\d{14}|\d{2}\.\d{3}\.\d{3}\/\d{4}-\d{2}$/
28
+ end
29
+
30
+ def black_listed?
31
+ plain =~ /^(\d)\1{13}$/
32
+ end
33
+
34
+ def digits_matches?
35
+ digit_matches?(12) && digit_matches?(13)
36
+ end
37
+
38
+ def digit_matches?(digit)
39
+ factor = [5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]
40
+ factor.unshift 6 if digit == 13
41
+
42
+ sum = 0
43
+
44
+ digit.times do |i|
45
+ sum += digits[i] * factor[i]
46
+ end
47
+
48
+ result = sum % 11
49
+ result = result < 2 ? 0 : 11 - result
50
+
51
+ result == digits[digit]
52
+ end
53
+ end
@@ -0,0 +1,43 @@
1
+ require 'test_helper'
2
+
3
+ class CnpjValidatorTest < Test::Unit::TestCase
4
+ def test_black_list
5
+ %w(00000000000000 11111111111111 22222222222222 33333333333333
6
+ 44444444444444 55555555555555 66666666666666 77777777777777
7
+ 88888888888888 99999999999999).each do |number|
8
+ assert_invalid number
9
+ end
10
+ end
11
+
12
+ def test_invalid
13
+ assert_invalid '50.108.399/7175-07'
14
+ assert_invalid '77.017.159/3095-80'
15
+ assert_invalid '03.971.701/5859-70'
16
+ end
17
+
18
+ def test_masked
19
+ assert_valid '38.417.923/0001-16'
20
+ assert_valid '43.010.889/0001-09'
21
+ assert_valid '85.113.468/0001-45'
22
+ end
23
+
24
+ def test_unmasked
25
+ assert_valid '38417923000116'
26
+ assert_valid '43010889000109'
27
+ assert_valid '85113468000145'
28
+ end
29
+
30
+ protected
31
+
32
+ def assert_valid(cnpj)
33
+ assert company(:cnpj => cnpj).valid?
34
+ end
35
+
36
+ def assert_invalid(cnpj)
37
+ assert company(:cnpj => cnpj).invalid?
38
+ end
39
+
40
+ def company(attributes = {})
41
+ Company.new(attributes)
42
+ end
43
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'active_model'
4
+ require 'cnpj_validator'
5
+ require 'ostruct'
6
+
7
+ class Company < OpenStruct
8
+ include ActiveModel::Validations
9
+ validates :cnpj, :cnpj => true
10
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cnpj_validator
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.3.0
6
+ platform: ruby
7
+ authors:
8
+ - Gabriel Sobrinho
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-07-02 00:00:00 -03:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: activemodel
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "3.0"
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: 0.8.7
36
+ type: :development
37
+ version_requirements: *id002
38
+ description:
39
+ email:
40
+ - gabriel.sobrinho@gmail.com
41
+ executables: []
42
+
43
+ extensions: []
44
+
45
+ extra_rdoc_files: []
46
+
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - MIT-LICENSE
51
+ - README.textile
52
+ - Rakefile
53
+ - cnpj_validator.gemspec
54
+ - lib/cnpj_validator.rb
55
+ - lib/cnpj_validator/cnpj.rb
56
+ - test/cnpj_validator_test.rb
57
+ - test/test_helper.rb
58
+ has_rdoc: true
59
+ homepage: https://github.com/sobrinho/cnpj_validator
60
+ licenses: []
61
+
62
+ post_install_message:
63
+ rdoc_options: []
64
+
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: "0"
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: "0"
79
+ requirements: []
80
+
81
+ rubyforge_project:
82
+ rubygems_version: 1.5.2
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: CNPJ validation for ActiveModel
86
+ test_files:
87
+ - test/cnpj_validator_test.rb
88
+ - test/test_helper.rb