Class: Rack::Client::Handler::Typhoeus
- Inherits:
-
Object
- Object
- Rack::Client::Handler::Typhoeus
- Includes:
- DualBand
- Defined in:
- lib/rack/client/handler/typhoeus.rb
Instance Method Summary collapse
- #async_call(env) ⇒ Object
- #body_params_for(rack_request) ⇒ Object
- #headers_for(typhoeus_response) ⇒ Object
-
#initialize(hydra = ::Typhoeus::Hydra.new) ⇒ Typhoeus
constructor
A new instance of Typhoeus.
- #params_for(rack_request) ⇒ Object
- #parse(typhoeus_response) ⇒ Object
- #process(rack_request) ⇒ Object
- #query_params_for(rack_request) ⇒ Object
- #request_body(rack_request) ⇒ Object
- #request_for(rack_request) ⇒ Object
- #sync_call(env) ⇒ Object
- #url_for(rack_request) ⇒ Object
Methods included from DualBand
Constructor Details
#initialize(hydra = ::Typhoeus::Hydra.new) ⇒ Typhoeus
Returns a new instance of Typhoeus.
9 10 11 |
# File 'lib/rack/client/handler/typhoeus.rb', line 9 def initialize(hydra = ::Typhoeus::Hydra.new) @hydra = hydra end |
Instance Method Details
#async_call(env) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/rack/client/handler/typhoeus.rb', line 13 def async_call(env) rack_request = Rack::Request.new(env) typhoeus_request = request_for(rack_request) typhoeus_request.on_complete do |response| yield parse(response).finish end @hydra.queue typhoeus_request end |
#body_params_for(rack_request) ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/rack/client/handler/typhoeus.rb', line 67 def body_params_for(rack_request) unless %w[ HEAD GET ].include? rack_request.request_method {:body => request_body(rack_request) } else {} end end |
#headers_for(typhoeus_response) ⇒ Object
40 41 42 43 44 45 |
# File 'lib/rack/client/handler/typhoeus.rb', line 40 def headers_for(typhoeus_response) headers = typhoeus_response.headers_hash headers.reject! {|k,v| v.nil? } # Typhoeus Simple bug: http://github.com/pauldix/typhoeus/issues#issue/42 Headers.new(headers) end |
#params_for(rack_request) ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/rack/client/handler/typhoeus.rb', line 55 def params_for(rack_request) { :method => rack_request.request_method.downcase.to_sym, :headers => Headers.from(rack_request.env).to_http, :params => query_params_for(rack_request) }.merge(body_params_for(rack_request)) end |
#parse(typhoeus_response) ⇒ Object
31 32 33 34 |
# File 'lib/rack/client/handler/typhoeus.rb', line 31 def parse(typhoeus_response) body = (typhoeus_response.body.nil? || typhoeus_response.body.empty?) ? [] : StringIO.new(typhoeus_response.body) Response.new(typhoeus_response.code, headers_for(typhoeus_response).to_http, body) end |
#process(rack_request) ⇒ Object
47 48 49 |
# File 'lib/rack/client/handler/typhoeus.rb', line 47 def process(rack_request) ::Typhoeus::Request.new((url_for(rack_request)).to_s, params_for(rack_request)).run end |
#query_params_for(rack_request) ⇒ Object
63 64 65 |
# File 'lib/rack/client/handler/typhoeus.rb', line 63 def query_params_for(rack_request) Rack::Utils.parse_nested_query(rack_request.query_string) end |
#request_body(rack_request) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/rack/client/handler/typhoeus.rb', line 75 def request_body(rack_request) input = rack_request.env['rack.input'] if input.respond_to?(:each) body = [] input.each do |chunk| body << chunk end body.join else body.to_s end end |
#request_for(rack_request) ⇒ Object
36 37 38 |
# File 'lib/rack/client/handler/typhoeus.rb', line 36 def request_for(rack_request) ::Typhoeus::Request.new((rack_request.url).to_s, params_for(rack_request)) end |
#sync_call(env) ⇒ Object
25 26 27 28 29 |
# File 'lib/rack/client/handler/typhoeus.rb', line 25 def sync_call(env) rack_request = Rack::Request.new(env) parse(process(rack_request)).finish end |
#url_for(rack_request) ⇒ Object
51 52 53 |
# File 'lib/rack/client/handler/typhoeus.rb', line 51 def url_for(rack_request) rack_request.url.split('?').first end |