Sha256: cacc32139cd05a9913c984d42dbcd8f448a13d3e781e1966250ca626d5c4409c

Contents?: true

Size: 1.63 KB

Versions: 1

Compression:

Stored size: 1.63 KB

Contents

module Faraday
  class Adapter
    class Excon < Faraday::Adapter
      dependency 'excon'

      def initialize(app, connection_options = {})
        @connection_options = connection_options
        super(app)
      end

      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_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.merge(@connection_options))

        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 =~ /\btimeout\b/
          raise Error::TimeoutError, err
        else
          raise Error::ConnectionFailed, err
        end
      rescue ::Excon::Errors::Timeout => err
        raise Error::TimeoutError, err
      end

      # TODO: support streaming requests
      def read_body(env)
        env[:body].respond_to?(:read) ? env[:body].read : env[:body]
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
avdi-faraday-0.8.1 lib/faraday/adapter/excon.rb