Created
January 9, 2011 20:41
-
-
Save anolson/771992 to your computer and use it in GitHub Desktop.
Simple Google geocoder with HTTParty.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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