Created
December 7, 2015 16:57
-
-
Save aellispierce/5001ea44d59f2ed4c503 to your computer and use it in GitHub Desktop.
Async
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
require 'rubygems' | |
require 'sinatra' | |
require "uri" | |
require "net/http" | |
require 'json' | |
#must use a server with at least ~60 threads to run ex. `puma config.ru -t 0:60` | |
class Proxy < Sinatra::Base | |
JOBS = {} | |
RESPONSES = {} | |
post '/start' do | |
params = JSON.parse(request.body.read) | |
json_body = { wait: false, account: params['account'], callback: "http://01925b1e.ngrok.io/callback"}.to_json | |
json_headers = { 'Content-Type' => 'application/json', 'Accept' => 'application/json' } | |
uri = URI.parse('http://jobs.asgateway.com/start') | |
http = Net::HTTP.new(uri.host, uri.port) | |
response = http.post(uri.path, json_body, json_headers) | |
id = JSON.parse(response.body)['id'] | |
JOBS[id] = Thread.current | |
Thread.stop | |
RESPONSES[id].to_json | |
end | |
post '/callback' do | |
params = JSON.parse(request.body.read) | |
id = params['id'] | |
RESPONSES[id] = params | |
JOBS[id].wakeup | |
{ ok: true }.to_json | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment