Skip to content

Instantly share code, notes, and snippets.

@gvarela
Created March 12, 2010 18:54
Show Gist options
  • Save gvarela/330626 to your computer and use it in GitHub Desktop.
Save gvarela/330626 to your computer and use it in GitHub Desktop.
# == Schema Information
#
# Table name: geocode_results
#
# id :integer(4) not null, primary key
# query :string(255)
# address :string(255)
# lat :string(255)
# lng :string(255)
# created_at :datetime
# updated_at :datetime
#
# Indexes
#
# index_geocode_results_on_query (query)
#
class GeocodeResult < ActiveRecord::Base
# Cache Money!
version 2
index :query
def self.find_or_new_geocode(query)
coded_query = Digest::MD5.hexdigest((query || '').strip)
geo = find_by_query(coded_query)
unless geo
res = Geokit::Geocoders::MultiGeocoder.geocode(query).to_hash
address = "#{res[:city]}, #{res[:state]}, #{res[:zip]}, #{res[:country_code]}"
geo = GeocodeResult.create(:query => coded_query, :lat => res[:lat], :lng => res[:lng], :address => address) if res[:success]
end
geo
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment