br_documents 0.0.12 → 0.0.13

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: 09c9aff76a8e9f8c1174cd31046388cca186a799
4
- data.tar.gz: b585193cc05100900597b13177920012f0c809b5
3
+ metadata.gz: f64e2139b8c0f45f67ba0d9a3faaa849cf234f2d
4
+ data.tar.gz: a334fc18af1193c28d8faff57cc2f75c0e26c5f2
5
5
  SHA512:
6
- metadata.gz: c8e5f19464173dc1dffceedf46bc56fa08df5a9b245d2af487183e0e6b09afa0669760c0a22316b29bec8a44110ab527f7beaf03fb50e98162970c36183df638
7
- data.tar.gz: 16f4ed6b454c4da58866dbb976309754c2b512427238df3b7c8d55344d837ab50a5e40a2860ea41c9f0fcf3c1adaa4241d1b3f2cfd6f0d8043cda1dcbea97777
6
+ metadata.gz: 6b7e01d7e6ac58d51dde5d7ad3a117bff65e6b2957b58c96af99b61c0c298076243681a7f9ae044ec46803830c9ef41b8c94fce81f1653265752f0a99156fc5c
7
+ data.tar.gz: 74e23ac1af71312b733e33b9793072a0c98f3b9af1e5b86e1740bcd152ef55512662ca6c75aa847d05290a16f0f2d5f332dd7cd724ec108f07bab6de345a9505
data/.travis.yml CHANGED
@@ -1,8 +1,6 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 1.9.3
4
- - 2.0.0
5
- - 2.1.1
6
- - jruby-19mode
3
+ - 2.2.2
4
+ - 2.2.5
7
5
  gemfile:
8
6
  - Gemfile
data/circle.yml CHANGED
@@ -1,3 +1,3 @@
1
1
  machine:
2
2
  ruby:
3
- version: 2.1.2
3
+ version: 2.2.5
@@ -5,19 +5,17 @@ module BrDocuments
5
5
  class Cnpj
6
6
  include Document
7
7
 
8
- def initialize(number)
9
- @number = number
10
- end
8
+ private
11
9
 
12
- def valid?
13
- regex = /^(\d{2}\.\d{3}\.\d{3}\/\d{4}-\d{2})$|^(\d{14})$/
14
- weight13 = [5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]
15
- weight14 = [6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]
10
+ def format_regex
11
+ /^(\d{2}\.\d{3}\.\d{3}\/\d{4}-\d{2})$|^(\d{14})$/
12
+ end
16
13
 
17
- valid_number?(@number, regex, weight13, weight14)
14
+ def weights_for_digital_check
15
+ [[5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2], [6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]]
18
16
  end
19
17
 
20
- def formatted
18
+ def format_number
21
19
  @number.sub(/(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})/, "\\1.\\2.\\3/\\4-\\5")
22
20
  end
23
21
  end
@@ -5,19 +5,17 @@ module BrDocuments
5
5
  class Cpf
6
6
  include Document
7
7
 
8
- def initialize(number)
9
- @number = number
10
- end
8
+ private
11
9
 
12
- def valid?
13
- regex = /^(\d{3}\.\d{3}\.\d{3}-\d{2})$|^(\d{11})$/
14
- weight10 = [10, 9, 8, 7, 6, 5, 4, 3, 2]
15
- weight11 = [11, 10, 9, 8, 7, 6, 5, 4, 3, 2]
10
+ def format_regex
11
+ /^(\d{3}\.\d{3}\.\d{3}-\d{2})$|^(\d{11})$/
12
+ end
16
13
 
17
- valid_number?(@number, regex, weight10, weight11)
14
+ def weights_for_digital_check
15
+ [[10, 9, 8, 7, 6, 5, 4, 3, 2], [11, 10, 9, 8, 7, 6, 5, 4, 3, 2]]
18
16
  end
19
17
 
20
- def formatted
18
+ def format_number
21
19
  @number.sub(/(\d{3})(\d{3})(\d{3})(\d{2})/, "\\1.\\2.\\3-\\4")
22
20
  end
23
21
  end
@@ -5,30 +5,38 @@ module BrDocuments
5
5
  module Document
6
6
  include Commons::Mod11
7
7
 
8
- protected
9
- def valid_number?(number, regex, weight1, weight2)
10
- valid_format?(number, regex) and
11
- not sequence_of_equal_numbers?(number) and
12
- valid_digital_check?(number, weight1, weight2)
8
+ def initialize(number)
9
+ @number = number
10
+ end
11
+
12
+ def valid?
13
+ valid_format? && !sequence_of_equal_numbers? && valid_digital_check?
14
+ end
15
+
16
+ def formatted
17
+ valid? ? format_number : ""
13
18
  end
14
19
 
15
20
  private
16
- def valid_format?(number, regex)
17
- regex.match(number).present?
21
+
22
+ def valid_format?
23
+ format_regex.match(@number).present?
18
24
  end
19
25
 
20
- def sequence_of_equal_numbers?(number)
21
- remove_mask(number).split("").uniq.length == 1
26
+ def sequence_of_equal_numbers?
27
+ number_without_mask.split("").uniq.length == 1
22
28
  end
23
29
 
24
- def valid_digital_check?(number, weight1, weight2)
25
- digital_check1 = generate_digital_check(remove_mask(number), weight1)
26
- digital_check2 = generate_digital_check(remove_mask(number), weight2)
27
- number[-2, 2].eql? "#{digital_check1}#{digital_check2}"
30
+ def valid_digital_check?
31
+ weight1, weight2 = weights_for_digital_check
32
+
33
+ digital_check1 = generate_digital_check(number_without_mask, weight1)
34
+ digital_check2 = generate_digital_check(number_without_mask, weight2)
35
+ @number[-2, 2].eql? "#{digital_check1}#{digital_check2}"
28
36
  end
29
37
 
30
- def remove_mask(number)
31
- number.gsub(/[\.\/-]/, "")
38
+ def number_without_mask
39
+ @number.gsub(/[\.\/-]/, "")
32
40
  end
33
41
  end
34
42
  end
@@ -1,3 +1,3 @@
1
1
  module BrDocuments
2
- VERSION = "0.0.12"
2
+ VERSION = "0.0.13"
3
3
  end
@@ -6,6 +6,13 @@ describe BrDocuments::CnpjCpf::Cnpj do
6
6
  cnpj = BrDocuments::CnpjCpf::Cnpj.new("04001155000101")
7
7
  expect(cnpj.formatted).to eq "04.001.155/0001-01"
8
8
  end
9
+
10
+ context "when is invalid" do
11
+ it "returns an empty string" do
12
+ cnpj = BrDocuments::CnpjCpf::Cnpj.new("123")
13
+ expect(cnpj.formatted).to eq ""
14
+ end
15
+ end
9
16
  end
10
17
 
11
18
  it "is invalid with malformed number" do
@@ -6,6 +6,13 @@ describe BrDocuments::CnpjCpf::Cpf do
6
6
  cpf = BrDocuments::CnpjCpf::Cpf.new("11144477735")
7
7
  expect(cpf.formatted).to eq "111.444.777-35"
8
8
  end
9
+
10
+ context "when is invalid" do
11
+ it "returns an empty string" do
12
+ cpf = BrDocuments::CnpjCpf::Cpf.new("123")
13
+ expect(cpf.formatted).to eq ""
14
+ end
15
+ end
9
16
  end
10
17
 
11
18
  it "is invalid with malformed number" do
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,4 @@
1
1
  require "simplecov"
2
- require "codeclimate-test-reporter"
3
- CodeClimate::TestReporter.start
4
2
  SimpleCov.start
5
3
 
6
4
  require "bundler/setup"
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.12
4
+ version: 0.0.13
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: 2015-09-30 00:00:00.000000000 Z
11
+ date: 2017-03-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -235,7 +235,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
235
235
  version: '0'
236
236
  requirements: []
237
237
  rubyforge_project:
238
- rubygems_version: 2.4.5
238
+ rubygems_version: 2.4.8
239
239
  signing_key:
240
240
  specification_version: 4
241
241
  summary: Validates brazilian documents like CPF, CNPJ and IE. It can be used with