viacep 2.0.2 → 2.0.3

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
  SHA256:
3
- metadata.gz: 3394e46631b047c6c4d8ee13b0e73e01e1c0f935128b2ea559a172aac3724e8c
4
- data.tar.gz: 96f668e8080c5850419aa28e3190f40bb2ef0cb02dfeabc2c1e0de15904c583a
3
+ metadata.gz: d996e5aa0ad22ffabeddff59c6e01715e82506cb67ba9161c7b706c0bf3b1231
4
+ data.tar.gz: 511ab0884767f8c4534454ce935dde9d6da9a7e7571e72b8f72caa2c875a2bc3
5
5
  SHA512:
6
- metadata.gz: a6515fcf3bab3b1b96ae4ade5e98464939824acd443af707a400f13be17fbe1ea917bd58416d0e553cfef0e9280088cedef32ca61e1ccadc344d913fc3f6c304
7
- data.tar.gz: 67b0d365a02aabeda3144dc9a072f473513880573ee506a275d06cbab2435a485add1ca9b045eb55acf608c121438114e01a01cd420338ab1aa233dac556ce3e
6
+ metadata.gz: 8822385880160352463509f979354cd456dc72edc85f14906ab69d7d03a1a09c06c76b32d62f3daf9a449c7669a015304be9c0ecf680f1ce6f89996840a1cffb
7
+ data.tar.gz: 5debef0f5bd8b1babb8b268f82b62872a44d0ce59ce4f0e731e77209ebcb5a03b5e00300506abd448698fd080d12086d7186eee4b8a1633c5063adbd513aa3ef
@@ -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/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 CHANGED
@@ -1,55 +1,50 @@
1
1
  <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/b/bc/BRA_orthographic.svg/270px-BRA_orthographic.svg.png" align="right" />
2
2
 
3
- # viacep [![GitHub license](https://img.shields.io/github/license/vnbrs/burocracia.cr.svg)](https://github.com/vnbrs/burocracia.cr/blob/master/LICENSE)
4
- > Gem responsável por buscar endereços a partir do CEP utilizando a API do ViaCEP com features úteis como timeout.
3
+ # viacep ![Tests](https://github.com/vnbrs/viacep/workflows/Tests/badge.svg) [![GitHub license](https://img.shields.io/github/license/vnbrs/burocracia.cr.svg)](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
5
 
6
- ## Instalação
6
+ ## Installing
7
7
 
8
- Instale o viacep pelo [RubyGems](https://rubygems.org/gems/viacep/) facilmente via terminal:
8
+ You can easily install viacep through [RubyGems](https://rubygems.org/gems/viacep/):
9
9
 
10
10
  ```shell
11
11
  gem install viacep
12
- => Vai que é tua! 🚀
12
+ => Rock on! 🚀
13
13
  ```
14
14
 
15
- ou...
15
+ or add the following to your Gemfile:
16
16
 
17
17
  ```shell
18
- bundle add viacep
19
- => Gem instalada no Gemfile! 🤓
18
+ gem "viacep"
20
19
  ```
21
20
 
22
- ## Como usar?
21
+ ## Usage
23
22
 
24
23
  ```ruby
25
24
  require 'viacep'
26
25
 
27
- # com CEPs sem formatação...
26
+ # with unformatted zipcodes... =)
28
27
  ViaCep::Address.new('80210130')
29
28
  => #<ViaCep::Address:0x00007fe52a8a0568 @cep="80210-130", @address="Rua José Ananias Mauad", @neighborhood="Jardim Botânico", @city="Curitiba", @state="PR", @ibge="4106902", @gia="">
30
29
 
31
- # com CEPs formatados...
30
+ # with formatted zipcodes... =)
32
31
  ViaCep::Address.new('13035-680')
33
32
  => #<ViaCep::Address:0x00007fe52a99e730 @cep="13035-680", @address="Avenida João Jorge", @neighborhood="Vila Industrial", @city="Campinas", @state="SP", @ibge="3509502", @gia="2446">
34
33
 
35
- # um CEP não existente...
36
- ViaCep::Address.new('123')
34
+ # or even an nonexistent zipcode... =(
35
+ ViaCep::Address.new('12345678')
37
36
  => exception thrown: ViaCep::ApiRequestError
38
37
 
39
- # especificando um timeout em segundos...
38
+ # and specifying a timeout in seconds... =)
40
39
  ViaCep::Address.new('80210130', timeout: 0.2)
41
40
  => exception thrown: Timeout::Error (execution expired)
42
41
  ```
43
42
 
44
- ## Contribuindo
43
+ ## Contributing
45
44
 
46
- 1. Faça um fork (https://github.com/vnbrs/viacep/fork)
47
- 2. Crie uma branch (git checkout -b my-new-feature)
48
- 3. Faça um commit (git commit -am 'Add some feature')
49
- 4. Faça o push (git push origin my-new-feature)
50
- 5. Crie um Pull Request
51
- 6. Valeu! 🤙🏼
52
-
53
- ## Contribuidores
54
-
55
- - [vnbrs](https://github.com/vnbrs) Vinicius Brasil - creator, maintainer
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! 🤙🏼
@@ -0,0 +1 @@
1
+ theme: jekyll-theme-minimal
@@ -1,5 +1,5 @@
1
1
  module ViaCep
2
- VERSION = "2.0.2".freeze
2
+ VERSION = "2.0.3".freeze
3
3
 
4
4
  require_relative "viacep/address"
5
5
  require_relative "viacep/exceptions"
@@ -4,7 +4,27 @@ module ViaCep
4
4
  class Address
5
5
  attr_reader :cep, :address, :neighborhood, :city, :state, :ibge, :gia
6
6
 
7
- # Initializes an instance of Address and fetches the CEP using the external API
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
+ #
8
28
  def initialize(cep, options = {})
9
29
  if cep.nil?
10
30
  raise ArgumentError, 'CEP cannot be nil'
@@ -8,7 +8,25 @@ module ViaCep
8
8
  class Service
9
9
  BASE_URL = 'https://viacep.com.br/ws'.freeze
10
10
 
11
- # Fetches the ViaCEP API to request a CEP
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
+ #
12
30
  def self.fetch(cep, timeout)
13
31
  Timeout.timeout(timeout) do
14
32
  uri = URI("#{BASE_URL}/#{cep}/json")
@@ -1,45 +1,51 @@
1
+ require 'spec_helper'
2
+
1
3
  RSpec.describe ViaCep::Address do
2
4
  describe '#initialize' do
3
5
  context 'when a valid CEP is passed' do
4
- subject(:address) { ViaCep::Address.new('80210130') }
5
-
6
6
  it 'returns the Address' do
7
+ mock_viacep_success
8
+
9
+ address = ViaCep::Address.new('80210130')
7
10
  expect(address.address).not_to be_empty
8
11
  end
9
12
  end
10
13
 
11
14
  context 'when a invalid length CEP is passed' do
12
15
  it 'raises ArgumentError' do
13
- expect { ViaCep::Address.new('000000') }.
14
- to raise_error(ArgumentError)
16
+ expect { ViaCep::Address.new('000000') }
17
+ .to raise_error(ArgumentError)
15
18
  end
16
19
  end
17
20
 
18
21
  context 'when a unexistent CEP is passed' do
19
22
  it 'raises ViaCep::ApiRequestError' do
20
- expect { ViaCep::Address.new('00000000') }.
21
- to raise_error(ViaCep::ApiRequestError)
23
+ mock_viacep_not_found
24
+ expect { ViaCep::Address.new('00000000') }
25
+ .to raise_error(ViaCep::ApiRequestError)
22
26
  end
23
27
  end
24
-
28
+
25
29
  context 'when a nil CEP is passed' do
26
30
  it 'raises ArgumentError' do
27
- expect { ViaCep::Address.new(nil) }.
28
- to raise_error(ArgumentError)
31
+ expect { ViaCep::Address.new(nil) }
32
+ .to raise_error(ArgumentError)
29
33
  end
30
34
  end
31
35
 
32
36
  context 'when a low timeout is specified' do
33
37
  it 'raises a Timeout::Error' do
34
- expect { ViaCep::Address.new('80210130', timeout: 0.001) }.
35
- to raise_error(Timeout::Error)
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)
36
41
  end
37
42
  end
38
43
 
39
44
  context 'when a regular timeout is specified' do
40
- subject(:address) { ViaCep::Address.new('80210130', timeout: 25) }
41
-
42
45
  it 'returns the Address' do
46
+ mock_viacep_success
47
+
48
+ address = ViaCep::Address.new('80210130', timeout: 3.0)
43
49
  expect(address.address).not_to be_empty
44
50
  end
45
51
  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,8 @@
1
+ HTTP/1.1 400 Bad Request
2
+ Server: nginx/1.14.0
3
+ Date: Thu, 14 Feb 2019 23:35:24 GMT
4
+ Content-Type: text/html; charset=utf-8
5
+ Transfer-Encoding: chunked
6
+ Connection: keep-alive
7
+
8
+ <h3>ViaCEP Bad Request (400)</h3>
@@ -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
+ }
@@ -1,3 +1,5 @@
1
+ require 'spec_helper'
2
+
1
3
  RSpec.describe ViaCep::Service do
2
4
  it 'should have a base URL' do
3
5
  expect(ViaCep::Service::BASE_URL).not_to be_empty
@@ -6,23 +8,25 @@ RSpec.describe ViaCep::Service do
6
8
  describe '.fetch' do
7
9
  context 'when a small timeout is specified' do
8
10
  it 'raises Timeout::Error' do
9
- expect { ViaCep::Service.fetch('80210130', 0.001) }.
10
- to raise_error(Timeout::Error)
11
+ mock_viacep_slow_success(delay_in_seconds: 0.01)
12
+ expect { ViaCep::Service.fetch('80210130', 0.001) }
13
+ .to raise_error(Timeout::Error)
11
14
  end
12
15
  end
13
16
 
14
17
  context 'when a suficient timeout is specified' do
15
- subject(:response) { ViaCep::Service.fetch('80210130', 25) }
16
-
17
18
  it 'returns the response' do
19
+ mock_viacep_success
20
+ response = ViaCep::Service.fetch('80210130', 5.0)
18
21
  expect(response).not_to be_empty
19
22
  end
20
23
  end
21
24
 
22
25
  context 'when a invalid CEP is passed' do
23
26
  it 'raises ViaCep::ApiRequestError' do
24
- expect { ViaCep::Service.fetch('000000', nil) }.
25
- to raise_error(ViaCep::ApiRequestError)
27
+ mock_viacep_invalid_format
28
+ expect { ViaCep::Service.fetch('000000', nil) }
29
+ .to raise_error(ViaCep::ApiRequestError)
26
30
  end
27
31
  end
28
32
  end
@@ -1,6 +1,10 @@
1
1
  require_relative '../lib/viacep'
2
+ require_relative './mock/request_mocker'
3
+ require 'webmock/rspec'
2
4
 
3
5
  RSpec.configure do |config|
6
+ config.include ViaCep::Test::RequestMocker
7
+
4
8
  config.expect_with :rspec do |expectations|
5
9
  expectations.include_chain_clauses_in_custom_matcher_descriptions = true
6
10
  expectations.syntax = :expect
@@ -12,4 +16,9 @@ RSpec.configure do |config|
12
16
 
13
17
  config.shared_context_metadata_behavior = :apply_to_host_groups
14
18
  config.order = :random
19
+
20
+ config.before(:each) do
21
+ WebMock.reset!
22
+ WebMock.disable_net_connect!
23
+ end
15
24
  end
@@ -7,12 +7,13 @@ Gem::Specification.new do |s|
7
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
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
9
  s.authors = ['Vinicius Brasil (@vnbrs)']
10
- s.email = 'marcosbrasilaraujo@gmail.com'
10
+ s.email = 'vini@hey.com'
11
11
  s.license = 'MIT'
12
12
  s.homepage = 'https://github.com/vnbrs/viacep'
13
13
 
14
14
  s.require_paths = ['lib']
15
15
  s.files = `git ls-files | grep -Ev '^(test|doc|examples|.yardoc|.github)'`.split("\n")
16
16
 
17
- s.add_development_dependency 'rspec', '~> 3.7'
17
+ s.add_development_dependency 'rspec', '~> 3.7'
18
+ s.add_development_dependency 'webmock', '~> 3.8.3'
18
19
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: viacep
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.2
4
+ version: 2.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vinicius Brasil (@vnbrs)
@@ -24,22 +24,43 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '3.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: webmock
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 3.8.3
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 3.8.3
27
41
  description: Gem sem dependências e fácil de usar para busca de endereços a partir
28
42
  do CEP utilizando o ViaCEP
29
- email: marcosbrasilaraujo@gmail.com
43
+ email: vini@hey.com
30
44
  executables: []
31
45
  extensions: []
32
46
  extra_rdoc_files: []
33
47
  files:
34
48
  - ".editorconfig"
35
49
  - ".gitignore"
50
+ - CHANGELOG.md
36
51
  - Gemfile
52
+ - LICENSE
37
53
  - README.md
54
+ - _config.yml
38
55
  - lib/viacep.rb
39
56
  - lib/viacep/address.rb
40
57
  - lib/viacep/exceptions.rb
41
58
  - lib/viacep/service.rb
42
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
43
64
  - spec/service_spec.rb
44
65
  - spec/spec_helper.rb
45
66
  - viacep.gemspec
@@ -62,7 +83,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
62
83
  - !ruby/object:Gem::Version
63
84
  version: '0'
64
85
  requirements: []
65
- rubygems_version: 3.0.1
86
+ rubygems_version: 3.1.2
66
87
  signing_key:
67
88
  specification_version: 4
68
89
  summary: Gem sem dependências e fácil de usar para busca de endereços a partir do