Created
March 5, 2014 17:25
-
-
Save danhawkins/9371919 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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