Class: Faraday::Adapter::EMSynchrony

Inherits:
Faraday::Adapter show all
Includes:
Faraday::Adapter::EMHttp::Options
Defined in:
lib/faraday/adapter/em_synchrony.rb,
lib/faraday/adapter/em_synchrony/parallel_manager.rb

Defined Under Namespace

Classes: ParallelManager

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 included from Faraday::Adapter::EMHttp::Options

#configure_compression, #configure_proxy, #configure_socket, #configure_ssl, #configure_timeout, #connection_config, #read_body, #request_config, #request_options

Methods inherited from Faraday::Adapter

#initialize, #save_response

Methods included from Parallelism

#inherited, #supports_parallel?

Methods included from Faraday::AutoloadHelper

#all_loaded_constants, #autoload_all, #load_autoloaded_constants

Methods inherited from Middleware

dependency, inherited, #initialize, loaded?, new

Methods included from MiddlewareRegistry

#fetch_middleware, #load_middleware, #lookup_middleware, #middleware_mutex, #register_middleware

Constructor Details

This class inherits a constructor from Faraday::Adapter

Class Method Details

.setup_parallel_manager(options = {}) ⇒ Object



16
17
18
# File 'lib/faraday/adapter/em_synchrony.rb', line 16

def self.setup_parallel_manager(options = {})
  ParallelManager.new
end

Instance Method Details

#call(env) ⇒ Object



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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/faraday/adapter/em_synchrony.rb', line 20

def call(env)
  super
  request = create_request(env)

  http_method = env[:method].to_s.downcase.to_sym

  # Queue requests for parallel execution.
  if env[:parallel_manager]
    env[:parallel_manager].add(request, http_method, request_config(env)) do |resp|
      save_response(env, resp.response_header.status, resp.response) do |resp_headers|
        resp.response_header.each do |name, value|
          resp_headers[name.to_sym] = value
        end
      end

      # Finalize the response object with values from `env`.
      env[:response].finish(env)
    end

  # Execute single request.
  else
    client = nil
    block = lambda { request.send(http_method, request_config(env)) }

    if !EM.reactor_running?
      EM.run do
        Fiber.new {
          client = block.call
          EM.stop
        }.resume
      end
    else
      client = block.call
    end

    raise client.error if client.error

    status = client.response_header.status
    reason = client.response_header.http_reason
    save_response(env, status, client.response, nil, reason) do |resp_headers|
      client.response_header.each do |name, value|
        resp_headers[name.to_sym] = value
      end
    end
  end

  @app.call env
rescue Errno::ECONNREFUSED
  raise Error::ConnectionFailed, $!
rescue EventMachine::Connectify::CONNECTError => err
  if err.message.include?("Proxy Authentication Required")
    raise Error::ConnectionFailed, %{407 "Proxy Authentication Required "}
  else
    raise Error::ConnectionFailed, err
  end
rescue Errno::ETIMEDOUT => err
  raise Error::TimeoutError, err
rescue RuntimeError => err
  if err.message == "connection closed by server"
    raise Error::ConnectionFailed, err
  else
    raise
  end
rescue => err
  if defined?(OpenSSL) && OpenSSL::SSL::SSLError === err
    raise Faraday::SSLError, err
  else
    raise
  end
end

#create_request(env) ⇒ Object



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

def create_request(env)
  EventMachine::HttpRequest.new(Utils::URI(env[:url].to_s), connection_config(env).merge(@connection_options))
end