module Geocoder

A basic, caching, geocoder

Constants

TIME_TO_LIVE

30 days

Private Class Methods

cached(key, &block) click to toggle source
# File lib/extensions/zz_geocoder.rb, line 33
def self.cached(key, &block)
  @cache ||= MicroSql.create("#{ENV["HOME"]}/geocoder.sqdb").key_value_table("geocache")
  @cache.cached(key, TIME_TO_LIVE, &block)
end

Public Instance Methods

geocode(address) click to toggle source
# File lib/extensions/zz_geocoder.rb, line 12
def geocode(address)
  Geocoder.cached(address) { __geocode__(address) }
end

Private Instance Methods

__geocode__(address) click to toggle source
# File lib/extensions/zz_geocoder.rb, line 18
def __geocode__(address)
  url = "http://maps.google.com/maps/geo?q=#{CGI.escape(address)}&output=json&oe=utf-8"

  json = Http.get(url)
  data = JSON.parse(json)

  status = data["Status"]
  if status["code"] != 200
    STDERR.puts "Geocoding failed for '#{address}' with status #{status["code"]}"
    return
  end
  
  data["Placemark"].first["Point"]["coordinates"]
end