Class: Faraday::Adapter::Rack

Inherits:
Faraday::Adapter show all
Defined in:
lib/faraday/adapter/rack.rb

Overview

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
end

Constant Summary collapse

SPECIAL_HEADERS =

not prefixed with “HTTP_”

%w[ CONTENT_LENGTH CONTENT_TYPE ]

Constants inherited from Faraday::Adapter

CONTENT_LENGTH

Instance Attribute Summary

Attributes included from Parallelism

#supports_parallel

Instance Method Summary collapse

Methods inherited from Faraday::Adapter

adapter?, #prepend_proxy_auth_string, #save_response

Methods included from Faraday::AutoloadHelper

#all_loaded_constants, #autoload_all, #load_autoloaded_constants

Methods included from MiddlewareRegistry

#lookup_middleware, #register_middleware

Methods included from Parallelism

#inherited, #supports_parallel?

Methods inherited from Middleware

adapter?, dependency, inherited, loaded?, new

Constructor Details

#initialize(faraday_app, rack_app) ⇒ Rack

Returns a new instance of Rack.



22
23
24
25
26
# File 'lib/faraday/adapter/rack.rb', line 22

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

Instance Method Details

#call(env) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/faraday/adapter/rack.rb', line 28

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

#execute_request(env, rack_env) ⇒ Object



52
53
54
# File 'lib/faraday/adapter/rack.rb', line 52

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