module SimpleGeocoder
A basic, caching, geocoder
Constants
- TIME_TO_LIVE
30 days
Public Instance Methods
cache()
click to toggle source
# File lib/simple_geocode.rb, line 13 def cache @cache ||= SimpleCache.new("simple_geocoder") end
cache=(cache)
click to toggle source
# File lib/simple_geocode.rb, line 9 def cache=(cache) @cache = cache end
latlng(address)
click to toggle source
# File lib/simple_geocode.rb, line 20 def latlng(address) cache.cached(address, TIME_TO_LIVE) { url = "http://maps.googleapis.com/maps/api/geocode/json?address=#{CGI.escape(address)}&sensor=false®ion=de" data = JSON.parse(get(url)) if data["status"] != "OK" STDERR.puts "Geocoding failed for '#{address}'" return end results = data["results"] location = results[0]["geometry"]["location"] location.values_at("lat", "lng") } end
Private Instance Methods
get(uri_str, limit = 10)
click to toggle source
# File lib/simple_geocode.rb, line 38 def get(uri_str, limit = 10) raise 'too many redirections' if limit == 0 uri = URI.parse(uri_str) http = Net::HTTP.new(uri.host, uri.port) if uri.scheme == "https" http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE end request = Net::HTTP::Get.new(uri.request_uri) response = http.request(request) case response when Net::HTTPSuccess then response.body when Net::HTTPRedirection then location = response['location'] App.logger.debug "redirected to #{location}" get(location, limit - 1) else response.value end end