Skip to content

Instantly share code, notes, and snippets.

@danhawkins
Created March 5, 2014 17:25
Show Gist options
  • Save danhawkins/9371919 to your computer and use it in GitHub Desktop.
Save danhawkins/9371919 to your computer and use it in GitHub Desktop.
def resolve_postcode_position(code)
cache_key = "postcode:geo:#{code.gsub(' ','')}"
coords = Rails.cache.fetch(cache_key) do
postcode_uri = URI.encode "https://maps.googleapis.com/maps/api/geocode/json?address=#{code},UK&sensor=false&key=#{CONFIG[:google][:api_key]}"
response = HTTParty.get(postcode_uri)
begin
response['results'].first['geometry']['location']
rescue
false
end
end
# clear cache if unable to find
Rails.cache.delete(cache_key) unless coords
coords
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment