Class: ViaCep::Service
- Inherits:
-
Object
- Object
- ViaCep::Service
- Defined in:
- lib/viacep/service.rb
Constant Summary collapse
- BASE_URL =
'https://viacep.com.br/ws'.freeze
Class Method Summary collapse
-
.fetch(cep, timeout) ⇒ Hash
Fetches the ViaCEP API to request a CEP.
Class Method Details
.fetch(cep, timeout) ⇒ Hash
Fetches the ViaCEP API to request a CEP.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/viacep/service.rb', line 30 def self.fetch(cep, timeout) Timeout.timeout(timeout) do uri = URI("#{BASE_URL}/#{cep}/json") request = Net::HTTP.get_response(uri) if request.code == '200' response = JSON.parse(request.body) if response["erro"] raise ApiRequestError, "The server responded with HTTP 200 could not process the request" end response else raise ApiRequestError, "The server responded with HTTP #{request.code}" end end end |