Sha256: 709e96b507f51d17c94cbdf12223734910a9397b859f4d33009ccd0b968c24eb

Contents?: true

Size: 1.58 KB

Versions: 2

Compression:

Stored size: 1.58 KB

Contents

require 'timeout'

module Faraday
  class Adapter
    # Sends requests to a Rack app.
    #
    # Examples
    #
    #   class MyRackApp
    #     def call(env)
    #       [200, {'Content-Type' => 'text/html'}, ["hello world"]]
    #     end
    #   end
    #
    #   Faraday.new do |conn|
    #     conn.adapter :rack, MyRackApp.new
    #   end
    class Rack < Faraday::Adapter
      dependency 'rack/test'

      # not prefixed with "HTTP_"
      SPECIAL_HEADERS = %w[ CONTENT_LENGTH CONTENT_TYPE ]

      def initialize(faraday_app, rack_app)
        super(faraday_app)
        mock_session = ::Rack::MockSession.new(rack_app)
        @session     = ::Rack::Test::Session.new(mock_session)
      end

      def call(env)
        super
        rack_env = {
          :method => env[:method],
          :input  => env[:body].respond_to?(:read) ? env[:body].read : env[:body]
        }

        env[:request_headers].each do |name, value|
          name = name.upcase.tr('-', '_')
          name = "HTTP_#{name}" unless SPECIAL_HEADERS.include? name
          rack_env[name] = value
        end if env[:request_headers]

        timeout  = env[:request][:timeout] || env[:request][:open_timeout]
        response = if timeout
          Timer.timeout(timeout, Faraday::Error::TimeoutError) { execute_request(env, rack_env) }
        else
          execute_request(env, rack_env)
        end

        save_response(env, response.status, response.body, response.headers)
        @app.call env
      end

      def execute_request(env, rack_env)
        @session.request(env[:url].to_s, rack_env)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
faraday-0.8.4 lib/faraday/adapter/rack.rb
faraday-0.8.2 lib/faraday/adapter/rack.rb