This repository has been archived by the owner on Mar 18, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Upgrade to v3 of Google's geocoding API
- Loading branch information
Showing
7 changed files
with
120 additions
and
86 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,32 @@ | ||
# address_standardization: A tiny Ruby library to quickly standardize a postal address. | ||
# Copyright (C) 2008-2010 Elliot Winkler. Released under the MIT license. | ||
|
||
# TODO: Force users to require MelissaData or GoogleMaps manually | ||
# so that no dependency is required without being unused | ||
require 'mechanize' | ||
require 'httparty' | ||
|
||
require 'address_standardization/ruby_ext' | ||
require 'address_standardization/class_level_inheritable_attributes' | ||
|
||
require 'address_standardization/address' | ||
require 'address_standardization/abstract_service' | ||
require 'address_standardization/melissa_data' | ||
require 'address_standardization/google_maps' | ||
here = File.expand_path('..', __FILE__) | ||
require "#{here}/address_standardization/ruby_ext" | ||
require "#{here}/address_standardization/class_level_inheritable_attributes" | ||
|
||
require "#{here}/address_standardization/address" | ||
require "#{here}/address_standardization/abstract_service" | ||
require "#{here}/address_standardization/melissa_data" | ||
require "#{here}/address_standardization/google_maps" | ||
|
||
module AddressStandardization | ||
class << self | ||
attr_accessor :test_mode | ||
alias_method :test_mode?, :test_mode | ||
|
||
attr_accessor :debug_mode | ||
alias_method :debug_mode?, :debug_mode | ||
|
||
def debug(*args) | ||
puts(*args) if debug_mode? | ||
end | ||
end | ||
self.test_mode = false | ||
self.debug_mode = $DEBUG || ENV["DEBUG"] || false | ||
end | ||
end |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,78 @@ | ||
module AddressStandardization | ||
# See <http://code.google.com/apis/maps/documentation/geocoding/> | ||
# See http://code.google.com/apis/maps/documentation/geocoding/ | ||
# for documentation on Google's Geocoding API. | ||
class GoogleMaps < AbstractService | ||
class << self | ||
attr_accessor :api_key | ||
|
||
def api_key; end | ||
def api_key=(value) | ||
warn "The Google Maps API no longer requires a key, so you are free to remove `AddressStandardization::GoogleMaps.api_key = ...` from your code as it is now a no-op." | ||
end | ||
|
||
protected | ||
# much of this code was borrowed from GeoKit, thanks... | ||
def get_live_response(address_info) | ||
raise "API key not specified.\nCall AddressStandardization::GoogleMaps.api_key = '...' before you call .standardize()." unless GoogleMaps.api_key | ||
|
||
# Much of this code was borrowed from GeoKit, specifically: | ||
# https://github.com/andre/geokit-gem/blob/master/lib/geokit/geocoders.rb#L530 | ||
|
||
address_info = address_info.stringify_keys | ||
|
||
address_str = [ | ||
address_info["street"], | ||
address_info["city"], | ||
(address_info["state"] || address_info["province"]), | ||
address_info["zip"] | ||
].compact.join(" ") | ||
url = "http://maps.google.com/maps/geo?q=#{address_str.url_escape}&output=xml&key=#{GoogleMaps.api_key}&oe=utf-8" | ||
AddressStandardization.debug "[GoogleMaps] Hitting URL: #{url}" | ||
uri = URI.parse(url) | ||
res = Net::HTTP.get_response(uri) | ||
return unless res.is_a?(Net::HTTPSuccess) | ||
|
||
content = res.body | ||
AddressStandardization.debug "[GoogleMaps] Response body:" | ||
AddressStandardization.debug "--------------------------------------------------" | ||
AddressStandardization.debug content | ||
AddressStandardization.debug "--------------------------------------------------" | ||
xml = Nokogiri::XML(content) | ||
xml.remove_namespaces! # good or bad? I say good. | ||
return unless xml.at("/kml/Response/Status/code").inner_text == "200" | ||
|
||
addr = {} | ||
|
||
addr[:street] = get_inner_text(xml, '//ThoroughfareName').to_s | ||
addr[:city] = get_inner_text(xml, '//LocalityName').to_s | ||
addr[:province] = addr[:state] = get_inner_text(xml, '//AdministrativeAreaName').to_s | ||
addr[:zip] = addr[:postalcode] = get_inner_text(xml, '//PostalCodeNumber').to_s | ||
addr[:country] = get_inner_text(xml, '//CountryName').to_s | ||
addr[:county] = get_inner_text(xml, '//SubAdministrativeAreaName').to_s | ||
|
||
return if addr[:street] =~ /^\s*$/ or addr[:city] =~ /^\s*$/ | ||
|
||
Address.new(addr) | ||
end | ||
|
||
private | ||
def get_inner_text(xml, xpath) | ||
lambda {|x| x && x.inner_text.upcase }.call(xml.at(xpath)) | ||
address_country = address_info["country"] || "US" | ||
|
||
resp = HTTParty.get("http://maps.google.com/maps/api/geocode/json", | ||
:query => { | ||
:sensor => 'false', | ||
:address => address_str, | ||
:bias => address_country.downcase | ||
} | ||
) | ||
data = resp.parsed_response | ||
AddressStandardization.debug <<EOT | ||
[GoogleMaps] Response body: | ||
-------------------------------------------------------------------------------- | ||
#{resp.body} | ||
-------------------------------------------------------------------------------- | ||
EOT | ||
AddressStandardization.debug <<EOT | ||
[GoogleMaps] Parsed response: | ||
-------------------------------------------------------------------------------- | ||
#{data} | ||
-------------------------------------------------------------------------------- | ||
EOT | ||
|
||
if data['results'].any? && data['status'] == "OK" | ||
result = data['results'].first | ||
addr = {} | ||
street = ["", ""] | ||
result['address_components'].each do |comp| | ||
case | ||
when comp['types'].include?("street_number") | ||
street[0] = comp['short_name'] | ||
when comp['types'].include?("route") | ||
street[1] = comp['long_name'] | ||
when comp['types'].include?("locality") | ||
addr[:city] = comp['long_name'] | ||
when comp['types'].include?("administrative_area_level_1") | ||
addr[:state] = addr[:province] = comp['short_name'] | ||
when comp['types'].include?("postal_code") | ||
addr[:postalcode] = addr[:zip] = comp['long_name'] | ||
when comp['types'].include?("country") | ||
# addr[:country_code] = comp['short_name'] | ||
addr[:country] = comp['long_name'] | ||
when comp['types'].include?("administrative_area_level_2") | ||
addr[:county] = comp['long_name'] | ||
end | ||
end | ||
addr[:street] = street.join(" ").strip | ||
Address.new(addr) | ||
else | ||
return nil | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
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