viacep 2.0.0 → 2.0.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b64d6ee01644f4f8b6febc8ed674889738deb2b740fa3415fd34decc47af2797
4
- data.tar.gz: f91cd76cc0aeadd76ced64672971b09b96cbecdfddaf51854f77d91fe3103002
3
+ metadata.gz: 6a0da61db07bc87dc9593747ae9d63580cc685e1d299da83dd0a03ab0ff13419
4
+ data.tar.gz: cf244b4466c7a73e25ffbf84024faf8fd2f50c7b797e6c9a892f9c381a4aa1b6
5
5
  SHA512:
6
- metadata.gz: 89cd06d5b60aa3bc72c40475f34ea27cef9e03e7f855ea9620418fc4ee6b9c54c6685035d747fd58b1934ff296049ca1f2c712468de35c9cf758a9c6e3462a6a
7
- data.tar.gz: 8e460e65e109f215c6074d4344bb6252eeff3f821de721a7cf90ef6ddb8bf9e25aed3d92e38c84207d58b0f52913c12f2f2beeccbf8b7508eb5603617be344ef
6
+ metadata.gz: 99d3fb4ba76ab983c3eadb79194e84fe2c73614724adedc8d9498ac5cce9e41e430e9b5587518a6fe731a3312a18b6bc11c509bab363a4eecaa291b3c3668a7c
7
+ data.tar.gz: 1ba60a80b2fefb9a5f80d941d84a4c58ad6bc5b83cd2d5ae95c8b5ab0e259f88c52f3f98f33e077a7b4bdd484c953848c0dc6828daf8d97a898772d953d370cf
@@ -0,0 +1,9 @@
1
+ root = true
2
+
3
+ [*]
4
+ indent_style = space
5
+ indent_size = 2
6
+ end_of_line = lf
7
+ charset = utf-8
8
+ trim_trailing_whitespace = true
9
+ insert_final_newline = true
@@ -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
@@ -0,0 +1 @@
1
+ --markup=markdown
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
3
+ gemspec
@@ -0,0 +1,48 @@
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 [![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.
5
+
6
+ ## Instalação
7
+
8
+ Instale o viacep pelo [RubyGems](https://rubygems.org/gems/viacep/) facilmente via terminal:
9
+
10
+ ```shell
11
+ gem install viacep
12
+ => Vai que é tua! 🚀
13
+ ```
14
+
15
+ ## Como usar?
16
+
17
+ ```ruby
18
+ require 'viacep'
19
+
20
+ # com CEPs sem formatação...
21
+ ViaCep::Address.new('80210130')
22
+ => #<ViaCep::Address:0x00007fe52a8a0568 @cep="80210-130", @address="Rua José Ananias Mauad", @neighborhood="Jardim Botânico", @city="Curitiba", @state="PR", @ibge="4106902", @gia="">
23
+
24
+ # com CEPs formatados...
25
+ ViaCep::Address.new('13035-680')
26
+ => #<ViaCep::Address:0x00007fe52a99e730 @cep="13035-680", @address="Avenida João Jorge", @neighborhood="Vila Industrial", @city="Campinas", @state="SP", @ibge="3509502", @gia="2446">
27
+
28
+ # um CEP não existente...
29
+ ViaCep::Address.new('123')
30
+ => exception thrown: ViaCep::ApiRequestError
31
+
32
+ # especificando um timeout em segundos...
33
+ ViaCep::Address.new('80210130', timeout: 0.2)
34
+ => exception thrown: Timeout::Error (execution expired)
35
+ ```
36
+
37
+ ## Contribuindo
38
+
39
+ 1. Faça um fork (https://github.com/vnbrs/viacep/fork)
40
+ 2. Crie uma branch (git checkout -b my-new-feature)
41
+ 3. Faça um commit (git commit -am 'Add some feature')
42
+ 4. Faça o push (git push origin my-new-feature)
43
+ 5. Crie um Pull Request
44
+ 6. Valeu! 🤙🏼
45
+
46
+ ## Contribuidores
47
+
48
+ - [vnbrs](https://github.com/vnbrs) Vinicius Brasil - creator, maintainer
@@ -1,26 +1,6 @@
1
- require 'service'
2
-
3
1
  module ViaCep
4
- class Address
5
- attr_reader :cep, :address, :neighborhood, :city, :state, :ibge, :gia
6
-
7
- def initialize(cep, options = {})
8
- cep = cep.to_s.delete('^0-9')
9
-
10
- response = Service.fetch cep, options[:timeout]
11
- fill_from response
12
- end
13
-
14
- private
2
+ VERSION = "2.0.1".freeze
15
3
 
16
- def fill_from(response)
17
- @cep = response['cep']
18
- @address = response['logradouro']
19
- @neighborhood = response['bairro']
20
- @city = response['localidade']
21
- @state = response['uf']
22
- @ibge = response['ibge']
23
- @gia = response['gia']
24
- end
25
- end
4
+ require_relative "viacep/address"
5
+ require_relative "viacep/exceptions"
26
6
  end
@@ -0,0 +1,27 @@
1
+ require_relative 'service'
2
+
3
+ module ViaCep
4
+ class Address
5
+ attr_reader :cep, :address, :neighborhood, :city, :state, :ibge, :gia
6
+
7
+ # Initializes an instance of Address and fetches the CEP using the external API
8
+ def initialize(cep, options = {})
9
+ cep = cep.to_s.delete('^0-9')
10
+
11
+ response = Service.fetch cep, options[:timeout]
12
+ fill_from response
13
+ end
14
+
15
+ private
16
+
17
+ def fill_from(response)
18
+ @cep = response['cep']
19
+ @address = response['logradouro']
20
+ @neighborhood = response['bairro']
21
+ @city = response['localidade']
22
+ @state = response['uf']
23
+ @ibge = response['ibge']
24
+ @gia = response['gia']
25
+ end
26
+ end
27
+ end
@@ -2,12 +2,13 @@ require 'net/http'
2
2
  require 'uri'
3
3
  require 'json'
4
4
  require 'timeout'
5
- require 'exceptions'
5
+ require_relative 'exceptions'
6
6
 
7
7
  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
  def self.fetch(cep, timeout)
12
13
  Timeout.timeout(timeout) do
13
14
  uri = URI("#{BASE_URL}/#{cep}/json")
@@ -0,0 +1,33 @@
1
+ RSpec.describe ViaCep::Address do
2
+ describe '#initialize' do
3
+ context 'when a valid CEP is passed' do
4
+ subject(:address) { ViaCep::Address.new('80210130') }
5
+
6
+ it 'returns the Address' do
7
+ expect(address.address).not_to be_empty
8
+ end
9
+ end
10
+
11
+ context 'when a invalid CEP is passed' do
12
+ it 'raises ViaCep::ApiRequestError' do
13
+ expect { ViaCep::Address.new('000000') }.
14
+ to raise_error(ViaCep::ApiRequestError)
15
+ end
16
+ end
17
+
18
+ context 'when a low timeout is specified' do
19
+ it 'raises a Timeout::Error' do
20
+ expect { ViaCep::Address.new('80210130', timeout: 0.001) }.
21
+ to raise_error(Timeout::Error)
22
+ end
23
+ end
24
+
25
+ context 'when a regular timeout is specified' do
26
+ subject(:address) { ViaCep::Address.new('80210130', timeout: 25) }
27
+
28
+ it 'returns the Address' do
29
+ expect(address.address).not_to be_empty
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,29 @@
1
+ RSpec.describe ViaCep::Service do
2
+ it 'should have a base URL' do
3
+ expect(ViaCep::Service::BASE_URL).not_to be_empty
4
+ end
5
+
6
+ describe '.fetch' do
7
+ context 'when a small timeout is specified' do
8
+ it 'raises Timeout::Error' do
9
+ expect { ViaCep::Service.fetch('80210130', 0.001) }.
10
+ to raise_error(Timeout::Error)
11
+ end
12
+ end
13
+
14
+ context 'when a suficient timeout is specified' do
15
+ subject(:response) { ViaCep::Service.fetch('80210130', 25) }
16
+
17
+ it 'returns the response' do
18
+ expect(response).not_to be_empty
19
+ end
20
+ end
21
+
22
+ context 'when a invalid CEP is passed' do
23
+ it 'raises ViaCep::ApiRequestError' do
24
+ expect { ViaCep::Service.fetch('000000', nil) }.
25
+ to raise_error(ViaCep::ApiRequestError)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,15 @@
1
+ require_relative '../lib/viacep'
2
+
3
+ RSpec.configure do |config|
4
+ config.expect_with :rspec do |expectations|
5
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
6
+ expectations.syntax = :expect
7
+ end
8
+
9
+ config.mock_with :rspec do |mocks|
10
+ mocks.verify_partial_doubles = true
11
+ end
12
+
13
+ config.shared_context_metadata_behavior = :apply_to_host_groups
14
+ config.order = :random
15
+ end
@@ -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 = 'marcosbrasilaraujo@gmail.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 'yard', '~> 0.9.11'
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.0
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vinicius Brasil (@vnbrs)
@@ -24,16 +24,41 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '3.7'
27
- description:
28
- email: vnbrs@icloud.com
27
+ - !ruby/object:Gem::Dependency
28
+ name: yard
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.9.11
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.9.11
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: marcosbrasilaraujo@gmail.com
29
44
  executables: []
30
45
  extensions: []
31
46
  extra_rdoc_files: []
32
47
  files:
33
- - lib/exceptions.rb
34
- - lib/service.rb
48
+ - ".editorconfig"
49
+ - ".gitignore"
50
+ - ".yardopts"
51
+ - Gemfile
52
+ - README.md
35
53
  - lib/viacep.rb
36
- homepage:
54
+ - lib/viacep/address.rb
55
+ - lib/viacep/exceptions.rb
56
+ - lib/viacep/service.rb
57
+ - spec/address_spec.rb
58
+ - spec/service_spec.rb
59
+ - spec/spec_helper.rb
60
+ - viacep.gemspec
61
+ homepage: https://github.com/vnbrs/viacep
37
62
  licenses:
38
63
  - MIT
39
64
  metadata: {}
@@ -56,6 +81,6 @@ rubyforge_project:
56
81
  rubygems_version: 2.7.6
57
82
  signing_key:
58
83
  specification_version: 4
59
- summary: Gem responsável por buscar endereços a partir do CEP utilizando a API do
60
- ViaCEP com features úteis como timeout.
84
+ summary: Gem sem dependências e fácil de usar para busca de endereços a partir do
85
+ CEP utilizando o ViaCEP
61
86
  test_files: []