Skip to content

Commit

Permalink
updated address model to use httparty and google_maps_api.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Hoyt authored and Jonathan Hoyt committed Mar 24, 2009
1 parent f61a414 commit 79f9ccc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
27 changes: 10 additions & 17 deletions app/models/address.rb
@@ -1,41 +1,34 @@
require 'lib/google_maps_api.rb'

class Address < ActiveRecord::Base
include
belongs_to :client

before_save :normalize_address

def normalize_address
# puts "normalizing"
if self.zip == nil
if address = getLocation(self.full_address)
# normalizing
if address = Address.normalize_address(self.full_address)
address["thoroughfare"] != nil ? self.thoroughfare = address["thoroughfare"] : self.thoroughfare = nil
address["city"] != nil ? self.city = address["city"] : self.city = nil
address["state"] != nil ? self.state = address["state"] : self.state = nil
address["zip"] != nil ? self.zip = address["zip"] : self.zip = nil
# puts "normalized!"
end
else
# puts "unable to normalize"
# unable to normalize
self.full_address = self.full_address
end
end

protected

def getLocation(address)
puts "getting location"
key = "ABQIAAAApuX_5BYbKnzmBE3HKXu8yBTJQa0g3IQ9GZqIMmInSLzwtGDKaBT9NkNo6YIUV4Fa6Ff5q37qmXVoMg"
output = "json"
host = "maps.google.com"
address = URI.encode(address)
path = "/maps/geo"
query = "q=#{address}&output=#{output}&key=#{key}"
puts "http://#{host}#{path}?#{query}"
uri = URI::HTTP.build({:host => host, :path => path, :query => query})

def self.normalize_address(address)

hash = GoogleMapsAPI.address_lookup(address)

data = Net::HTTP.get(uri)
hash = JSON.parse(data)
puts hash.inspect
# puts hash.inspect

return false unless hash["Status"]["code"] == 200

Expand Down
13 changes: 13 additions & 0 deletions lib/google_maps_api.rb
@@ -0,0 +1,13 @@
require 'httparty'

class GoogleMapsAPI
include HTTParty
base_uri "maps.google.com"
default_params :key => "ABQIAAAApuX_5BYbKnzmBE3HKXu8yBTJQa0g3IQ9GZqIMmInSLzwtGDKaBT9NkNo6YIUV4Fa6Ff5q37qmXVoMg",
:output => "json"

def self.address_lookup(address)
get('/maps/geo', :query => {:q => address})
end

end

0 comments on commit 79f9ccc

Please sign in to comment.