Skip to content

Instantly share code, notes, and snippets.

@aellispierce
Created December 7, 2015 16:57
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 aellispierce/5001ea44d59f2ed4c503 to your computer and use it in GitHub Desktop.
Save aellispierce/5001ea44d59f2ed4c503 to your computer and use it in GitHub Desktop.
Async
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