Skip to content

Instantly share code, notes, and snippets.

@xenda
Created June 12, 2012 18:05
Show Gist options
  • Save xenda/2919060 to your computer and use it in GitHub Desktop.
Save xenda/2919060 to your computer and use it in GitHub Desktop.
require 'xmlsimple'
require 'json'
class WebAPI
def self.districts(city_id = nil)
params ||= {:ciudad_id => city_id} if city_id
loadJSON("distritos", params)
end
def self.cities(country_id = nil)
params ||= {:pais_id => country_id} if country_id
loadJSON("ciudades",params)
end
def self.countries
loadJSON("paises")
end
def self.country_name(id)
WebAPI.find_name("paises",id)
end
def self.city_name(id)
WebAPI.find_name("ciudades",id)
end
def self.district_name(id)
WebAPI.find_name("distritos",id)
end
def self.find_name(kind, id)
path = "http://semanaeconomica.com/#{kind}/#{id}.json"
Rails.logger.info path
req = HTTParty.get(path)
Rails.logger.info req.inspect
o = JSON.parse(req.body)
o["nombre"]
rescue Exception => ex
Rails.logger.info ex.message
""
end
def self.loadJSON(path,params={})
Rails.logger.info "Loading"
Rails.logger.info path
Rails.logger.info params.inspect
@cache ||= {}
key = path
path = "http://semanaeconomica.com/#{path}.json"
@cache[[key, params.hash]] ||= JSON.parse(HTTParty.get(path, :query => params).body).map{|h| [h["nombre"],h["id"]]}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment