Class: Rack::Client::Handler::Typhoeus

Inherits:
Object
  • Object
show all
Includes:
DualBand
Defined in:
lib/rack/client/handler/typhoeus.rb

Instance Method Summary collapse

Methods included from DualBand

#call

Constructor Details

#initialize(url, 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(url, hydra = Typhoeus::Hydra.new)
  @uri, @hydra = URI.parse(url), 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



52
53
54
55
56
57
58
# File 'lib/rack/client/handler/typhoeus.rb', line 52

def body_params_for(rack_request)
  unless %w[ HEAD GET ].include? rack_request.request_method
    {:body => rack_request.body.string}
  else
    {}
  end
end

#params_for(rack_request) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/rack/client/handler/typhoeus.rb', line 44

def params_for(rack_request)
  {
    :method => rack_request.request_method.downcase.to_sym,
    :headers => Headers.from(rack_request.env).to_http,
    :params => {}
  }.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.new(typhoeus_response.headers_hash).to_http, body)
end

#process(rack_request) ⇒ Object



40
41
42
# File 'lib/rack/client/handler/typhoeus.rb', line 40

def process(rack_request)
  ::Typhoeus::Request.run((@uri + rack_request.path).to_s, params_for(rack_request))
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((@uri + rack_request.path).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