Browse files

first commit

  • Loading branch information...
0 parents commit 7912879b6776e7ebf7c5a2cf8da20f9000b3f121 makuchaku committed Aug 16, 2011
Showing with 74 additions and 0 deletions.
  1. +25 −0 README
  2. 0 Rakefile
  3. +3 −0 bridge.kdev4
  4. +40 −0 bridge.rb
  5. +6 −0 config.ru
25 README
@@ -0,0 +1,25 @@
+jsonp-proxy
+Mike Mayo <mike@overhrd.com> twitter: @greenisus
+
+Dead simple Sinatra app to make REST API calls and respond in a JSONP style.
+https://github.com/greenisus/jsonp-proxy.git
+
+Example:
+
+http://localhost:4567/?jsonp=callback&u=https%3A//auth.api.rackspacecloud.com/v1.0
+
+If you included proper headers, you would get something like this back:
+
+callback({ the.response.json });
+
+Request params (all must be URL escaped):
+
+jsonp => callback(response);
+u => uri
+m => HTTP method (GET, POST, PUT, DELETE, HEAD, etc) default GET
+h => request headers hash in json, url encoded
+b => request body
+
+Callback argument looks like this:
+{ status: 200, headers: { header: hash }, body: 'body text' }
+
0 Rakefile
No changes.
3 bridge.kdev4
@@ -0,0 +1,3 @@
+[Project]
+Manager=KDevGenericManager
+Name=bridge
40 bridge.rb
@@ -0,0 +1,40 @@
+require 'rubygems'
+require 'sinatra'
+require "uri"
+require 'net/http'
+#require 'net/https'
+require 'json'
+
+get '/' do
+ headers = params['h'] ? JSON.parse(params['h']) : {}
+ url = URI.parse(params['u'])
+
+ if params['m'] == 'get'
+ request = Net::HTTP::Get.new(url.path)
+ elsif params['m'] == 'post'
+ request = Net::HTTP::Post.new(url.path)
+ elsif params['m'] == 'put'
+ request = Net::HTTP::Put.new(url.path)
+ elsif params['m'] == 'delete'
+ request = Net::HTTP::Delete.new(url.path)
+ elsif params['m'] == 'head'
+ request = Net::HTTP::Head.new(url.path)
+ else
+ request = Net::HTTP::Get.new(url.path)
+ end
+
+ headers.each do |key, value|
+ request.add_field(key, value)
+ end
+
+ request.body = params['b'] if params['b']
+
+ http = Net::HTTP.new(url.host, url.port)
+ http.use_ssl = false
+ response = http.start do |http|
+ http.request(request)
+ end
+
+ arg = "{ status: #{response.code}, headers: [#{response.each_name { }.to_json}], body: '#{response.body}' }"
+ json = "#{params['jsonp']}(#{arg});"
+end
6 config.ru
@@ -0,0 +1,6 @@
+require 'rubygems'
+require 'sinatra'
+require 'bridge.rb'
+
+run Sinatra::Application
+

0 comments on commit 7912879

Please sign in to comment.