module IbgeLocalidades
Constants
- API_VERSION
- MAIN_URL
Public Instance Methods
as_objects(resposta)
click to toggle source
# File lib/ibge-localidades.rb, line 19 def as_objects resposta return self.new(resposta) if resposta.is_a? Hash resposta.each_with_object([]) do |obj, arr| arr << self.new(obj) end end
buscar_por_relacionamento(*args)
click to toggle source
# File lib/ibge-localidades.rb, line 26 def buscar_por_relacionamento *args args.each do |relacionamento| string_relacionamento = relacionamento.to_s define_singleton_method "listar_por_#{string_relacionamento}" do |id| as_objects get(Module.const_get("IbgeLocalidades::#{string_relacionamento.capitalize}::PATH") + id.to_s + self::PATH) end end end
find_by_id(id)
click to toggle source
# File lib/ibge-localidades.rb, line 39 def find_by_id id as_objects get(self::PATH + id.to_s) end
get(url)
click to toggle source
# File lib/ibge-localidades.rb, line 9 def get(url) uri = URI(MAIN_URL + url) resposta = JSON.parse(Net::HTTP.get(uri)) return resposta rescue StandardError => e return { 'error' => 'Ocorreu um erro.', 'backtrace'=> e.backtrace.join("\n") } end
listar()
click to toggle source
# File lib/ibge-localidades.rb, line 35 def listar as_objects get(self::PATH) end