Class: AddressStandardization::GoogleMaps::Address

Inherits:
AbstractAddress show all
Defined in:
lib/address_standardization/google_maps.rb

Instance Attribute Summary

Attributes inherited from AbstractAddress

#address_info

Class Method Summary collapse

Methods inherited from AbstractAddress

#==, #initialize, #method_missing, #validate_keys

Methods included from ClassLevelInheritableAttributes

#cattr_inheritable, #inherited

Constructor Details

This class inherits a constructor from AddressStandardization::AbstractAddress

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class AddressStandardization::AbstractAddress

Class Method Details

.standardize(address_info) ⇒ Object

much of this code was borrowed from GeoKit, thanks…



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
# File 'lib/address_standardization/google_maps.rb', line 15

def standardize(address_info)
  raise "API key not specified.\nCall AddressStandardization::GoogleMaps.api_key = '...' before you call .standardize()." unless GoogleMaps.api_key
  
  address_str = "%s, %s, %s %s" % [
    address_info[:street],
    address_info[:city],
    (address_info[:state] || address_info[:province]),
    address_info[:zip]
  ]
  url = "http://maps.google.com/maps/geo?q=#{address_str.url_escape}&output=xml&key=#{GoogleMaps.api_key}&oe=utf-8"
  # puts url
  uri = URI.parse(url)
  res = Net::HTTP.get_response(uri)
  unless res.is_a?(Net::HTTPSuccess)
    File.open("test.xml", "w") {|f| f.write("(no response or response was unsuccessful)") }
    return nil 
  end
  xml = res.body
  #File.open("test.xml", "w") {|f| f.write(xml) }
  xml = Hpricot::XML(xml)
  
  if xml.at("//kml/Response/Status/code").inner_text == "200"
    addr = {}
    
    addr[:street] = get_inner_text(xml, '//ThoroughfareName')
    addr[:city] = get_inner_text(xml, '//LocalityName')
    addr[:province] = addr[:state] = get_inner_text(xml, '//AdministrativeAreaName')
    addr[:zip] = addr[:postalcode] = get_inner_text(xml, '//PostalCodeNumber')
    addr[:country] = get_inner_text(xml, '//CountryName')
    
    new(addr)
  else
    #File.open("test.xml", "w") {|f| f.write("(no response or response was unsuccessful)") }
    nil
  end
end