correios-cep 0.4.0 → 0.5.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: b4205b263bcf608493677f1f0f6ce7a9cbf4fea6
4
- data.tar.gz: b39d686fef4ff806416993478d88885a05fe694a
3
+ metadata.gz: 37ca8dbe26d97bd4264d9c02a66ba229d7014d92
4
+ data.tar.gz: c92f319267e2fb49a431c110753ce6c6206022bf
5
5
  SHA512:
6
- metadata.gz: 102b1121be45c66e6963b59ab0490cefd5a7431c8509f98f9285cfc34cee7db68b49dee7efa15c97dfa4a855dc5919da27c9b4be21f00bc6dccc59f382f18f46
7
- data.tar.gz: c9ea444975185a81518c5a71b5fcfd787d6c6c53d69c13645482e45c34aa2c4eb036d490d9ae940fef9996ff960b26af4276b5439ff34e6024c98de0cbe65812
6
+ metadata.gz: c620b2d30da1e0f7485f68300c1e115cf22bccb19fa917c4af9be36a21bee09a5849bdf93ca02926a7093c8539d2841fc81287eb90c59032375d54d6691a3b4f
7
+ data.tar.gz: 7376ae683db061ed2b5070e056c686f98e191911d6cedb2670e9fa561319a54d5637db32044eb9b47f4a398a45112ddfa4b00c789cfac57cb250e3eda35443fe
@@ -3,6 +3,6 @@ rvm:
3
3
  - 1.9.2
4
4
  - 1.9.3
5
5
  - 2.0.0
6
- - 2.1.5
7
- - 2.2.2
6
+ - 2.1.7
7
+ - 2.2.3
8
8
  script: bundle exec rspec
@@ -1,5 +1,9 @@
1
+ ## Version 0.5.0
2
+ - Fix: Handle errors returned from Correios web service. [Issue #10](https://github.com/prodis/correios-cep/issues/10)
3
+ - Improvent: Zipcode input validation.
4
+
1
5
  ## Version 0.4.0
2
- - Improvement: Replace Nokogiri by Ox. Issue #3.
6
+ - Improvement: Replace Nokogiri by Ox. [Issue #3](https://github.com/prodis/correios-cep/issues/3).
3
7
 
4
8
  ## Version 0.3.3
5
9
  - Fix: Downgrade LogMe gem version to 0.0.8, because 0.0.9 does not support Ruby 1.9.2.
@@ -27,7 +31,7 @@
27
31
  - Improvement: Update LogMe gem to version 0.0.5 with support to log messages label.
28
32
 
29
33
  ## Version 0.1.1
30
- - New: A "get" class method in AddressFinder class.
34
+ - New: A `get` class method in `AddressFinder` class.
31
35
 
32
36
  ## Version 0.1.0
33
37
  - First working version.
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014-2015 Fernando Hamasaki de Amorim
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Current available solutions to find Brazilian addresses by zipcode use an HTML form from Correios web site to perform it, instead of to use a real API.
4
4
 
5
- This works with an HTTP request to the form, followed by parsing the HTML result page. The huge problem here is when the Correios web site development team decides to modify some HTML element in the result page, even a layout update, it will break the parser logic for result.
5
+ The old solution works with an HTTP request to the form, followed by parsing the HTML result page. The huge problem here is when the Correios web site development team decides to modify some HTML element in the result page, even a layout update, it will break the parser logic for result.
6
6
 
7
7
  Correios CEP gem solves this problem, getting data directly from Correios database.
8
8
 
@@ -132,9 +132,16 @@ To disable the log and configure other log output, use **Correios::CEP** module:
132
132
  end
133
133
  ```
134
134
 
135
+ ## Changelog
136
+
137
+ [See the changes in each version.](CHANGELOG.md)
138
+
139
+
135
140
  ## Author
136
141
  - [Fernando Hamasaki de Amorim (prodis)](http://prodis.blog.br)
137
142
 
143
+ ![Prodis Logo](http://prodis.net.br/images/prodis_150.gif)
144
+
138
145
 
139
146
  ## Contributing to correios-cep
140
147
 
@@ -146,34 +153,3 @@ To disable the log and configure other log output, use **Correios::CEP** module:
146
153
  - Don't forget to rebase with branch master in main project before submit the pull request.
147
154
  - Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
148
155
  - Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
149
-
150
-
151
- ## Copyright
152
-
153
- (The MIT License)
154
-
155
- [Prodis a.k.a. Fernando Hamasaki de Amorim](http://prodis.blog.br)
156
-
157
- ![Prodis Logo](http://prodis.net.br/images/prodis_150.gif)
158
-
159
- Copyright © 2014-2015 Prodis
160
-
161
- Permission is hereby granted, free of charge, to any person obtaining
162
- a copy of this software and associated documentation files (the
163
- "Software"), to deal in the Software without restriction, including
164
- without limitation the rights to use, copy, modify, merge, publish,
165
- distribute, sublicense, and/or sell copies of the Software, and to
166
- permit persons to whom the Software is furnished to do so, subject to
167
- the following conditions:
168
-
169
- The above copyright notice and this permission notice shall be
170
- included in all copies or substantial portions of the Software.
171
-
172
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
173
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
174
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
175
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
176
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
177
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
178
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
179
-
@@ -1,32 +1,32 @@
1
- lib = File.expand_path('../lib', __FILE__)
1
+ lib = File.expand_path("../lib", __FILE__)
2
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
- require 'correios/cep/version'
3
+ require "correios/cep/version"
4
4
 
5
5
  Gem::Specification.new do |spec|
6
- spec.name = 'correios-cep'
6
+ spec.name = "correios-cep"
7
7
  spec.version = Correios::CEP::VERSION
8
- spec.authors = ['Prodis a.k.a. Fernando Hamasaki de Amorim']
9
- spec.email = ['prodis@gmail.com']
8
+ spec.authors = ["Prodis a.k.a. Fernando Hamasaki de Amorim"]
9
+ spec.email = ["prodis@gmail.com"]
10
10
  spec.description = %q{Correios CEP gem gets updated Brazilian address from a zipcode, directly from Correios database. No HTML parsers.}
11
11
  spec.summary = spec.description
12
- spec.homepage = 'http://prodis.blog.br/correios-cep-gem-para-consulta-de-enderecos-por-cep'
13
- spec.license = 'MIT'
12
+ spec.homepage = "http://prodis.blog.br/correios-cep-gem-para-consulta-de-enderecos-por-cep"
13
+ spec.license = "MIT"
14
14
 
15
15
  spec.files = `git ls-files`.split($/)
16
16
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
17
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
- spec.require_paths = ['lib']
18
+ spec.require_paths = ["lib"]
19
19
 
20
20
  spec.platform = Gem::Platform::RUBY
21
- spec.required_ruby_version = Gem::Requirement.new('>= 1.9.2')
21
+ spec.required_ruby_version = Gem::Requirement.new(">= 1.9.2")
22
22
 
23
- spec.add_dependency 'log-me', '= 0.0.8'
24
- spec.add_dependency "ox", "~> 2.2"
23
+ spec.add_dependency "log-me", "= 0.0.8"
24
+ spec.add_dependency "ox", "~> 2.2"
25
25
 
26
- spec.add_development_dependency "coveralls"
27
- spec.add_development_dependency "pry"
28
- spec.add_development_dependency "rake"
29
- spec.add_development_dependency "rspec", "~> 3.2"
30
- spec.add_development_dependency "vcr", "~> 2.9"
31
- spec.add_development_dependency "webmock", "~> 1.15"
26
+ spec.add_development_dependency "coveralls", "~> 0.8.3"
27
+ spec.add_development_dependency "pry", "~> 0.10.3"
28
+ spec.add_development_dependency "rake", "~> 10.4", ">= 10.4.2"
29
+ spec.add_development_dependency "rspec", "~> 3.2"
30
+ spec.add_development_dependency "vcr", "~> 2.9"
31
+ spec.add_development_dependency "webmock", "~> 1.15"
32
32
  end
@@ -2,6 +2,7 @@ module Correios
2
2
  module CEP
3
3
  class AddressFinder
4
4
  def get(zipcode)
5
+ validate(zipcode)
5
6
  response = web_service.request(zipcode)
6
7
  parser.address(response)
7
8
  end
@@ -19,6 +20,14 @@ module Correios
19
20
  def parser
20
21
  @parser ||= Correios::CEP::Parser.new
21
22
  end
23
+
24
+ private
25
+
26
+ def validate(zipcode)
27
+ if zipcode.to_s.strip.empty? || !zipcode.to_s.match(/\A\d{5}-?\d{3}\z/)
28
+ raise ArgumentError.new("invalid cep format")
29
+ end
30
+ end
22
31
  end
23
32
  end
24
33
  end
@@ -15,8 +15,9 @@ module Correios
15
15
 
16
16
  def address(xml)
17
17
  doc = Ox.parse(xml)
18
+
18
19
  return_node = find_node(doc.nodes, 'return')
19
- return if return_node.nil?
20
+ return {} if return_node.nil?
20
21
 
21
22
  address = {}
22
23
  return_node.nodes.each do |element|
@@ -30,7 +31,8 @@ module Correios
30
31
  private
31
32
 
32
33
  def find_node(nodes, name)
33
- node = nodes.first
34
+ node = nodes.last
35
+ return nil unless node.is_a?(Ox::Element)
34
36
  return node if node.nil? || node.name == name
35
37
 
36
38
  find_node(node.nodes, name)
@@ -1,5 +1,5 @@
1
1
  module Correios
2
2
  module CEP
3
- VERSION = '0.4.0'
3
+ VERSION = '0.5.0'
4
4
  end
5
5
  end
@@ -1,24 +1,36 @@
1
- require 'spec_helper'
1
+ require "spec_helper"
2
2
 
3
3
  describe Correios::CEP::AddressFinder do
4
- let(:cep) { '54250610' }
5
- let(:web_service_response) { '<end>Rua Fernando Amorim</end>' }
6
- let(:address) { { address: 'Rua Fernando Amorim' } }
4
+ context "with invalid cep" do
5
+ it "should raise ArgumentError when cep is nil" do
6
+ expect{ subject.get(nil) }.to raise_error(ArgumentError)
7
+ end
7
8
 
8
- before do
9
- allow_any_instance_of(Correios::CEP::WebService).to receive(:request).with(cep).and_return(web_service_response)
10
- allow_any_instance_of(Correios::CEP::Parser).to receive(:address).with(web_service_response).and_return(address)
9
+ it "should raise ArgumentError when cep does not have a valid format" do
10
+ expect{ subject.get("542506-10") }.to raise_error(ArgumentError)
11
+ end
11
12
  end
12
13
 
13
- describe '#get' do
14
- it 'returns address' do
15
- expect(subject.get(cep)).to eql address
14
+ context "with valid cep" do
15
+ let(:cep) { '54250610' }
16
+ let(:web_service_response) { "<end>Rua Fernando Amorim</end>" }
17
+ let(:address) { { address: "Rua Fernando Amorim" } }
18
+
19
+ before do
20
+ allow_any_instance_of(Correios::CEP::WebService).to receive(:request).with(cep){ web_service_response }
21
+ allow_any_instance_of(Correios::CEP::Parser).to receive(:address).with(web_service_response){ address }
22
+ end
23
+
24
+ describe "#get" do
25
+ it "returns address" do
26
+ expect(subject.get(cep)).to eql address
27
+ end
16
28
  end
17
- end
18
29
 
19
- describe '.get' do
20
- it 'returns address' do
21
- expect(Correios::CEP::AddressFinder.get(cep)).to eql address
30
+ describe ".get" do
31
+ it "returns address" do
32
+ expect(Correios::CEP::AddressFinder.get(cep)).to eql address
33
+ end
22
34
  end
23
35
  end
24
36
  end
@@ -2,20 +2,20 @@
2
2
  require 'spec_helper'
3
3
 
4
4
  describe Correios::CEP::Parser do
5
- describe '#address' do
5
+ describe "#address" do
6
6
  let(:expected_address) do
7
7
  {
8
- address: 'Rua Fernando Amorim',
9
- neighborhood: 'Cavaleiro',
10
- city: 'Jaboatão dos Guararapes',
11
- state: 'PE',
12
- zipcode: '54250610',
13
- complement: ''
8
+ address: "Rua Fernando Amorim",
9
+ neighborhood: "Cavaleiro",
10
+ city: "Jaboatão dos Guararapes",
11
+ state: "PE",
12
+ zipcode: "54250610",
13
+ complement: ""
14
14
  }
15
15
  end
16
16
 
17
- context 'when address is found' do
18
- context 'and does not have complement' do
17
+ context "when address is found" do
18
+ context "and does not have complement" do
19
19
  let(:xml) do
20
20
  "<?xml version='1.0' encoding='UTF-8'?>" +
21
21
  "<S:Envelope>" +
@@ -36,7 +36,7 @@ describe Correios::CEP::Parser do
36
36
  "</S:Envelope>"
37
37
  end
38
38
 
39
- it 'returns address' do
39
+ it "returns address" do
40
40
  expect(subject.address(xml)).to eq expected_address
41
41
  end
42
42
  end
@@ -62,8 +62,8 @@ describe Correios::CEP::Parser do
62
62
  "</S:Envelope>"
63
63
  end
64
64
 
65
- it 'returns address' do
66
- expected_address[:complement] = 'de 1500 até o fim'
65
+ it "returns address" do
66
+ expected_address[:complement] = "de 1500 até o fim"
67
67
 
68
68
  expect(subject.address(xml)).to eq expected_address
69
69
  end
@@ -90,8 +90,8 @@ describe Correios::CEP::Parser do
90
90
  "</S:Envelope>"
91
91
  end
92
92
 
93
- it 'returns address' do
94
- expected_address[:complement] = 'de 1500 até o fim (zona mista)'
93
+ it "returns address" do
94
+ expected_address[:complement] = "de 1500 até o fim (zona mista)"
95
95
 
96
96
  expect(subject.address(xml)).to eq expected_address
97
97
  end
@@ -108,8 +108,8 @@ describe Correios::CEP::Parser do
108
108
  "</S:Envelope>"
109
109
  end
110
110
 
111
- it 'returns nil' do
112
- expect(subject.address(xml)).to be_nil
111
+ it "returns empty hash" do
112
+ expect(subject.address(xml)).to eq({})
113
113
  end
114
114
  end
115
115
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: correios-cep
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Prodis a.k.a. Fernando Hamasaki de Amorim
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-29 00:00:00.000000000 Z
11
+ date: 2015-10-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: log-me
@@ -42,44 +42,50 @@ dependencies:
42
42
  name: coveralls
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ">="
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '0'
47
+ version: 0.8.3
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ">="
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '0'
54
+ version: 0.8.3
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: pry
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ">="
59
+ - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '0'
61
+ version: 0.10.3
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ">="
66
+ - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '0'
68
+ version: 0.10.3
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rake
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.4'
73
76
  - - ">="
74
77
  - !ruby/object:Gem::Version
75
- version: '0'
78
+ version: 10.4.2
76
79
  type: :development
77
80
  prerelease: false
78
81
  version_requirements: !ruby/object:Gem::Requirement
79
82
  requirements:
83
+ - - "~>"
84
+ - !ruby/object:Gem::Version
85
+ version: '10.4'
80
86
  - - ">="
81
87
  - !ruby/object:Gem::Version
82
- version: '0'
88
+ version: 10.4.2
83
89
  - !ruby/object:Gem::Dependency
84
90
  name: rspec
85
91
  requirement: !ruby/object:Gem::Requirement
@@ -135,6 +141,7 @@ files:
135
141
  - ".travis.yml"
136
142
  - CHANGELOG.md
137
143
  - Gemfile
144
+ - LICENSE
138
145
  - README.md
139
146
  - Rakefile
140
147
  - correios-cep.gemspec
@@ -171,7 +178,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
171
178
  version: '0'
172
179
  requirements: []
173
180
  rubyforge_project:
174
- rubygems_version: 2.4.3
181
+ rubygems_version: 2.4.5.1
175
182
  signing_key:
176
183
  specification_version: 4
177
184
  summary: Correios CEP gem gets updated Brazilian address from a zipcode, directly