Sha256: bfeaeba39d597aab7f9242ede17a636fa5c7c5be683d434b808e8fe9f40eb060
Contents?: true
Size: 835 Bytes
Versions: 6
Compression:
Stored size: 835 Bytes
Contents
require "cgi" require "json" require "micro_sql" # A basic, caching, geocoder module Geocoder extend self # 30 days TIME_TO_LIVE = 30 * 24 * 3600 def geocode(address) Geocoder.cached(address) { __geocode__(address) } end private 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 def self.cached(key, &block) @cache ||= MicroSql.create("#{ENV["HOME"]}/geocoder.sqdb").key_value_table("geocache") @cache.cached(key, TIME_TO_LIVE, &block) end end
Version data entries
6 entries across 6 versions & 1 rubygems