Sha256: 2fd7cb92f3938e7b2f18dd62a97cc2cd7ac1004be8ace4f6bf602c020376c09f

Contents?: true

Size: 1.94 KB

Versions: 9

Compression:

Stored size: 1.94 KB

Contents

module Faraday
  class Adapter
    class Patron < Faraday::Adapter
      dependency 'patron'

      def initialize(app, &block)
        super(app)
        @block = block if block_given?
      end

      def call(env)
        super

        # TODO: support streaming requests
        env[:body] = env[:body].read if env[:body].respond_to? :read

        session = @session ||= create_session

        if req = env[:request]
          session.timeout = session.connect_timeout = req[:timeout] if req[:timeout]
          session.connect_timeout = req[:open_timeout]              if req[:open_timeout]
                    
          if proxy = req[:proxy]
            session.proxy = proxy[:uri].to_s
            if proxy[:user] && proxy[:password]
              prepend_proxy_auth_string(proxy, session)
            end
          end
        end

        response = begin
          data = env[:body] ? env[:body].to_s : nil
          session.request(env[:method], env[:url].to_s, env[:request_headers], :data => data)
        rescue Errno::ECONNREFUSED
          raise Error::ConnectionFailed, $!
        end

        save_response(env, response.status, response.body, response.headers)

        @app.call env
      rescue ::Patron::TimeoutError => err
        raise Faraday::Error::TimeoutError, err
      end

      if loaded? && defined?(::Patron::Request::VALID_ACTIONS)
        # HAX: helps but doesn't work completely
        # https://github.com/toland/patron/issues/34
        ::Patron::Request::VALID_ACTIONS.tap do |actions|
          actions << :patch unless actions.include? :patch
          actions << :options unless actions.include? :options
        end
      end

      def create_session
        session = ::Patron::Session.new
        session.insecure = true
        @block.call(session) if @block
        session
      end
    end
    
    def prepend_proxy_auth_string(proxy, session)
      session.proxy.insert(7, "#{proxy[:user]}:#{proxy[:password]}@")
    end
  end
end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
faraday-0.9.0.rc5 lib/faraday/adapter/patron.rb
faraday-0.9.0.rc4 lib/faraday/adapter/patron.rb
faraday-0.9.0.rc3 lib/faraday/adapter/patron.rb
faraday-0.9.0.rc2 lib/faraday/adapter/patron.rb
faraday-0.9.0.rc1 lib/faraday/adapter/patron.rb
faraday-0.8.7 lib/faraday/adapter/patron.rb
vagrant-actionio-0.0.9 vendor/bundle/gems/faraday-0.8.6/lib/faraday/adapter/patron.rb
faraday-0.8.6 lib/faraday/adapter/patron.rb
faraday-0.8.5 lib/faraday/adapter/patron.rb