add Typhoeus adapter, with parallel support

This commit is contained in:
rick 2009-12-10 08:59:53 -08:00
parent 93d08dd305
commit 3c8a7daca6
4 changed files with 102 additions and 25 deletions

View File

@ -11,6 +11,7 @@ module Faraday
autoload :Response, 'faraday/response'
module Adapter
autoload :NetHttp, 'faraday/adapter/net_http'
autoload :NetHttp, 'faraday/adapter/net_http'
autoload :Typhoeus, 'faraday/adapter/typhoeus'
end
end

View File

@ -0,0 +1,39 @@
require 'typhoeus'
module Faraday
module Adapter
module Typhoeus
def in_parallel?
!!@parallel_manager
end
def in_parallel(options = {})
setup_parallel_manager(options)
yield
run_parallel_requests
end
def setup_parallel_manager(options = {})
@parallel_manager ||= ::Typhoeus::Hydra.new(options)
end
def run_parallel_requests
@parallel_manager.run
@parallel_manager = nil
end
def _get(uri, request_headers)
response_class.new do |resp|
is_async = in_parallel?
setup_parallel_manager
req = ::Typhoeus::Request.new(uri.to_s, :headers => request_headers, :method => :get)
req.on_complete do |response|
resp.process(response.body)
resp.processed!
end
@parallel_manager.queue(req)
if !is_async then run_parallel_requests end
end
end
end
end
end

View File

@ -1,24 +0,0 @@
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'helper'))
class NetHttpAdapterTest < Faraday::TestCase
class Connection < Faraday::Connection
include Faraday::Adapter::NetHttp
end
Faraday::Connection.send :include, Faraday::Adapter::NetHttp
describe "#get" do
it "retrieves the response body" do
assert_equal 'hello world', Faraday::Connection.new(LIVE_SERVER).get('hello_world').body
end
it "retrieves the response JSON with YajlResponse" do
conn = Faraday::Connection.new(LIVE_SERVER)
conn.response_class = Faraday::Response::YajlResponse
assert_equal [1,2,3], conn.get('json')
end
it "retrieves the response headers" do
assert_equal 'text/html', Faraday::Connection.new(LIVE_SERVER).get('hello_world').headers['content-type']
end
end
end

61
test/adapter_test.rb Normal file
View File

@ -0,0 +1,61 @@
require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
class AdapterTest < Faraday::TestCase
before do
@connection = Faraday::Connection.new(LIVE_SERVER)
end
[Faraday::Adapter::NetHttp, Faraday::Adapter::Typhoeus].each do |adapter|
describe "#get with #{adapter} adapter" do
before do
@connection.extend adapter
end
it "retrieves the response body" do
assert_equal 'hello world', @connection.get('hello_world').body
end
it "retrieves the response JSON with StringResponse" do
@connection.response_class = Faraday::Response::StringResponse
assert_equal 'hello world', @connection.get('hello_world')
end
it "retrieves the response JSON with YajlResponse" do
@connection.response_class = Faraday::Response::YajlResponse
assert_equal [1,2,3], @connection.get('json')
end
it "retrieves the response headers" do
assert_equal 'text/html', @connection.get('hello_world').headers['content-type']
end
end
end
describe "async Typhoeus requests" do
before do
@connection.extend Faraday::Adapter::Typhoeus
end
it "clears parallel manager after running a single request" do
assert !@connection.in_parallel?
resp = @connection.get('hello_world')
assert !@connection.in_parallel?
assert_equal 'hello world', @connection.get('hello_world').body
end
it "uses parallel manager to run multiple requests" do
resp1, resp2 = nil, nil
@connection.in_parallel do
resp1 = @connection.get('hello_world')
resp2 = @connection.get('hello_world')
assert @connection.in_parallel?
assert_nil resp1.body
assert_nil resp2.body
end
assert !@connection.in_parallel?
assert_equal 'hello world', resp1.body
assert_equal 'hello world', resp2.body
end
end
end