Sha256: cde3313431bec6ab4b305e496dfbcb4f05faa5f3176c250863ecb02dbc3f4971

Contents?: true

Size: 488 Bytes

Versions: 2

Compression:

Stored size: 488 Bytes

Contents

require 'httparty'
require 'timeout'
require 'exceptions'

module ViaCep
  class Service
    BASE_URI = 'https://viacep.com.br/ws'

    def self.fetch(cep, timeout = nil)
      Timeout::timeout(timeout) do
        response = HTTParty.get("#{BASE_URI}/#{cep}/json")
        if response.code == 404 || response.parsed_response['erro']
          raise AddressNotFound, 'the API responded with HTTP 404'
        else
          response.parsed_response
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
viacep-1.0.1 lib/service.rb
viacep-1.0.0 lib/service.rb