validate_cnpj 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bb37b704418393868926712524af42b4b11057f2
4
+ data.tar.gz: ab4433ba48ea09331ec2040b98780a805a556add
5
+ SHA512:
6
+ metadata.gz: 22f324bac9aa81f1205812186a6bcdd9231169f27920de87487088a5daa409a044dec84f67618b83bf2380ae929e18c86f709105347ead4eea67f3024f456ef9
7
+ data.tar.gz: 77e151771b8f99718ea8d545f9d22f206bd2941f926e69a7b23c310c939b9cd51120c842591200791836d29f96fcfa0e11c22197169a312fb8e08519ba909ddc
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ .coveralls.yml
7
+ *.swp
8
+ Gemfile.lock
9
+ InstalledFiles
10
+ _yardoc
11
+ coverage
12
+ doc/
13
+ lib/bundler/man
14
+ pkg
15
+ rdoc
16
+ spec/reports
17
+ tmp
18
+ .*~
19
+ *~
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.12.5
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at hosseinramon@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in validate_cnpj.gemspec
4
+ gemspec
data/LEIAME.md ADDED
@@ -0,0 +1,66 @@
1
+ # ValidateCnpj
2
+
3
+ To read this documentation in English click [here](https://github.com/RamonHossein/validate_cnpj/blob/master/README.md).
4
+
5
+ Adiciona suporte a validação do CNPJ para Rails (ActiveModel) testando de uma forma simples.
6
+
7
+ ## Instalação
8
+
9
+ Adicione esta linha ao Gemfile de sua aplicação:
10
+
11
+ ```ruby
12
+ gem 'validate_cnpj'
13
+ ```
14
+
15
+ E então execute:
16
+
17
+ $ bundle
18
+
19
+ Ou instale ele próprio como:
20
+
21
+ $ gem install validate_cnpj
22
+
23
+ ## Uso
24
+
25
+ Basta usar como qualquer outro validador:
26
+
27
+ ```ruby
28
+ class User < ActiveRecord::Base
29
+ validates :cnpj, cnpj: true
30
+ end
31
+ ```
32
+
33
+ ## Mensagem de erro
34
+
35
+ Se você precisar localizar a mensagem de erro, basta adicioná-la ao seu arquivo I18n locale:
36
+
37
+ ```ruby
38
+ errors:
39
+ messages:
40
+ this_document_is_invalid: "Este documento é invalido"
41
+ ```
42
+
43
+ Você pode fornecer sua própria mensagem usando: opção de mensagem.
44
+
45
+ ```ruby
46
+ validates :cnpj, cnpj: {message: "nova mensagem de erro"}
47
+ ```
48
+
49
+ ## Mantido por:
50
+ [RamonHossein](https://github.com/RamonHossein)
51
+
52
+ ## Contribuidores
53
+
54
+ Para ver as pessoas que têm contribuído com código, dê uma olhada na [lista de contribuintes](http://github.com/RamonHossein/validate_cnpj/contributors).
55
+
56
+ ## Contribuindo
57
+
58
+ 1. Fork
59
+ 2. Crie sua feature branch (`git checkout -b my-new-feature`)
60
+ 3. Commit suas alterações (`git commit -am 'Added some feature'`)
61
+ 4. Push para a branch (`git push origin my-new-feature`)
62
+ 5. Crie um novo Pull Request
63
+
64
+ # Licença
65
+
66
+ A gem está disponível como código aberto sob os termos da [MIT License](http://opensource.org/licenses/MIT).
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 RamonHossein
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,66 @@
1
+ # ValidateCnpj
2
+
3
+ To read this documentation in English click [here](https://github.com/RamonHossein/validate_cnpj/blob/master/README.md).
4
+
5
+ Adds CNPJ validation support to Rails (ActiveModel) and test it in a simple way.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'validate_cnpj'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install validate_cnpj
22
+
23
+ ## Usage
24
+
25
+ Just use as any other validator:
26
+
27
+ ```ruby
28
+ class User < ActiveRecord::Base
29
+ validates :cnpj, cnpj: true
30
+ end
31
+ ```
32
+
33
+ ## Error Message
34
+
35
+ If you need to localize the error message, just add this to your I18n locale file:
36
+
37
+ ```ruby
38
+ errors:
39
+ messages:
40
+ this_document_is_invalid: "This document is invalid"
41
+ ```
42
+
43
+ You can provide your own message using :message option.
44
+
45
+ ```ruby
46
+ validates :cnpj, cnpj: {message: "new error message"}
47
+ ```
48
+
49
+ ## Mantainers
50
+ [RamonHossein](https://github.com/RamonHossein)
51
+
52
+ ## Contributors
53
+
54
+ To see the generous people who have contributed code, take a look at the [contributors list](http://github.com/RamonHossein/validate_cnpj/contributors).
55
+
56
+ ## Contributing
57
+
58
+ 1. Fork it
59
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
60
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
61
+ 4. Push to the branch (`git push origin my-new-feature`)
62
+ 5. Create new Pull Request
63
+
64
+ ## License
65
+
66
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "validate_cnpj"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,7 @@
1
+ require "active_model"
2
+ require "validate_cnpj/cnpj_validator"
3
+ require "validate_cnpj/version"
4
+
5
+ module ValidateCnpj
6
+ autoload :Cnpj, "validate_cnpj/cnpj"
7
+ end
@@ -0,0 +1,76 @@
1
+ module ValidateCnpj
2
+ class Cnpj
3
+ def initialize(document)
4
+ @validate = validation(document)
5
+ end
6
+
7
+ def validation(document)
8
+ return true if document.blank?
9
+ return false if regex(document) == false
10
+ return false if verify_blacklist(document) == true
11
+ return validate?
12
+ end
13
+
14
+ def valid?
15
+ @validate
16
+ end
17
+
18
+ private
19
+
20
+ def regex(document)
21
+ doc = document =~ /^(\d{2}\.?\d{3}\.?\d{3}\/?\d{4})-?(\d{2})$/
22
+ @number = $1
23
+ @result = $2
24
+ doc == 0 ? true : false
25
+ end
26
+
27
+ def validate?
28
+ @number = @number.gsub(/[\/.-]/, "")
29
+ @result == (first_verification + second_verification) ? true : false
30
+ end
31
+
32
+ def first_verification
33
+ sum = multiply_sum([5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2], @number)
34
+ verification(sum%11).to_s
35
+ end
36
+
37
+ def second_verification
38
+ sum = multiply_sum([6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2], @number + first_verification)
39
+ verification(sum%11).to_s
40
+ end
41
+
42
+ def multiply_sum(array, number)
43
+ multiply = []
44
+ number.scan(/\d{1}/).each_with_index { |e, i| multiply[i] = e.to_i * array[i] }
45
+ multiply.inject { |s, e| s + e }
46
+ end
47
+
48
+ def verification(rest)
49
+ rest < 2 ? 0 : (11 - rest)
50
+ end
51
+
52
+ def verify_blacklist(document)
53
+ cnpj = formal_cnpj(document)
54
+ BLACKLIST.include?(cnpj)
55
+ end
56
+
57
+ def formal_cnpj(document)
58
+ document = document.gsub(/[\/.-]/, "")
59
+ document =~ /(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})/
60
+ return "#{$1}.#{$2}.#{$3}/#{$4}-#{$5}"
61
+ end
62
+
63
+ BLACKLIST= [
64
+ "00.000.000/0000-00",
65
+ "11.111.111/1111-11",
66
+ "22.222.222/2222-22",
67
+ "33.333.333/3333-33",
68
+ "44.444.444/4444-44",
69
+ "55.555.555/5555-55",
70
+ "66.666.666/6666-66",
71
+ "77.777.777/7777-77",
72
+ "88.888.888/8888-88",
73
+ "99.999.999/9999-99"
74
+ ]
75
+ end
76
+ end
@@ -0,0 +1,7 @@
1
+ class CnpjValidator < ActiveModel::EachValidator
2
+ def validate_each(record, attribute, value)
3
+ unless ValidateCnpj::Cnpj.new(value).valid?
4
+ record.errors[attribute] << (options[:message] || I18n.t("errors.messages.this_document_is_invalid"))
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module ValidateCnpj
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'validate_cnpj/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "validate_cnpj"
8
+ spec.version = ValidateCnpj::VERSION
9
+ spec.authors = ["RamonHossein"]
10
+ spec.email = ["hosseinramon@gmail.com"]
11
+
12
+ spec.summary = %q{Validates Cnpj}
13
+ spec.homepage = "https://github.com/RamonHossein/validate_cnpj"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.12"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.0"
24
+ spec.add_development_dependency "coveralls"
25
+ spec.add_development_dependency "activemodel"
26
+ spec.add_development_dependency "pry"
27
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: validate_cnpj
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - RamonHossein
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-08-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: coveralls
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: activemodel
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description:
98
+ email:
99
+ - hosseinramon@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - CODE_OF_CONDUCT.md
108
+ - Gemfile
109
+ - LEIAME.md
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - bin/console
114
+ - bin/setup
115
+ - lib/validate_cnpj.rb
116
+ - lib/validate_cnpj/cnpj.rb
117
+ - lib/validate_cnpj/cnpj_validator.rb
118
+ - lib/validate_cnpj/version.rb
119
+ - validate_cnpj.gemspec
120
+ homepage: https://github.com/RamonHossein/validate_cnpj
121
+ licenses:
122
+ - MIT
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 2.5.1
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: Validates Cnpj
144
+ test_files: []