Class: Faraday::Adapter::Typhoeus

Inherits:
Faraday::Adapter show all
Defined in:
lib/faraday/adapter/typhoeus.rb

Constant Summary

Constants inherited from Faraday::Adapter

CONTENT_LENGTH

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Faraday::Adapter

#save_response

Methods included from Faraday::AutoloadHelper

#all_loaded_constants, #autoload_all, #load_autoloaded_constants, #lookup_module, #register_lookup_modules

Methods inherited from Middleware

dependency, #initialize, loaded?

Constructor Details

This class inherits a constructor from Faraday::Middleware

Class Method Details

.setup_parallel_manager(options = {}) ⇒ Object



6
7
8
# File 'lib/faraday/adapter/typhoeus.rb', line 6

def self.setup_parallel_manager(options = {})
  options.empty? ? ::Typhoeus::Hydra.hydra : ::Typhoeus::Hydra.new(options)
end

Instance Method Details

#call(env) ⇒ Object



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
57
58
59
60
61
# File 'lib/faraday/adapter/typhoeus.rb', line 12

def call(env)
  super

  # TODO: support streaming requests
  env[:body] = env[:body].read if env[:body].respond_to? :read

  req = ::Typhoeus::Request.new env[:url].to_s,
    :method  => env[:method],
    :body    => env[:body],
    :headers => env[:request_headers],
    :disable_ssl_peer_verification => (env[:ssl] && !env[:ssl].fetch(:verify, true))

  if ssl = env[:ssl]
    req.ssl_cert   = ssl[:client_cert_file] if ssl[:client_cert_file]
    req.ssl_key    = ssl[:client_key_file]  if ssl[:client_key_file]
    req.ssl_cacert = ssl[:ca_file]          if ssl[:ca_file]
    req.ssl_capath = ssl[:ca_path]          if ssl[:ca_path]
  end

  env_req = env[:request]
  
  if proxy = env_req[:proxy]
    req.proxy = "#{proxy[:uri].host}:#{proxy[:uri].port}"
    
    if proxy[:username] && proxy[:password]
      req.proxy_username = proxy[:username]
      req.proxy_password = proxy[:password]
    end
  end
  
  req.timeout = req.connect_timeout = (env_req[:timeout] * 1000) if env_req[:timeout]
  req.connect_timeout = (env_req[:open_timeout] * 1000)          if env_req[:open_timeout]

  is_parallel = !!env[:parallel_manager]
  req.on_complete do |resp|
    save_response(env, resp.code, resp.body) do |response_headers|
      response_headers.parse resp.headers
    end
    # in async mode, :response is initialized at this point
    env[:response].finish(env) if is_parallel
  end

  hydra = env[:parallel_manager] || self.class.setup_parallel_manager
  hydra.queue req
  hydra.run unless is_parallel

  @app.call env
rescue Errno::ECONNREFUSED
  raise Error::ConnectionFailed, $!
end