br_documents 0.0.15 → 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 +4 -4
- data/.circleci/config.yml +42 -0
- data/.gitignore +1 -1
- data/Gemfile.lock +64 -0
- data/README.md +25 -1
- data/br_documents.gemspec +1 -1
- data/lib/br_documents/gtin/gtin.rb +28 -0
- data/lib/br_documents/gtin_validator.rb +8 -0
- data/lib/br_documents/version.rb +1 -1
- data/spec/gtin/gtin_spec.rb +43 -0
- data/spec/gtin_validator_spec.rb +47 -0
- metadata +13 -7
- data/.travis.yml +0 -6
- data/circle.yml +0 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: ac405ceecbb3815bf47f64f9e72a5c879668e810
|
|
4
|
+
data.tar.gz: dba3e8eed8d0799421ba07870157cdf39dabfe74
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 1c31e91f3559a125a625df02fb4b2f7ae81bd404c5fb565c5d50dc588efc30aaefb08a9756fd424ebb79f691b3a397f75705a7b60b51d483fc0a49cf12961770
|
|
7
|
+
data.tar.gz: 16504c4a406c49fc0eced2158fd5e9c6a43084428eeab94e3cc93ecfb8e9099fbc1f3773dc34d91c286c9da4a62345ac1389f7c236840d86e945eb8ef3bfeb07
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
version: 2
|
|
2
|
+
jobs:
|
|
3
|
+
build:
|
|
4
|
+
docker:
|
|
5
|
+
- image: circleci/ruby:2.5.1
|
|
6
|
+
steps:
|
|
7
|
+
- checkout
|
|
8
|
+
- restore_cache:
|
|
9
|
+
key: br-documents-cache-v1-{{ checksum "Gemfile.lock" }}
|
|
10
|
+
- run: bundle check || bundle install
|
|
11
|
+
- save_cache:
|
|
12
|
+
key: br-documents-cache-v1-{{ .Environment.CIRCLE_SHA1 }}
|
|
13
|
+
paths:
|
|
14
|
+
- .
|
|
15
|
+
- save_cache:
|
|
16
|
+
key: br-documents-cache-v1-{{ checksum "Gemfile.lock" }}
|
|
17
|
+
paths:
|
|
18
|
+
- ./vendor/bundle
|
|
19
|
+
rspec:
|
|
20
|
+
docker:
|
|
21
|
+
- image: circleci/ruby:2.5.1
|
|
22
|
+
steps:
|
|
23
|
+
- restore_cache:
|
|
24
|
+
key: br-documents-cache-v1-{{ .Environment.CIRCLE_SHA1 }}
|
|
25
|
+
- restore_cache:
|
|
26
|
+
key: br-documents-cache-v1-{{ checksum "Gemfile.lock" }}
|
|
27
|
+
- run:
|
|
28
|
+
name: Install Test Coverage for CodeClimate
|
|
29
|
+
command: |
|
|
30
|
+
curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
|
|
31
|
+
chmod +x ./cc-test-reporter
|
|
32
|
+
- run: ./cc-test-reporter before-build
|
|
33
|
+
- run: bundle exec rspec
|
|
34
|
+
- run: ./cc-test-reporter after-build --exit-code $?
|
|
35
|
+
workflows:
|
|
36
|
+
version: 2
|
|
37
|
+
main:
|
|
38
|
+
jobs:
|
|
39
|
+
- build
|
|
40
|
+
- rspec:
|
|
41
|
+
requires:
|
|
42
|
+
- build
|
data/.gitignore
CHANGED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
br_documents (0.1.0)
|
|
5
|
+
activemodel (>= 4.0.0)
|
|
6
|
+
i18n (>= 0.6.5)
|
|
7
|
+
|
|
8
|
+
GEM
|
|
9
|
+
remote: https://rubygems.org/
|
|
10
|
+
specs:
|
|
11
|
+
activemodel (5.2.3)
|
|
12
|
+
activesupport (= 5.2.3)
|
|
13
|
+
activesupport (5.2.3)
|
|
14
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
|
15
|
+
i18n (>= 0.7, < 2)
|
|
16
|
+
minitest (~> 5.1)
|
|
17
|
+
tzinfo (~> 1.1)
|
|
18
|
+
byebug (10.0.2)
|
|
19
|
+
codeclimate-test-reporter (1.0.7)
|
|
20
|
+
simplecov
|
|
21
|
+
concurrent-ruby (1.1.5)
|
|
22
|
+
diff-lcs (1.3)
|
|
23
|
+
docile (1.1.5)
|
|
24
|
+
i18n (1.6.0)
|
|
25
|
+
concurrent-ruby (~> 1.0)
|
|
26
|
+
json (2.0.3)
|
|
27
|
+
minitest (5.11.3)
|
|
28
|
+
rake (12.0.0)
|
|
29
|
+
rspec (3.5.0)
|
|
30
|
+
rspec-core (~> 3.5.0)
|
|
31
|
+
rspec-expectations (~> 3.5.0)
|
|
32
|
+
rspec-mocks (~> 3.5.0)
|
|
33
|
+
rspec-core (3.5.4)
|
|
34
|
+
rspec-support (~> 3.5.0)
|
|
35
|
+
rspec-expectations (3.5.0)
|
|
36
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
37
|
+
rspec-support (~> 3.5.0)
|
|
38
|
+
rspec-mocks (3.5.0)
|
|
39
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
40
|
+
rspec-support (~> 3.5.0)
|
|
41
|
+
rspec-support (3.5.0)
|
|
42
|
+
simplecov (0.14.0)
|
|
43
|
+
docile (~> 1.1.0)
|
|
44
|
+
json (>= 1.8, < 3)
|
|
45
|
+
simplecov-html (~> 0.10.0)
|
|
46
|
+
simplecov-html (0.10.0)
|
|
47
|
+
thread_safe (0.3.6)
|
|
48
|
+
tzinfo (1.2.5)
|
|
49
|
+
thread_safe (~> 0.1)
|
|
50
|
+
|
|
51
|
+
PLATFORMS
|
|
52
|
+
ruby
|
|
53
|
+
|
|
54
|
+
DEPENDENCIES
|
|
55
|
+
br_documents!
|
|
56
|
+
bundler (>= 1.3)
|
|
57
|
+
byebug (= 10.0.2)
|
|
58
|
+
codeclimate-test-reporter
|
|
59
|
+
rake (>= 10.1.0)
|
|
60
|
+
rspec (>= 3.0.0)
|
|
61
|
+
simplecov (>= 0.9.0)
|
|
62
|
+
|
|
63
|
+
BUNDLED WITH
|
|
64
|
+
1.16.4
|
data/README.md
CHANGED
|
@@ -147,9 +147,33 @@ ie.formatted # => "254.062.407"
|
|
|
147
147
|
ie.valid? # => true
|
|
148
148
|
```
|
|
149
149
|
|
|
150
|
+
## Environment variables
|
|
151
|
+
|
|
152
|
+
The following variables are necessary to be set:
|
|
153
|
+
|
|
154
|
+
Environment var | Development? | Test? | CI | Production? | Data
|
|
155
|
+
-------------------------------|--------------|-------|-----|-------------|-----
|
|
156
|
+
TZ | no | no | yes | no | America/Sao_Paulo
|
|
157
|
+
BUNDLE_PATH | no | no | yes | no | ./vendor/bundle
|
|
158
|
+
CC_TEST_REPORTER_ID | no | no | yes | no | get at codeclimate
|
|
159
|
+
RAILS_ENV | no | no | yes | no | test
|
|
160
|
+
|
|
161
|
+
## Deploy to Rubygems
|
|
162
|
+
|
|
163
|
+
$ bundle exec rake release # Create tag vX.X.X, build and push br_documents-X.X.X.gem to Rubygems
|
|
164
|
+
|
|
165
|
+
You can build or install using one of the above tasks
|
|
166
|
+
|
|
167
|
+
$ bundle exec rake build # Build br_documents-X.X.X.gem into the pkg directory
|
|
168
|
+
$ bundle exec rake install # Build and install br_documents-X.X.X.gem into system gems
|
|
169
|
+
|
|
170
|
+
#### Login
|
|
171
|
+
$ gem signin
|
|
172
|
+
$ gem signout
|
|
173
|
+
|
|
150
174
|
## How to add contributors
|
|
151
175
|
|
|
152
|
-
|
|
176
|
+
gem owner --add leandrolourenci@gmail.com br_documents
|
|
153
177
|
|
|
154
178
|
## Contributing
|
|
155
179
|
|
data/br_documents.gemspec
CHANGED
|
@@ -26,5 +26,5 @@ Gem::Specification.new do |spec|
|
|
|
26
26
|
spec.add_development_dependency "rspec", ">= 3.0.0"
|
|
27
27
|
spec.add_development_dependency "codeclimate-test-reporter"
|
|
28
28
|
spec.add_development_dependency "simplecov", ">= 0.9.0"
|
|
29
|
-
spec.add_development_dependency "byebug", "
|
|
29
|
+
spec.add_development_dependency "byebug", "10.0.2" if RUBY_VERSION >= "2.0"
|
|
30
30
|
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
module BrDocuments
|
|
2
|
+
class Gtin
|
|
3
|
+
def initialize(code)
|
|
4
|
+
@code = code
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
def valid?
|
|
8
|
+
valid_length? && valid_checksum?
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
private
|
|
12
|
+
|
|
13
|
+
def valid_length?
|
|
14
|
+
[8, 12, 13, 14].include?(@code.length)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def valid_checksum?
|
|
18
|
+
numbers = @code.chars[0, @code.length - 1].reverse
|
|
19
|
+
odd = even = 0
|
|
20
|
+
|
|
21
|
+
numbers.each_with_index do |number, i|
|
|
22
|
+
(i + 1).even? ? (even += number.to_i) : (odd += number.to_i * 3)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
@code.chars.last.to_i == (10 - (odd + even) % 10)
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
data/lib/br_documents/version.rb
CHANGED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
require "spec_helper"
|
|
2
|
+
|
|
3
|
+
describe BrDocuments::Gtin do
|
|
4
|
+
describe "#valid?" do
|
|
5
|
+
context 'when the checksum is valid' do
|
|
6
|
+
context 'when the GTIN has 8 digits' do
|
|
7
|
+
subject { described_class.new('12345618') }
|
|
8
|
+
|
|
9
|
+
it { is_expected.to be_valid }
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
context 'when the GTIN has 12 digits' do
|
|
13
|
+
subject { described_class.new('114567854874') }
|
|
14
|
+
|
|
15
|
+
it { is_expected.to be_valid }
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
context 'when the GTIN has 13 digits' do
|
|
19
|
+
subject { described_class.new('1145678548719') }
|
|
20
|
+
|
|
21
|
+
it { is_expected.to be_valid }
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
context 'when the GTIN has 14 digits' do
|
|
25
|
+
subject { described_class.new('1234567891125') }
|
|
26
|
+
|
|
27
|
+
it { is_expected.to be_valid }
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
context 'when the number of digits is different of 8, 13 or 14' do
|
|
31
|
+
subject { described_class.new('12345678901234560') }
|
|
32
|
+
|
|
33
|
+
it { is_expected.to_not be_valid }
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
context 'when the checksum is invald' do
|
|
38
|
+
subject { described_class.new('1145678548712') }
|
|
39
|
+
|
|
40
|
+
it { is_expected.to_not be_valid }
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
require "spec_helper"
|
|
2
|
+
|
|
3
|
+
describe GtinValidator do
|
|
4
|
+
let(:record) { double("model") }
|
|
5
|
+
|
|
6
|
+
before do
|
|
7
|
+
allow(record).to receive(:errors).and_return([])
|
|
8
|
+
allow(record.errors).to receive(:messages).and_return({})
|
|
9
|
+
allow(record.errors).to receive(:add) do | attribute, error |
|
|
10
|
+
record.errors.messages[attribute] = [error]
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
subject { GtinValidator.new(attributes: "gtin") }
|
|
15
|
+
|
|
16
|
+
context "when GTIN code is valid" do
|
|
17
|
+
before { subject.validate_each(record, "gtin", "1243658721548") }
|
|
18
|
+
|
|
19
|
+
it "doesn't add errors in model" do
|
|
20
|
+
expect(record.errors.messages).to be_empty
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
context "when GTIN is blank" do
|
|
25
|
+
before { subject.validate_each(record, "gtin", "") }
|
|
26
|
+
|
|
27
|
+
it "doesn't add errors in model" do
|
|
28
|
+
expect(record.errors.messages).to be_empty
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
context "when GTIN is nil" do
|
|
33
|
+
before { subject.validate_each(record, "gtin", nil) }
|
|
34
|
+
|
|
35
|
+
it "doesn't add errors in model" do
|
|
36
|
+
expect(record.errors.messages).to be_empty
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
context "when GTIN is invalid" do
|
|
41
|
+
before { subject.validate_each(record, "gtin", "253667853") }
|
|
42
|
+
|
|
43
|
+
it "adds errors in model" do
|
|
44
|
+
expect(record.errors.messages).to include 'gtin' => [:invalid]
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: br_documents
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0
|
|
4
|
+
version: 0.1.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- ASSEINFO - Sistemas de Informação
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2019-05-06 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: activemodel
|
|
@@ -114,14 +114,14 @@ dependencies:
|
|
|
114
114
|
requirements:
|
|
115
115
|
- - '='
|
|
116
116
|
- !ruby/object:Gem::Version
|
|
117
|
-
version:
|
|
117
|
+
version: 10.0.2
|
|
118
118
|
type: :development
|
|
119
119
|
prerelease: false
|
|
120
120
|
version_requirements: !ruby/object:Gem::Requirement
|
|
121
121
|
requirements:
|
|
122
122
|
- - '='
|
|
123
123
|
- !ruby/object:Gem::Version
|
|
124
|
-
version:
|
|
124
|
+
version: 10.0.2
|
|
125
125
|
description: Validates brazilian documents like CPF, CNPJ and IE.
|
|
126
126
|
email:
|
|
127
127
|
- asseinfo@asseinfo.com.br
|
|
@@ -129,14 +129,14 @@ executables: []
|
|
|
129
129
|
extensions: []
|
|
130
130
|
extra_rdoc_files: []
|
|
131
131
|
files:
|
|
132
|
+
- ".circleci/config.yml"
|
|
132
133
|
- ".gitignore"
|
|
133
134
|
- ".rspec"
|
|
134
|
-
- ".travis.yml"
|
|
135
135
|
- Gemfile
|
|
136
|
+
- Gemfile.lock
|
|
136
137
|
- README.md
|
|
137
138
|
- Rakefile
|
|
138
139
|
- br_documents.gemspec
|
|
139
|
-
- circle.yml
|
|
140
140
|
- config/locales/pt-BR.yml
|
|
141
141
|
- lib/br_documents.rb
|
|
142
142
|
- lib/br_documents/cnpj_cpf/cnpj.rb
|
|
@@ -145,6 +145,8 @@ files:
|
|
|
145
145
|
- lib/br_documents/cnpj_validator.rb
|
|
146
146
|
- lib/br_documents/commons/mod11.rb
|
|
147
147
|
- lib/br_documents/cpf_validator.rb
|
|
148
|
+
- lib/br_documents/gtin/gtin.rb
|
|
149
|
+
- lib/br_documents/gtin_validator.rb
|
|
148
150
|
- lib/br_documents/ie/ac.rb
|
|
149
151
|
- lib/br_documents/ie/al.rb
|
|
150
152
|
- lib/br_documents/ie/am.rb
|
|
@@ -182,6 +184,8 @@ files:
|
|
|
182
184
|
- spec/cnpj_cpf/cpf_spec.rb
|
|
183
185
|
- spec/cnpj_validator_spec.rb
|
|
184
186
|
- spec/cpf_validator_spec.rb
|
|
187
|
+
- spec/gtin/gtin_spec.rb
|
|
188
|
+
- spec/gtin_validator_spec.rb
|
|
185
189
|
- spec/ie/ac_spec.rb
|
|
186
190
|
- spec/ie/al_spec.rb
|
|
187
191
|
- spec/ie/am_spec.rb
|
|
@@ -235,7 +239,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
235
239
|
version: '0'
|
|
236
240
|
requirements: []
|
|
237
241
|
rubyforge_project:
|
|
238
|
-
rubygems_version: 2.6.
|
|
242
|
+
rubygems_version: 2.6.14.1
|
|
239
243
|
signing_key:
|
|
240
244
|
specification_version: 4
|
|
241
245
|
summary: Validates brazilian documents like CPF, CNPJ and IE. It can be used with
|
|
@@ -245,6 +249,8 @@ test_files:
|
|
|
245
249
|
- spec/cnpj_cpf/cpf_spec.rb
|
|
246
250
|
- spec/cnpj_validator_spec.rb
|
|
247
251
|
- spec/cpf_validator_spec.rb
|
|
252
|
+
- spec/gtin/gtin_spec.rb
|
|
253
|
+
- spec/gtin_validator_spec.rb
|
|
248
254
|
- spec/ie/ac_spec.rb
|
|
249
255
|
- spec/ie/al_spec.rb
|
|
250
256
|
- spec/ie/am_spec.rb
|
data/.travis.yml
DELETED
data/circle.yml
DELETED