class Faraday::Adapter::EMSynchrony

EventMachine Synchrony adapter.

Public Class Methods

setup_parallel_manager(_options = nil) click to toggle source

@return [ParallelManager]

# File lib/faraday/adapter/em_synchrony.rb, line 20
def self.setup_parallel_manager(_options = nil)
  ParallelManager.new
end

Public Instance Methods

call(env) click to toggle source
Calls superclass method Faraday::Adapter#call
# File lib/faraday/adapter/em_synchrony.rb, line 24
def call(env)
  super
  request = create_request(env)

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

  if env[:parallel_manager]
    # Queue requests for parallel execution.
    execute_parallel_request(env, request, http_method)
  else
    # Execute single request.
    execute_single_request(env, request, http_method)
  end

  @app.call env
rescue Errno::ECONNREFUSED
  raise Faraday::ConnectionFailed, $ERROR_INFO
rescue EventMachine::Connectify::CONNECTError => e
  if e.message.include?('Proxy Authentication Required')
    raise Faraday::ConnectionFailed,
          %(407 "Proxy Authentication Required")
  end

  raise Faraday::ConnectionFailed, e
rescue Errno::ETIMEDOUT => e
  raise Faraday::TimeoutError, e
rescue RuntimeError => e
  if e.message == 'connection closed by server'
    raise Faraday::ConnectionFailed, e
  end

  raise Faraday::TimeoutError, e if e.message.include?('timeout error')

  raise
rescue StandardError => e
  if defined?(OpenSSL) && e.is_a?(OpenSSL::SSL::SSLError)
    raise Faraday::SSLError, e
  end

  raise
end
create_request(env) click to toggle source
# File lib/faraday/adapter/em_synchrony.rb, line 66
def create_request(env)
  EventMachine::HttpRequest.new(
    Utils::URI(env[:url].to_s),
    connection_config(env).merge(@connection_options)
  )
end

Private Instance Methods

call_block(block) click to toggle source
# File lib/faraday/adapter/em_synchrony.rb, line 119
def call_block(block)
  client = nil

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

  client
end
execute_parallel_request(env, request, http_method) click to toggle source
# File lib/faraday/adapter/em_synchrony.rb, line 75
def execute_parallel_request(env, request, http_method)
  env[:parallel_manager].add(request, http_method,
                             request_config(env)) do |resp|
    if (req = env[:request]).stream_response?
      warn "Streaming downloads for #{self.class.name} " \
          'are not yet implemented.'
      req.on_data.call(resp.response, resp.response.bytesize)
    end

    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
end
execute_single_request(env, request, http_method) click to toggle source
# File lib/faraday/adapter/em_synchrony.rb, line 96
def execute_single_request(env, request, http_method)
  block = -> { request.send(http_method, request_config(env)) }
  client = call_block(block)

  raise client.error if client&.error

  if env[:request].stream_response?
    warn "Streaming downloads for #{self.class.name} " \
        'are not yet implemented.'
    env[:request].on_data.call(
      client.response,
      client.response.bytesize
    )
  end
  status = client.response_header.status
  reason = client.response_header.http_reason
  save_response(env, status, client.response, nil, reason) do |headers|
    client.response_header.each do |name, value|
      headers[name.to_sym] = value
    end
  end
end