Skip to content

Instantly share code, notes, and snippets.

@anolson
Created January 9, 2011 20:41
Show Gist options
  • Save anolson/771992 to your computer and use it in GitHub Desktop.
Save anolson/771992 to your computer and use it in GitHub Desktop.
Simple Google geocoder with HTTParty.
require 'rubygems'
require 'httparty'
module Google
module Geocode
class RequestDenied < StandardError; end
class Client
include HTTParty
format :json
def request(options = {})
response = self.class.get("http://maps.googleapis.com/maps/api/geocode/json", {:query => options})
p response["status"]
if(response["status"] == "OK")
response["results"]
else
raise_error(response)
end
end
def raise_error(response)
case response["status"]
when "REQUEST_DENIED"
raise RequestDenied, "(#{response["status"]}): #{response["error"]}"
end
end
end
#
# Usage: Google::Geocode::geocode(:address => "Blacksburg, VA")
#
def self.geocode(options ={})
Client.new.request(options.merge!({:sensor => false}))
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment