Created
January 6, 2016 09:12
-
-
Save remvee/d0dc7a78c3a6ec9a0171 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
require 'net/http' | |
class GoogleMapsImage | |
class << self | |
attr_writer :api_key | |
def get(lat, long, options = {}) | |
options = {width: 500, height: 500}.merge(options) | |
key = [lat, long, options] | |
return cache[key] if cache.has_key?(key) | |
uri = URI("https://example.com/").tap do |u| | |
u.query = URI.encode_www_form( | |
width: options[:width], | |
height: options[:height], | |
api_key: @api_key | |
) | |
end | |
resp = Net::HTTP.get_response(uri) | |
if resp.is_a?(Net::HTTPSuccess) | |
cache[key] = [resp.content_type, resp.body] | |
end | |
end | |
private | |
def cache | |
@cache ||= {} | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment