class Faraday::Adapter::Excon

Public Instance Methods

call(env) click to toggle source
# File lib/faraday/adapter/excon.rb, line 6
def call(env)
  super

  opts = {}
  if env[:url].scheme == 'https' && ssl = env[:ssl]
    opts[:ssl_verify_peer] = !!ssl.fetch(:verify, true)
    opts[:ssl_ca_path] = ssl[:ca_path] if ssl[:ca_path]
    opts[:ssl_ca_file] = ssl[:ca_file] if ssl[:ca_file]
  end

  if ( req = env[:request] )
    if req[:timeout]
      opts[:read_timeout]      = req[:timeout]
      opts[:connect_timeout]   = req[:timeout]
      opts[:write_timeout]     = req[:timeout]
    end

    if req[:open_timeout]
      opts[:connect_timeout]   = req[:open_timeout]
      opts[:write_timeout]     = req[:open_timeout]
    end
  end

  conn = ::Excon.new(env[:url].to_s, opts)

  resp = conn.request            :method  => env[:method].to_s.upcase,
    :headers => env[:request_headers],
    :body    => read_body(env)

  save_response(env, resp.status.to_i, resp.body, resp.headers)

  @app.call env
rescue ::Excon::Errors::SocketError => err
  if err.message =~ %r\btimeout\b/
    raise Error::TimeoutError, err
  else
    raise Error::ConnectionFailed, err
  end
rescue ::Excon::Errors::Timeout => err
  raise Error::TimeoutError, err
end
read_body(env) click to toggle source

TODO: support streaming requests

# File lib/faraday/adapter/excon.rb, line 50
def read_body(env)
  env[:body].respond_to?(:read) ? env[:body].read : env[:body]
end