bra_document_validation 1.0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 27d8c78d6db49bc48c0e1dfd5ed47ed8dc9ae540e2b8972c28ef1a19d763b8bc
4
+ data.tar.gz: 2a2694b688925f879cf5d0fd2d859e107eab8305c33ea87f402674c7f469540f
5
+ SHA512:
6
+ metadata.gz: 3179114b07dbd55af289d116e5d2c8dfb860aa67c7b6b7523d5051a79a17b1bc3e8e3d91a38d33b5fc54e544ad3df711b30dd0cecb20f2a4182a9b902dca2c3c
7
+ data.tar.gz: 047cede5bb6692ac13edd7fc06aca61382954df487d1708103de5f25ff96b4d785e28871449ac788b80a1aa86ede1b171d9a09d5a8299bca84a8205c6fd4dbb1
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.2
6
+ before_install: gem install bundler -v 2.1.4
@@ -0,0 +1,12 @@
1
+
2
+ # Changelog
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
6
+ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [Unreleased]
9
+
10
+ ## [1.0.0] - 2020-11-08
11
+ ### Added
12
+ - CPF and CNPJ Validators
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in bra_document_validation.gemspec
6
+ gemspec
@@ -0,0 +1,62 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ bra_document_validation (0.1.0)
5
+ activemodel (>= 4.0)
6
+ bra_documents
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activemodel (6.0.3.4)
12
+ activesupport (= 6.0.3.4)
13
+ activesupport (6.0.3.4)
14
+ concurrent-ruby (~> 1.0, >= 1.0.2)
15
+ i18n (>= 0.7, < 2)
16
+ minitest (~> 5.1)
17
+ tzinfo (~> 1.1)
18
+ zeitwerk (~> 2.2, >= 2.2.2)
19
+ bra_documents (1.0.0)
20
+ coderay (1.1.3)
21
+ concurrent-ruby (1.1.7)
22
+ diff-lcs (1.4.4)
23
+ i18n (1.8.5)
24
+ concurrent-ruby (~> 1.0)
25
+ method_source (0.9.2)
26
+ minitest (5.14.2)
27
+ pry (0.12.2)
28
+ coderay (~> 1.1.0)
29
+ method_source (~> 0.9.0)
30
+ pry-nav (0.3.0)
31
+ pry (>= 0.9.10, < 0.13.0)
32
+ rake (13.0.1)
33
+ rspec (3.9.0)
34
+ rspec-core (~> 3.9.0)
35
+ rspec-expectations (~> 3.9.0)
36
+ rspec-mocks (~> 3.9.0)
37
+ rspec-core (3.9.3)
38
+ rspec-support (~> 3.9.3)
39
+ rspec-expectations (3.9.2)
40
+ diff-lcs (>= 1.2.0, < 2.0)
41
+ rspec-support (~> 3.9.0)
42
+ rspec-mocks (3.9.1)
43
+ diff-lcs (>= 1.2.0, < 2.0)
44
+ rspec-support (~> 3.9.0)
45
+ rspec-support (3.9.3)
46
+ thread_safe (0.3.6)
47
+ tzinfo (1.2.7)
48
+ thread_safe (~> 0.1)
49
+ zeitwerk (2.4.0)
50
+
51
+ PLATFORMS
52
+ ruby
53
+
54
+ DEPENDENCIES
55
+ bra_document_validation!
56
+ pry
57
+ pry-nav
58
+ rake
59
+ rspec
60
+
61
+ BUNDLED WITH
62
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Bruno Vicenzo
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.
@@ -0,0 +1,62 @@
1
+ # BraDocumentValidation
2
+
3
+ This gem allows to validate Brazilian documents, CPF and CNPJ, with format and digit verification.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'bra_document_validation'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install bra_document_validation
20
+
21
+ ## Usage
22
+
23
+ ```rb
24
+ validates :document_number, 'bra_document_validation/cpf': true
25
+ validates :document_number, 'bra_document_validation/cnpj': true
26
+ ```
27
+
28
+ # Format
29
+
30
+ By default, it expects a raw document number (only numbers).
31
+ However you can pass options if your number is formatted.
32
+
33
+ ```rb
34
+ validates :document_number, 'bra_document_validation/cpf': { formatted: true }
35
+ validates :document_number, 'bra_document_validation/cnpj': { formatted: false }
36
+ ```
37
+
38
+ # Messaging
39
+
40
+ When the document format does not match `invalid_format` message is added to the errors on field.
41
+ When the document verification digit is invalid `invalid_verification_digit`.
42
+ However you can add a custom message that substitute the above messages.
43
+
44
+ ```rb
45
+ validates :document_number, 'bra_document_validation/cpf': { message: :invalid }
46
+ validates :document_number, 'bra_document_validation/cnpj': { message: 'A custom message' }
47
+ ```
48
+
49
+ ## Development
50
+
51
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
52
+
53
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
54
+
55
+ ## Contributing
56
+
57
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/bra_document_validation.
58
+
59
+
60
+ ## License
61
+
62
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'bra_document_validation'
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(__FILE__)
@@ -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,37 @@
1
+ require_relative 'lib/bra_document_validation/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'bra_document_validation'
5
+ spec.version = BraDocumentValidation::VERSION
6
+ spec.authors = ['Bruno Vicenzo']
7
+ spec.email = ['bruno@alumni.usp.br']
8
+
9
+ spec.summary = %q{This gem add ActiveModel validations to Brazilian documents.}
10
+ spec.description = %q{We can validate CPF and CNPJ formats and verifying digits.}
11
+ spec.homepage = 'https://github.com/bvicenzo/bra_document_validation'
12
+ spec.license = 'MIT'
13
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
14
+
15
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org/'
16
+
17
+ spec.metadata['homepage_uri'] = spec.homepage
18
+ spec.metadata['source_code_uri'] = spec.homepage
19
+ spec.metadata['changelog_uri'] = 'https://github.com/bvicenzo/bra_document_validation/blob/master/CHANGELOG.md'
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+ spec.bindir = 'exe'
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ['lib']
29
+
30
+ spec.add_runtime_dependency 'activemodel', '>= 4.0'
31
+ spec.add_runtime_dependency 'bra_documents'
32
+
33
+ spec.add_development_dependency 'pry'
34
+ spec.add_development_dependency 'pry-nav'
35
+ spec.add_development_dependency 'rake'
36
+ spec.add_development_dependency 'rspec'
37
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_model'
4
+ require 'bra_documents'
5
+ require 'bra_document_validation/version'
6
+ require 'bra_document_validation/cnpj_validator'
7
+ require 'bra_document_validation/cpf_validator'
8
+
9
+ module BraDocumentValidation
10
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BraDocumentValidation
4
+ class CNPJValidator < ActiveModel::EachValidator
5
+ FORMATTED_CNPJ_PATTERN = /\A(\d{2}\.\d{3}\.\d{3}\/\d{4})-(\d{2})\Z/.freeze
6
+ RAW_CNPJ_PATTERN = /\A\d{14}\Z/.freeze
7
+ NOT_NUMBER_PATTERN = /\D/.freeze
8
+
9
+ def validate_each(record, attribute, value)
10
+ return record.errors.add(attribute, error_message(:invalid_format)) unless document_format.match?(value.to_s)
11
+ full_number = only_numbers_for(value.to_s)
12
+ record.errors.add(attribute, error_message(:invalid_verification_digit)) if black_listed?(full_number) || !digit_verified?(full_number)
13
+ end
14
+
15
+ private
16
+
17
+ def digit_verified?(full_number)
18
+ company_number, matrix_subsidiary_number = numbers_for(full_number)
19
+
20
+ full_number == BraDocuments::CNPJGenerator.generate(company_number: company_number, matrix_subsidiary_number: matrix_subsidiary_number)
21
+ end
22
+
23
+ def numbers_for(value)
24
+ number = value.gsub(NOT_NUMBER_PATTERN, '').chars
25
+
26
+ [
27
+ number.shift(BraDocuments::CNPJGenerator::COMPANY_NUMBER_SIZE).join,
28
+ number.shift(BraDocuments::CNPJGenerator::MATRIX_SUBSIDIARY_SIZE).join
29
+ ]
30
+ end
31
+
32
+ def only_numbers_for(value)
33
+ value.gsub(NOT_NUMBER_PATTERN, '')
34
+ end
35
+
36
+ def black_listed?(number)
37
+ number.chars.uniq.size == 1
38
+ end
39
+
40
+ def document_format
41
+ options[:formatted] ? FORMATTED_CNPJ_PATTERN : RAW_CNPJ_PATTERN
42
+ end
43
+
44
+ def error_message(default_message)
45
+ options.fetch(:message, default_message)
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BraDocumentValidation
4
+ class CPFValidator < ActiveModel::EachValidator
5
+ FORMATTED_CPF_PATTERN = /^(\d{3}\.\d{3}\.\d{3})-(\d{2})$/.freeze
6
+ RAW_CPF_PATTERN = /\A\d{11}\z/
7
+ NOT_NUMBER_PATTERN = /\D/.freeze
8
+
9
+ def validate_each(record, attribute, value)
10
+ return record.errors.add(attribute, error_message(:invalid_format)) unless document_format.match?(value.to_s)
11
+ full_number = only_numbers_for(value.to_s)
12
+ record.errors.add(attribute, error_message(:invalid_verification_digit)) if black_listed?(full_number) || !digit_verified?(full_number)
13
+ end
14
+
15
+ private
16
+
17
+ def digit_verified?(number)
18
+ person_number = number_without_verifying_digits_for(number)
19
+
20
+ number == BraDocuments::CPFGenerator.generate(person_number: person_number)
21
+ end
22
+
23
+ def number_without_verifying_digits_for(number)
24
+ number.chars.shift(BraDocuments::CPFGenerator::PERSON_NUMBER_SIZE).join
25
+ end
26
+
27
+ def only_numbers_for(value)
28
+ value.gsub(NOT_NUMBER_PATTERN, '')
29
+ end
30
+
31
+ def black_listed?(number)
32
+ number.chars.uniq.size == 1
33
+ end
34
+
35
+ def document_format
36
+ options[:formatted] ? FORMATTED_CPF_PATTERN : RAW_CPF_PATTERN
37
+ end
38
+
39
+ def error_message(default_message)
40
+ options.fetch(:message, default_message)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BraDocumentValidation
4
+ VERSION = '1.0.0'
5
+ end
metadata ADDED
@@ -0,0 +1,147 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bra_document_validation
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Bruno Vicenzo
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-11-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activemodel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bra_documents
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry-nav
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: rake
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: rspec
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: We can validate CPF and CNPJ formats and verifying digits.
98
+ email:
99
+ - bruno@alumni.usp.br
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - CHANGELOG.md
108
+ - Gemfile
109
+ - Gemfile.lock
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - bin/console
114
+ - bin/setup
115
+ - bra_document_validation.gemspec
116
+ - lib/bra_document_validation.rb
117
+ - lib/bra_document_validation/cnpj_validator.rb
118
+ - lib/bra_document_validation/cpf_validator.rb
119
+ - lib/bra_document_validation/version.rb
120
+ homepage: https://github.com/bvicenzo/bra_document_validation
121
+ licenses:
122
+ - MIT
123
+ metadata:
124
+ allowed_push_host: https://rubygems.org/
125
+ homepage_uri: https://github.com/bvicenzo/bra_document_validation
126
+ source_code_uri: https://github.com/bvicenzo/bra_document_validation
127
+ changelog_uri: https://github.com/bvicenzo/bra_document_validation/blob/master/CHANGELOG.md
128
+ post_install_message:
129
+ rdoc_options: []
130
+ require_paths:
131
+ - lib
132
+ required_ruby_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: 2.3.0
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ requirements: []
143
+ rubygems_version: 3.1.4
144
+ signing_key:
145
+ specification_version: 4
146
+ summary: This gem add ActiveModel validations to Brazilian documents.
147
+ test_files: []