Skip to content

Instantly share code, notes, and snippets.

@remvee
Created January 6, 2016 09:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save remvee/d0dc7a78c3a6ec9a0171 to your computer and use it in GitHub Desktop.
Save remvee/d0dc7a78c3a6ec9a0171 to your computer and use it in GitHub Desktop.
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