viacep 1.0.0 → 2.0.3
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/.editorconfig +9 -0
- data/.gitignore +55 -0
- data/CHANGELOG.md +30 -0
- data/Gemfile +3 -0
- data/LICENSE +21 -0
- data/README.md +50 -0
- data/_config.yml +1 -0
- data/lib/viacep.rb +3 -21
- data/lib/viacep/address.rb +55 -0
- data/lib/{exceptions.rb → viacep/exceptions.rb} +1 -1
- data/lib/viacep/service.rb +49 -0
- data/spec/address_spec.rb +53 -0
- data/spec/mock/request_mocker.rb +28 -0
- data/spec/mock/viacep_invalid_format.txt +8 -0
- data/spec/mock/viacep_not_found.txt +17 -0
- data/spec/mock/viacep_success.txt +25 -0
- data/spec/service_spec.rb +33 -0
- data/spec/spec_helper.rb +24 -0
- data/viacep.gemspec +19 -0
- metadata +31 -15
- data/lib/service.rb +0 -20
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d996e5aa0ad22ffabeddff59c6e01715e82506cb67ba9161c7b706c0bf3b1231
|
4
|
+
data.tar.gz: 511ab0884767f8c4534454ce935dde9d6da9a7e7571e72b8f72caa2c875a2bc3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8822385880160352463509f979354cd456dc72edc85f14906ab69d7d03a1a09c06c76b32d62f3daf9a449c7669a015304be9c0ecf680f1ce6f89996840a1cffb
|
7
|
+
data.tar.gz: 5debef0f5bd8b1babb8b268f82b62872a44d0ce59ce4f0e731e77209ebcb5a03b5e00300506abd448698fd080d12086d7186eee4b8a1633c5063adbd513aa3ef
|
data/.editorconfig
ADDED
data/.gitignore
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
capybara-*.html
|
5
|
+
.rspec
|
6
|
+
/log
|
7
|
+
/db/*.sqlite3
|
8
|
+
/db/*.sqlite3-journal
|
9
|
+
/public/system
|
10
|
+
/coverage/
|
11
|
+
/InstalledFiles
|
12
|
+
/pkg/
|
13
|
+
/spec/reports/
|
14
|
+
/test/version_tmp/
|
15
|
+
**/tmp/*
|
16
|
+
**.orig
|
17
|
+
rerun.txt
|
18
|
+
pickle-email-*.html
|
19
|
+
|
20
|
+
## Specific to RubyMotion:
|
21
|
+
config/initializers/secret_token.rb
|
22
|
+
config/secrets.yml
|
23
|
+
|
24
|
+
.dat*
|
25
|
+
.repl_history
|
26
|
+
build/
|
27
|
+
|
28
|
+
## Documentation cache and generated files:
|
29
|
+
/.yardoc/
|
30
|
+
/_yardoc/
|
31
|
+
/doc/
|
32
|
+
/rdoc/
|
33
|
+
|
34
|
+
## Environment normalisation:
|
35
|
+
/.bundle/
|
36
|
+
/vendor/bundle
|
37
|
+
/lib/bundler/man/
|
38
|
+
|
39
|
+
# for a library or gem, you might want to ignore these files since the code is
|
40
|
+
# intended to run in multiple environments; otherwise, check them in:
|
41
|
+
Gemfile.lock
|
42
|
+
.ruby-version*
|
43
|
+
.ruby-gemset*
|
44
|
+
.rvmrc*
|
45
|
+
|
46
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
47
|
+
.rvmrc
|
48
|
+
|
49
|
+
# if using bower-rails ignore default bower_components path bower.json files
|
50
|
+
/vendor/assets/bower_components
|
51
|
+
*.bowerrc
|
52
|
+
bower.json
|
53
|
+
|
54
|
+
# Ignore pow environment settings
|
55
|
+
.powenv
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# Changelog
|
2
|
+
All notable changes to this project will be documented in this file.
|
3
|
+
|
4
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
5
|
+
|
6
|
+
## Unreleased
|
7
|
+
### Added
|
8
|
+
N/A
|
9
|
+
|
10
|
+
### Changed
|
11
|
+
N/A
|
12
|
+
|
13
|
+
### Deprecated
|
14
|
+
N/A
|
15
|
+
|
16
|
+
### Removed
|
17
|
+
N/A
|
18
|
+
|
19
|
+
### Fixed
|
20
|
+
N/A
|
21
|
+
|
22
|
+
### Security
|
23
|
+
N/A
|
24
|
+
|
25
|
+
## [2.0.3] - 2019-07-20
|
26
|
+
### Added
|
27
|
+
- Create CHANGELOG.md file to track changes.
|
28
|
+
|
29
|
+
### Fixed
|
30
|
+
- Improve public API documentation
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2019 Vinicius Brasil
|
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
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/b/bc/BRA_orthographic.svg/270px-BRA_orthographic.svg.png" align="right" />
|
2
|
+
|
3
|
+
# viacep  [](https://github.com/vnbrs/burocracia.cr/blob/master/LICENSE)
|
4
|
+
> Dependency-less zipcode lookup gem for Brazilian addresses with an easy-to-use API and useful features such as timeout.
|
5
|
+
|
6
|
+
## Installing
|
7
|
+
|
8
|
+
You can easily install viacep through [RubyGems](https://rubygems.org/gems/viacep/):
|
9
|
+
|
10
|
+
```shell
|
11
|
+
gem install viacep
|
12
|
+
=> Rock on! 🚀
|
13
|
+
```
|
14
|
+
|
15
|
+
or add the following to your Gemfile:
|
16
|
+
|
17
|
+
```shell
|
18
|
+
gem "viacep"
|
19
|
+
```
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
require 'viacep'
|
25
|
+
|
26
|
+
# with unformatted zipcodes... =)
|
27
|
+
ViaCep::Address.new('80210130')
|
28
|
+
=> #<ViaCep::Address:0x00007fe52a8a0568 @cep="80210-130", @address="Rua José Ananias Mauad", @neighborhood="Jardim Botânico", @city="Curitiba", @state="PR", @ibge="4106902", @gia="">
|
29
|
+
|
30
|
+
# with formatted zipcodes... =)
|
31
|
+
ViaCep::Address.new('13035-680')
|
32
|
+
=> #<ViaCep::Address:0x00007fe52a99e730 @cep="13035-680", @address="Avenida João Jorge", @neighborhood="Vila Industrial", @city="Campinas", @state="SP", @ibge="3509502", @gia="2446">
|
33
|
+
|
34
|
+
# or even an nonexistent zipcode... =(
|
35
|
+
ViaCep::Address.new('12345678')
|
36
|
+
=> exception thrown: ViaCep::ApiRequestError
|
37
|
+
|
38
|
+
# and specifying a timeout in seconds... =)
|
39
|
+
ViaCep::Address.new('80210130', timeout: 0.2)
|
40
|
+
=> exception thrown: Timeout::Error (execution expired)
|
41
|
+
```
|
42
|
+
|
43
|
+
## Contributing
|
44
|
+
|
45
|
+
1. Create a fork (https://github.com/vnbrs/viacep/fork)
|
46
|
+
2. Create a branch (git checkout -b my-new-feature)
|
47
|
+
3. Make a commit (git commit -am 'Add some feature')
|
48
|
+
4. Push your code (git push origin my-new-feature)
|
49
|
+
5. Create a Pull Request
|
50
|
+
6. Thanks! 🤙🏼
|
data/_config.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
theme: jekyll-theme-minimal
|
data/lib/viacep.rb
CHANGED
@@ -1,24 +1,6 @@
|
|
1
|
-
require 'service'
|
2
|
-
|
3
1
|
module ViaCep
|
4
|
-
|
5
|
-
attr_reader :cep, :address, :neighborhood, :city, :state, :ibge, :gia
|
6
|
-
|
7
|
-
def initialize(cep, options = {})
|
8
|
-
cep = cep.delete('^0-9')
|
9
|
-
fill_from_response(Service.fetch(cep, options[:timeout]))
|
10
|
-
end
|
11
|
-
|
12
|
-
private
|
2
|
+
VERSION = "2.0.3".freeze
|
13
3
|
|
14
|
-
|
15
|
-
|
16
|
-
@address = response['logradouro']
|
17
|
-
@neighborhood = response['bairro']
|
18
|
-
@city = response['localidade']
|
19
|
-
@state = response['uf']
|
20
|
-
@ibge = response['ibge']
|
21
|
-
@gia = response['gia']
|
22
|
-
end
|
23
|
-
end
|
4
|
+
require_relative "viacep/address"
|
5
|
+
require_relative "viacep/exceptions"
|
24
6
|
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require_relative 'service'
|
2
|
+
|
3
|
+
module ViaCep
|
4
|
+
class Address
|
5
|
+
attr_reader :cep, :address, :neighborhood, :city, :state, :ibge, :gia
|
6
|
+
|
7
|
+
#
|
8
|
+
# Initializes an instance of ViaCep::Address and fetches the CEP using the external API.
|
9
|
+
#
|
10
|
+
# @example Fetch a CEP
|
11
|
+
# ViaCep::Address.new('80210130')
|
12
|
+
# #=> #<ViaCep::Address:0x00007fe52a8a0568 @cep="80210-130", @address="Rua José Ananias Mauad", @neighborhood="Jardim Botânico", @city="Curitiba", @state="PR", @ibge="4106902", @gia="">
|
13
|
+
#
|
14
|
+
# @example Fetch a CEP with a formatted string
|
15
|
+
# ViaCep::Address.new('80210-130')
|
16
|
+
# #=> #<ViaCep::Address:0x00007fe52a8a0568 @cep="80210-130", @address="Rua José Ananias Mauad", @neighborhood="Jardim Botânico", @city="Curitiba", @state="PR", @ibge="4106902", @gia="">
|
17
|
+
#
|
18
|
+
# @param [String] cep The CEP to be fetched.
|
19
|
+
# @option options [Integer] :timeout The timeout in seconds to the request.
|
20
|
+
#
|
21
|
+
# @raise [ArgumentError] This is raised when CEP is nil.
|
22
|
+
# @raise [ArgumentError] This is raised when CEP format is invalid.
|
23
|
+
# @raise [ViaCep::ApiRequestError] This is raised when the external API is down or the CEP does not exist.
|
24
|
+
# @raise [Timeout::Error] This is raised when the timeout argument is specified and the request timed out.
|
25
|
+
#
|
26
|
+
# @return [ViaCep::Address]
|
27
|
+
#
|
28
|
+
def initialize(cep, options = {})
|
29
|
+
if cep.nil?
|
30
|
+
raise ArgumentError, 'CEP cannot be nil'
|
31
|
+
end
|
32
|
+
|
33
|
+
cep = cep.to_s.delete('^0-9')
|
34
|
+
|
35
|
+
if cep.length != 8
|
36
|
+
raise ArgumentError, 'CEP must have 8 digits'
|
37
|
+
end
|
38
|
+
|
39
|
+
response = Service.fetch cep, options[:timeout]
|
40
|
+
fill_from response
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def fill_from(response)
|
46
|
+
@cep = response['cep']
|
47
|
+
@address = response['logradouro']
|
48
|
+
@neighborhood = response['bairro']
|
49
|
+
@city = response['localidade']
|
50
|
+
@state = response['uf']
|
51
|
+
@ibge = response['ibge']
|
52
|
+
@gia = response['gia']
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'uri'
|
3
|
+
require 'json'
|
4
|
+
require 'timeout'
|
5
|
+
require_relative 'exceptions'
|
6
|
+
|
7
|
+
module ViaCep
|
8
|
+
class Service
|
9
|
+
BASE_URL = 'https://viacep.com.br/ws'.freeze
|
10
|
+
|
11
|
+
#
|
12
|
+
# Fetches the ViaCEP API to request a CEP.
|
13
|
+
#
|
14
|
+
# @example Fetch a CEP with no timeout.
|
15
|
+
# ViaCep::Service.fetch('80210130', nil)
|
16
|
+
# #=> {"cep"=>"80210-130", "logradouro"=>"Rua José Ananias Mauad", "complemento"=>"", "bairro"=>"Jardim Botânico", "localidade"=>"Curitiba", "uf"=>"PR", "unidade"=>"", "ibge"=>"4106902", "gia"=>""}
|
17
|
+
#
|
18
|
+
# @example Fetch a CEP with a timeout of 30 seconds.
|
19
|
+
# ViaCep::Service.fetch('80210130', 30)
|
20
|
+
# #=> {"cep"=>"80210-130", "logradouro"=>"Rua José Ananias Mauad", "complemento"=>"", "bairro"=>"Jardim Botânico", "localidade"=>"Curitiba", "uf"=>"PR", "unidade"=>"", "ibge"=>"4106902", "gia"=>""}
|
21
|
+
#
|
22
|
+
# @param [String] cep The CEP to be fetched.
|
23
|
+
# @param [Integer, nil] timeout The timeout in seconds for the request to be finished.
|
24
|
+
#
|
25
|
+
# @raise [ViaCep::ApiRequestError] This is raised when the external API is down or the CEP does not exist.
|
26
|
+
# @raise [Timeout::Error] This is raised when the timeout argument is specified and the request timed out.
|
27
|
+
#
|
28
|
+
# @return [Hash]
|
29
|
+
#
|
30
|
+
def self.fetch(cep, timeout)
|
31
|
+
Timeout.timeout(timeout) do
|
32
|
+
uri = URI("#{BASE_URL}/#{cep}/json")
|
33
|
+
request = Net::HTTP.get_response(uri)
|
34
|
+
|
35
|
+
if request.code == '200'
|
36
|
+
response = JSON.parse(request.body)
|
37
|
+
|
38
|
+
if response["erro"]
|
39
|
+
raise ApiRequestError, "The server responded with HTTP 200 could not process the request"
|
40
|
+
end
|
41
|
+
|
42
|
+
response
|
43
|
+
else
|
44
|
+
raise ApiRequestError, "The server responded with HTTP #{request.code}"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe ViaCep::Address do
|
4
|
+
describe '#initialize' do
|
5
|
+
context 'when a valid CEP is passed' do
|
6
|
+
it 'returns the Address' do
|
7
|
+
mock_viacep_success
|
8
|
+
|
9
|
+
address = ViaCep::Address.new('80210130')
|
10
|
+
expect(address.address).not_to be_empty
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
context 'when a invalid length CEP is passed' do
|
15
|
+
it 'raises ArgumentError' do
|
16
|
+
expect { ViaCep::Address.new('000000') }
|
17
|
+
.to raise_error(ArgumentError)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
context 'when a unexistent CEP is passed' do
|
22
|
+
it 'raises ViaCep::ApiRequestError' do
|
23
|
+
mock_viacep_not_found
|
24
|
+
expect { ViaCep::Address.new('00000000') }
|
25
|
+
.to raise_error(ViaCep::ApiRequestError)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'when a nil CEP is passed' do
|
30
|
+
it 'raises ArgumentError' do
|
31
|
+
expect { ViaCep::Address.new(nil) }
|
32
|
+
.to raise_error(ArgumentError)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
context 'when a low timeout is specified' do
|
37
|
+
it 'raises a Timeout::Error' do
|
38
|
+
mock_viacep_slow_success(delay_in_seconds: 0.01)
|
39
|
+
expect { ViaCep::Address.new('80210130', timeout: 0.001) }
|
40
|
+
.to raise_error(Timeout::Error)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context 'when a regular timeout is specified' do
|
45
|
+
it 'returns the Address' do
|
46
|
+
mock_viacep_success
|
47
|
+
|
48
|
+
address = ViaCep::Address.new('80210130', timeout: 3.0)
|
49
|
+
expect(address.address).not_to be_empty
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'webmock/rspec'
|
2
|
+
|
3
|
+
module ViaCep
|
4
|
+
module Test
|
5
|
+
module RequestMocker
|
6
|
+
def mock_viacep_success
|
7
|
+
WebMock.stub_request(:get, /viacep/).to_return(File.new("#{__dir__}/viacep_success.txt"))
|
8
|
+
end
|
9
|
+
|
10
|
+
def mock_viacep_not_found
|
11
|
+
WebMock.stub_request(:get, /viacep/).to_return(File.new("#{__dir__}/viacep_not_found.txt"))
|
12
|
+
end
|
13
|
+
|
14
|
+
def mock_viacep_invalid_format
|
15
|
+
WebMock.stub_request(:get, /viacep/).to_return(File.new("#{__dir__}/viacep_invalid_format.txt"))
|
16
|
+
end
|
17
|
+
|
18
|
+
def mock_viacep_slow_success(delay_in_seconds:)
|
19
|
+
success = lambda do |request|
|
20
|
+
sleep delay_in_seconds
|
21
|
+
File.new("#{__dir__}/viacep_success.txt")
|
22
|
+
end
|
23
|
+
|
24
|
+
WebMock.stub_request(:get, /viacep/).to_return(success)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
HTTP/1.1 200 OK
|
2
|
+
Server: nginx/1.14.0
|
3
|
+
Date: Thu, 14 Feb 2019 23:34:10 GMT
|
4
|
+
Content-Type: application/json; charset=utf-8
|
5
|
+
Transfer-Encoding: chunked
|
6
|
+
Connection: keep-alive
|
7
|
+
Access-Control-Allow-Origin: *
|
8
|
+
Access-Control-Allow-Methods: GET, OPTIONS
|
9
|
+
Access-Control-Allow-Headers: Content-Type, X-Request-With, X-Requested-By
|
10
|
+
Access-Control-Allow-Credentials: true
|
11
|
+
Access-Control-Max-Age: 86400
|
12
|
+
Cache-Control: max-age=3600
|
13
|
+
Cache-Control: public
|
14
|
+
|
15
|
+
{
|
16
|
+
"erro": true
|
17
|
+
}
|
@@ -0,0 +1,25 @@
|
|
1
|
+
HTTP/1.1 200 OK
|
2
|
+
Server: nginx/1.14.0
|
3
|
+
Date: Thu, 14 Feb 2019 23:32:54 GMT
|
4
|
+
Content-Type: application/json; charset=utf-8
|
5
|
+
Transfer-Encoding: chunked
|
6
|
+
Connection: keep-alive
|
7
|
+
Access-Control-Allow-Origin: *
|
8
|
+
Access-Control-Allow-Methods: GET, OPTIONS
|
9
|
+
Access-Control-Allow-Headers: Content-Type, X-Request-With, X-Requested-By
|
10
|
+
Access-Control-Allow-Credentials: true
|
11
|
+
Access-Control-Max-Age: 86400
|
12
|
+
Cache-Control: max-age=3600
|
13
|
+
Cache-Control: public
|
14
|
+
|
15
|
+
{
|
16
|
+
"cep": "13035-680",
|
17
|
+
"logradouro": "Avenida João Jorge",
|
18
|
+
"complemento": "",
|
19
|
+
"bairro": "Vila Industrial",
|
20
|
+
"localidade": "Campinas",
|
21
|
+
"uf": "SP",
|
22
|
+
"unidade": "",
|
23
|
+
"ibge": "3509502",
|
24
|
+
"gia": "2446"
|
25
|
+
}
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe ViaCep::Service do
|
4
|
+
it 'should have a base URL' do
|
5
|
+
expect(ViaCep::Service::BASE_URL).not_to be_empty
|
6
|
+
end
|
7
|
+
|
8
|
+
describe '.fetch' do
|
9
|
+
context 'when a small timeout is specified' do
|
10
|
+
it 'raises Timeout::Error' do
|
11
|
+
mock_viacep_slow_success(delay_in_seconds: 0.01)
|
12
|
+
expect { ViaCep::Service.fetch('80210130', 0.001) }
|
13
|
+
.to raise_error(Timeout::Error)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
context 'when a suficient timeout is specified' do
|
18
|
+
it 'returns the response' do
|
19
|
+
mock_viacep_success
|
20
|
+
response = ViaCep::Service.fetch('80210130', 5.0)
|
21
|
+
expect(response).not_to be_empty
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context 'when a invalid CEP is passed' do
|
26
|
+
it 'raises ViaCep::ApiRequestError' do
|
27
|
+
mock_viacep_invalid_format
|
28
|
+
expect { ViaCep::Service.fetch('000000', nil) }
|
29
|
+
.to raise_error(ViaCep::ApiRequestError)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require_relative '../lib/viacep'
|
2
|
+
require_relative './mock/request_mocker'
|
3
|
+
require 'webmock/rspec'
|
4
|
+
|
5
|
+
RSpec.configure do |config|
|
6
|
+
config.include ViaCep::Test::RequestMocker
|
7
|
+
|
8
|
+
config.expect_with :rspec do |expectations|
|
9
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
10
|
+
expectations.syntax = :expect
|
11
|
+
end
|
12
|
+
|
13
|
+
config.mock_with :rspec do |mocks|
|
14
|
+
mocks.verify_partial_doubles = true
|
15
|
+
end
|
16
|
+
|
17
|
+
config.shared_context_metadata_behavior = :apply_to_host_groups
|
18
|
+
config.order = :random
|
19
|
+
|
20
|
+
config.before(:each) do
|
21
|
+
WebMock.reset!
|
22
|
+
WebMock.disable_net_connect!
|
23
|
+
end
|
24
|
+
end
|
data/viacep.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require File.expand_path('../lib/viacep', __FILE__)
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = 'viacep'
|
5
|
+
s.version = ViaCep::VERSION
|
6
|
+
s.date = '2018-05-02'
|
7
|
+
s.summary = 'Gem sem dependências e fácil de usar para busca de endereços a partir do CEP utilizando o ViaCEP'
|
8
|
+
s.description = 'Gem sem dependências e fácil de usar para busca de endereços a partir do CEP utilizando o ViaCEP'
|
9
|
+
s.authors = ['Vinicius Brasil (@vnbrs)']
|
10
|
+
s.email = 'vini@hey.com'
|
11
|
+
s.license = 'MIT'
|
12
|
+
s.homepage = 'https://github.com/vnbrs/viacep'
|
13
|
+
|
14
|
+
s.require_paths = ['lib']
|
15
|
+
s.files = `git ls-files | grep -Ev '^(test|doc|examples|.yardoc|.github)'`.split("\n")
|
16
|
+
|
17
|
+
s.add_development_dependency 'rspec', '~> 3.7'
|
18
|
+
s.add_development_dependency 'webmock', '~> 3.8.3'
|
19
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: viacep
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vinicius Brasil (@vnbrs)
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-05-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -25,29 +25,46 @@ dependencies:
|
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '3.7'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: webmock
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
34
|
-
type: :
|
33
|
+
version: 3.8.3
|
34
|
+
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
41
|
-
description:
|
42
|
-
|
40
|
+
version: 3.8.3
|
41
|
+
description: Gem sem dependências e fácil de usar para busca de endereços a partir
|
42
|
+
do CEP utilizando o ViaCEP
|
43
|
+
email: vini@hey.com
|
43
44
|
executables: []
|
44
45
|
extensions: []
|
45
46
|
extra_rdoc_files: []
|
46
47
|
files:
|
47
|
-
-
|
48
|
-
-
|
48
|
+
- ".editorconfig"
|
49
|
+
- ".gitignore"
|
50
|
+
- CHANGELOG.md
|
51
|
+
- Gemfile
|
52
|
+
- LICENSE
|
53
|
+
- README.md
|
54
|
+
- _config.yml
|
49
55
|
- lib/viacep.rb
|
50
|
-
|
56
|
+
- lib/viacep/address.rb
|
57
|
+
- lib/viacep/exceptions.rb
|
58
|
+
- lib/viacep/service.rb
|
59
|
+
- spec/address_spec.rb
|
60
|
+
- spec/mock/request_mocker.rb
|
61
|
+
- spec/mock/viacep_invalid_format.txt
|
62
|
+
- spec/mock/viacep_not_found.txt
|
63
|
+
- spec/mock/viacep_success.txt
|
64
|
+
- spec/service_spec.rb
|
65
|
+
- spec/spec_helper.rb
|
66
|
+
- viacep.gemspec
|
67
|
+
homepage: https://github.com/vnbrs/viacep
|
51
68
|
licenses:
|
52
69
|
- MIT
|
53
70
|
metadata: {}
|
@@ -66,10 +83,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
66
83
|
- !ruby/object:Gem::Version
|
67
84
|
version: '0'
|
68
85
|
requirements: []
|
69
|
-
|
70
|
-
rubygems_version: 2.7.3
|
86
|
+
rubygems_version: 3.1.2
|
71
87
|
signing_key:
|
72
88
|
specification_version: 4
|
73
|
-
summary: Gem
|
74
|
-
|
89
|
+
summary: Gem sem dependências e fácil de usar para busca de endereços a partir do
|
90
|
+
CEP utilizando o ViaCEP
|
75
91
|
test_files: []
|
data/lib/service.rb
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
require 'httparty'
|
2
|
-
require 'timeout'
|
3
|
-
require 'exceptions'
|
4
|
-
|
5
|
-
module ViaCep
|
6
|
-
class Service
|
7
|
-
BASE_URI = 'https://viacep.com.br/ws'
|
8
|
-
|
9
|
-
def self.fetch(cep, timeout = nil)
|
10
|
-
Timeout::timeout(timeout) do
|
11
|
-
response = HTTParty.get("#{BASE_URI}/#{cep}/json")
|
12
|
-
if response.code == 404 || response.parsed_response['erro']
|
13
|
-
raise AddressNotFound, 'the API responded with HTTP 404'
|
14
|
-
else
|
15
|
-
response.parsed_response
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|