br_documents 0.0.15 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 85b3dba0a5cb19581015f983a1ec5f186521756d
4
- data.tar.gz: f4166ebcf7711bc8849438bd317649ce0c02a5a4
3
+ metadata.gz: ac405ceecbb3815bf47f64f9e72a5c879668e810
4
+ data.tar.gz: dba3e8eed8d0799421ba07870157cdf39dabfe74
5
5
  SHA512:
6
- metadata.gz: 74be958176462d91c7eeb8779b0976191dd34fdf361b086717806d1462ffcb9d89b08c63fc2efb20513986e70ac8056e9876531d16c391793dabcc340b2109d2
7
- data.tar.gz: 69fc04d5fa9cc32d5b52544b094281fa923cec602c11a92f7ea58752e9d4f0400e2b08a2141104e8b356e49e3d2d0b969b05b117337a917d4e1e46ce787aff0f
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
@@ -4,7 +4,7 @@
4
4
  .config
5
5
  .yardoc
6
6
  .idea/
7
- Gemfile.lock
7
+ .byebug_history
8
8
  InstalledFiles
9
9
  _yardoc
10
10
  coverage
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
- gem owner --add leandrolourenci@gmail.com br_documents
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", "4.0.1" if RUBY_VERSION >= "2.0"
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
@@ -0,0 +1,8 @@
1
+ class GtinValidator < ActiveModel::EachValidator
2
+ def validate_each(record, attribute, value)
3
+ if value.present?
4
+ gtin = BrDocuments::Gtin.new(value)
5
+ record.errors.add(attribute, :invalid) if not gtin.valid?
6
+ end
7
+ end
8
+ end
@@ -1,3 +1,3 @@
1
1
  module BrDocuments
2
- VERSION = "0.0.15"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -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.15
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: 2018-02-15 00:00:00.000000000 Z
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: 4.0.1
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: 4.0.1
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.13
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
@@ -1,6 +0,0 @@
1
- language: ruby
2
- rvm:
3
- - 2.2.2
4
- - 2.2.5
5
- gemfile:
6
- - Gemfile
data/circle.yml DELETED
@@ -1,3 +0,0 @@
1
- machine:
2
- ruby:
3
- version: 2.2.5