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

Instance Attribute Summary

Attributes included from Parallelism

#supports_parallel

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Faraday::Adapter

#prepend_proxy_auth_string, #save_response

Methods included from Faraday::AutoloadHelper

#all_loaded_constants, #autoload_all, #load_autoloaded_constants

Methods included from MiddlewareRegistry

#lookup_middleware, #register_middleware

Methods included from Parallelism

#inherited, #supports_parallel?

Methods inherited from Middleware

dependency, inherited, #initialize, loaded?, new

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

def call(env)
  super
  perform_request env
  @app.call env
end

#configure_proxy(req, env) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/faraday/adapter/typhoeus.rb', line 73

def configure_proxy(req, env)
  proxy = request_options(env)[:proxy]
  return unless 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

#configure_ssl(req, env) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/faraday/adapter/typhoeus.rb', line 63

def configure_ssl(req, env)
  ssl = env[:ssl]

  req.ssl_version = ssl[:version]          if ssl[:version]
  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

#configure_timeout(req, env) ⇒ Object



85
86
87
88
89
# File 'lib/faraday/adapter/typhoeus.rb', line 85

def configure_timeout(req, env)
  env_req = request_options(env)
  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]
end

#parallel?(env) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/faraday/adapter/typhoeus.rb', line 95

def parallel?(env)
  !!env[:parallel_manager]
end

#perform_request(env) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/faraday/adapter/typhoeus.rb', line 18

def perform_request(env)
  read_body env

  hydra = env[:parallel_manager] || self.class.setup_parallel_manager
  hydra.queue request(env)
  hydra.run unless parallel?(env)
rescue Errno::ECONNREFUSED
  raise Error::ConnectionFailed, $!
end

#read_body(env) ⇒ Object

TODO: support streaming requests



29
30
31
# File 'lib/faraday/adapter/typhoeus.rb', line 29

def read_body(env)
  env[:body] = env[:body].read if env[:body].respond_to? :read
end

#request(env) ⇒ Object



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 33

def request(env)
  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))

  configure_ssl     req, env
  configure_proxy   req, env
  configure_timeout 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.headers
    end
    # in async mode, :response is initialized at this point
    env[:response].finish(env) if parallel?(env)
  end

  req
end

#request_options(env) ⇒ Object



91
92
93
# File 'lib/faraday/adapter/typhoeus.rb', line 91

def request_options(env)
  env[:request]
end