class Faraday::Adapter::Typhoeus

Adapter to use Faraday with Typhoeus.

@example Use Typhoeus.

require 'faraday'
require 'typhoeus'
require 'typhoeus/adapters/faraday'

conn = Faraday.new(url: "www.example.com") do |faraday|
  faraday.adapter :typhoeus
end

response = conn.get("/")

Public Class Methods

setup_parallel_manager(options = {}) click to toggle source

Setup Hydra with provided options.

@example Setup Hydra.

Faraday::Adapter::Typhoeus.setup_parallel_manager
#=> #<Typhoeus::Hydra ... >

@param (see Typhoeus::Hydra#initialize) @option (see Typhoeus::Hydra#initialize)

@return [ Typhoeus::Hydra ] The hydra.

# File lib/typhoeus/adapters/faraday.rb, line 31
def self.setup_parallel_manager(options = {})
  ::Typhoeus::Hydra.new(options)
end

Public Instance Methods

call(env) click to toggle source

Hook into Faraday and perform the request with Typhoeus.

@param [ Hash ] env The environment.

@return [ void ]

Calls superclass method
# File lib/typhoeus/adapters/faraday.rb, line 42
def call(env)
  super
  perform_request env
  @app.call env
end

Private Instance Methods

configure_proxy(req, env) click to toggle source
# File lib/typhoeus/adapters/faraday.rb, line 108
def configure_proxy(req, env)
  proxy = env[:request][:proxy]
  return unless proxy

  req.options[:proxy] = "#{proxy[:uri].host}:#{proxy[:uri].port}"

  if proxy[:username] && proxy[:password]
    req.options[:proxyuserpwd] = "#{proxy[:username]}:#{proxy[:password]}"
  end
end
configure_socket(req, env) click to toggle source
# File lib/typhoeus/adapters/faraday.rb, line 125
def configure_socket(req, env)
  if bind = env[:request][:bind]
    req.options[:interface] = bind[:host]
  end
end
configure_ssl(req, env) click to toggle source
# File lib/typhoeus/adapters/faraday.rb, line 96
def configure_ssl(req, env)
  ssl = env[:ssl]

  ssl_verifyhost = (ssl && ssl.fetch(:verify, true)) ? 2 : 0
  req.options[:ssl_verifyhost] = ssl_verifyhost
  req.options[:sslversion] = ssl[:version]     if ssl[:version]
  req.options[:sslcert]    = ssl[:client_cert] if ssl[:client_cert]
  req.options[:sslkey]     = ssl[:client_key]  if ssl[:client_key]
  req.options[:cainfo]     = ssl[:ca_file]     if ssl[:ca_file]
  req.options[:capath]     = ssl[:ca_path]     if ssl[:ca_path]
end
configure_timeout(req, env) click to toggle source
# File lib/typhoeus/adapters/faraday.rb, line 119
def configure_timeout(req, env)
  env_req = env[:request]
  req.options[:timeout_ms] = (env_req[:timeout] * 1000)             if env_req[:timeout]
  req.options[:connecttimeout_ms] = (env_req[:open_timeout] * 1000) if env_req[:open_timeout]
end
parallel?(env) click to toggle source
# File lib/typhoeus/adapters/faraday.rb, line 131
def parallel?(env)
  !!env[:parallel_manager]
end
perform_request(env) click to toggle source
# File lib/typhoeus/adapters/faraday.rb, line 50
def perform_request(env)
  if parallel?(env)
    env[:parallel_manager].queue request(env)
  else
    request(env).run
  end
end
read_body(env) click to toggle source
# File lib/typhoeus/adapters/faraday.rb, line 92
def read_body(env)
  env[:body] = env[:body].read if env[:body].respond_to? :read
end
request(env) click to toggle source
# File lib/typhoeus/adapters/faraday.rb, line 58
def request(env)
  read_body env

  req = ::Typhoeus::Request.new(
    env[:url].to_s,
    :method  => env[:method],
    :body    => env[:body],
    :headers => env[:request_headers]
  )

  configure_ssl     req, env
  configure_proxy   req, env
  configure_timeout req, env
  configure_socket  req, env

  req.on_complete do |resp|
    if resp.timed_out?
      if parallel?(env)
        # TODO: error callback in async mode
      else
        raise Faraday::Error::TimeoutError, "request timed out"
      end
    end

    save_response(env, resp.code, resp.body) do |response_headers|
      response_headers.parse resp.response_headers
    end
    # in async mode, :response is initialized at this point
    env[:response].finish(env) if parallel?(env)
  end

  req
end