Module: ViaCep::HTTP

Defined in:
lib/via_cep/http.rb

Class Method Summary collapse

Class Method Details

.get(path:, query: {}) ⇒ Object

Main HTTP module using Net::HTTP

return [Net::HTTPOK] | [Net::HTTPBadRequest]



10
11
12
# File 'lib/via_cep/http.rb', line 10

def self.get(path:, query: {})
  Net::HTTP.get_response(ViaCep::HTTP.uri(path: path, query: query))
end

.uri(path:, query: {}) ⇒ Object



18
19
20
21
22
23
# File 'lib/via_cep/http.rb', line 18

def self.uri(path:, query: {})
  base_uri = URI(BASE_URL)
  base_uri.path = "/ws/#{URI::Parser.new.escape(path)}/json"
  base_uri.query = URI.encode_www_form(query)
  base_uri
end

.was_successful?(request) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/via_cep/http.rb', line 14

def self.was_successful?(request)
  request.code.eql?('200')
end