viacep 1.0.1 → 2.0.0

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: 3ec579906f11b5272d4c97f865bd0d7d88d950fae82b48195e2022830a5d7fb7
4
- data.tar.gz: 16866995fcc1820366b3b03211ee5c5a497cdcf906d243186b41ee630f8ed092
3
+ metadata.gz: b64d6ee01644f4f8b6febc8ed674889738deb2b740fa3415fd34decc47af2797
4
+ data.tar.gz: f91cd76cc0aeadd76ced64672971b09b96cbecdfddaf51854f77d91fe3103002
5
5
  SHA512:
6
- metadata.gz: 0b3b0d0fa689885bb9a9420bf03f23656975dbd716b4dcb7c7003164d1bd7fd7e43f1633a7db33aafbb542ddd689640f5f5717ee5e24df9482a670dbba42a85a
7
- data.tar.gz: b952125f2c30ee2755bba65d693919fad6c65e2f8bc7860ed20710bf7499b1503af374acb38e1a48e2a7bb8c8a19f0a69bc83d5bfec2e8fec8077889d5387103
6
+ metadata.gz: 89cd06d5b60aa3bc72c40475f34ea27cef9e03e7f855ea9620418fc4ee6b9c54c6685035d747fd58b1934ff296049ca1f2c712468de35c9cf758a9c6e3462a6a
7
+ data.tar.gz: 8e460e65e109f215c6074d4344bb6252eeff3f821de721a7cf90ef6ddb8bf9e25aed3d92e38c84207d58b0f52913c12f2f2beeccbf8b7508eb5603617be344ef
data/lib/exceptions.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  module ViaCep
2
2
  class Error < StandardError; end
3
- class AddressNotFound < Error; end
3
+ class ApiRequestError < Error; end
4
4
  end
data/lib/service.rb CHANGED
@@ -1,18 +1,28 @@
1
- require 'httparty'
1
+ require 'net/http'
2
+ require 'uri'
3
+ require 'json'
2
4
  require 'timeout'
3
5
  require 'exceptions'
4
6
 
5
7
  module ViaCep
6
8
  class Service
7
- BASE_URI = 'https://viacep.com.br/ws'
9
+ BASE_URL = 'https://viacep.com.br/ws'.freeze
8
10
 
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'
11
+ def self.fetch(cep, timeout)
12
+ Timeout.timeout(timeout) do
13
+ uri = URI("#{BASE_URL}/#{cep}/json")
14
+ request = Net::HTTP.get_response(uri)
15
+
16
+ if request.code == '200'
17
+ response = JSON.parse(request.body)
18
+
19
+ if response["erro"]
20
+ raise ApiRequestError, "The server responded with HTTP 200 could not process the request"
21
+ end
22
+
23
+ response
14
24
  else
15
- response.parsed_response
25
+ raise ApiRequestError, "The server responded with HTTP #{request.code}"
16
26
  end
17
27
  end
18
28
  end
data/lib/viacep.rb CHANGED
@@ -5,13 +5,15 @@ module ViaCep
5
5
  attr_reader :cep, :address, :neighborhood, :city, :state, :ibge, :gia
6
6
 
7
7
  def initialize(cep, options = {})
8
- cep = cep.delete('^0-9')
9
- fill_from_response(Service.fetch(cep, options[:timeout]))
8
+ cep = cep.to_s.delete('^0-9')
9
+
10
+ response = Service.fetch cep, options[:timeout]
11
+ fill_from response
10
12
  end
11
13
 
12
14
  private
13
15
 
14
- def fill_from_response(response)
16
+ def fill_from(response)
15
17
  @cep = response['cep']
16
18
  @address = response['logradouro']
17
19
  @neighborhood = response['bairro']
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: 1.0.1
4
+ version: 2.0.0
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: 2011-09-29 00:00:00.000000000 Z
11
+ date: 2018-05-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -24,20 +24,6 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '3.7'
27
- - !ruby/object:Gem::Dependency
28
- name: httparty
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - "~>"
32
- - !ruby/object:Gem::Version
33
- version: 0.13.7
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: 0.13.7
41
27
  description:
42
28
  email: vnbrs@icloud.com
43
29
  executables: []
@@ -67,7 +53,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
67
53
  version: '0'
68
54
  requirements: []
69
55
  rubyforge_project:
70
- rubygems_version: 2.7.3
56
+ rubygems_version: 2.7.6
71
57
  signing_key:
72
58
  specification_version: 4
73
59
  summary: Gem responsável por buscar endereços a partir do CEP utilizando a API do