Created
July 10, 2014 08:39
-
-
Save polarblau/56b23d2f8749b4a94729 to your computer and use it in GitHub Desktop.
Proxy API request through your server to avoid cross–origin issues.
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
# 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