Skip to content

Instantly share code, notes, and snippets.

@polarblau
Created July 10, 2014 08:39
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 polarblau/56b23d2f8749b4a94729 to your computer and use it in GitHub Desktop.
Save polarblau/56b23d2f8749b4a94729 to your computer and use it in GitHub Desktop.
Proxy API request through your server to avoid cross–origin issues.
# USAGE:
#
# Mount endpoint, e.g.:
#
# map '/api_proxy' do
# run APIProxy.new
# end
#
# Then call the endpoint, passing the actual URL
# as GET param `api_url`
#
class APIProxy
def call(env)
request = Rack::Request.new(env)
api_url = request.params['api_url']
[200, { 'Content-Type' => 'application/json' }, [get_json(api_url)]]
end
private
def get_json(url)
uri = URI.parse(URI::unescape(url))
response = Net::HTTP.get(uri)
response
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment