This repository has been archived by the owner on Mar 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
google_maps.rb
78 lines (73 loc) · 2.89 KB
/
google_maps.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
module AddressStandardization
# See http://code.google.com/apis/maps/documentation/geocoding/
# for documentation on Google's Geocoding API.
class GoogleMaps < AbstractService
class << self
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
def get_live_response(address_info)
# 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(" ")
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