Class: Faraday::Adapter::Typhoeus

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

Constant Summary

Constants inherited from Faraday::Adapter

CONTENT_TYPE, DEFAULT_BOUNDARY, FORM_TYPE, MULTIPART_TYPE

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Faraday::Adapter

#full_path_for, #has_multipart?, #process_body_for_request, #process_to_params

Methods included from Faraday::AutoloadHelper

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

Methods inherited from Middleware

#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



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

def call(env)
  super

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

  env_req = env[:request]
  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]

  req.on_complete do |resp|
    env.update \
      :status           => resp.code,
      :response_headers => parse_response_headers(resp.headers),
      :body             => resp.body
    env[:response].finish(env)
  end

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

  if !env[:parallel_manager]
    hydra.run
  end

  @app.call env
rescue Errno::ECONNREFUSED
  raise Error::ConnectionFailed.new(Errno::ECONNREFUSED)
end

#create_multipart(env, params, boundary = nil) ⇒ Object

TODO: build in support for multipart streaming if typhoeus supports it.



66
67
68
69
# File 'lib/faraday/adapter/typhoeus.rb', line 66

def create_multipart(env, params, boundary = nil)
  stream = super
  stream.read
end

#in_parallel(options = {}) ⇒ Object



49
50
51
52
53
54
# File 'lib/faraday/adapter/typhoeus.rb', line 49

def in_parallel(options = {})
  @hydra = ::Typhoeus::Hydra.new(options)
  yield
  @hydra.run
  @hydra = nil
end

#parse_response_headers(header_string) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/faraday/adapter/typhoeus.rb', line 56

def parse_response_headers(header_string)
  return {} unless header_string && !header_string.empty?
  Hash[*header_string.split(/\r\n/).
    tap    { |a|      a.shift           }. # drop the HTTP status line
    map    { |h|      h.split(/:\s+/,2) }. # split key and value
    reject { |(k, v)| k.nil?            }. # Ignore blank lines
    map    { |(k, v)| [k.downcase, v]   }.flatten]
end