Class: CepAberto::Cep

Inherits:
Object
  • Object
show all
Defined in:
lib/cep_aberto/cep.rb

Overview

This class holds the methods to find zipcodes and address using the CEP Aberto API.

Class Method Summary collapse

Class Method Details

.cities(state, token) ⇒ Object

Returns a list of cities from a given state. Params:

state

State acronym (e.g. SP)

token

the user authorization token

Returns an array with all cities from the state



36
37
38
39
40
41
42
43
44
45
# File 'lib/cep_aberto/cep.rb', line 36

def Cep::cities(state, token)
  uri = URI("#{@domain}cities.json")
  params = {'estado' => state}
  all_cities = request_cep(uri, params, token)
  cities_arr = Array.new
  all_cities.each do |c|
    cities_arr.push(c["nome"])
  end
  return cities_arr
end

.find(cep, token) ⇒ Object

Find an address from a zipcode using the CEP Aberto API. Params:

zipcode

the zipcode to be searched by CEP Aberto API

token

the user authorization token

Returns a hash with the full address



20
21
22
23
24
25
26
27
28
29
# File 'lib/cep_aberto/cep.rb', line 20

def Cep::find(cep, token)
  validate_cep(cep)
  uri = URI("#{@domain}ceps.json")
  params = {'cep' => cep}
  address = request_cep(uri, params, token)
  if(address["logradouro"].is_a?String)
    address["logradouro"] = address["logradouro"].split(",")[0]
  end
  return address
end