Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

sync / async API with rack-client

View rack-client-em-http.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
require 'rack/client'
require 'yajl'
require 'pp'
 
require 'eventmachine'
require 'em-synchrony'
require 'em-synchrony/em-http'
 
module Rack
module Client
class Defaults
include Rack::Client::DualBand
 
def initialize(app)
@app = app
end
 
def sync_call(env)
env['QUERY_STRING'] += '&appkey=demo'
response = Response.new(*@app.call(env))
 
response.finish
end
 
def async_call(env, &b)
env['QUERY_STRING'] += '&appkey=demo'
 
@app.call(env) do |response_parts|
response = Response.new(*response_parts)
yield response.finish
end
end
end
end
end
 
url = "http://api.postrank.com/v2/feed/info?id=igvita.com"
 
EM.synchrony do
client = Rack::Client.new do
use Rack::Client::Defaults
run Rack::Client::Handler::EmHttp
end
 
# async request
client.get url do |resp|
pp Yajl::Parser.parse(resp.body)
end
 
sleep(1)
 
# fibered sync request via em-synchrony
pp client.get url
 
EM.stop
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.