Class: Rack::Client::FollowRedirects

Inherits:
Object
  • Object
show all
Includes:
Test::Methods
Defined in:
lib/rack/client/follow_redirects.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ FollowRedirects

Returns a new instance of FollowRedirects.



4
5
6
# File 'lib/rack/client/follow_redirects.rb', line 4

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rack/client/follow_redirects.rb', line 8

def call(env)
  status, headers, body = @app.call(env)
  response = Rack::Response.new(body, status, headers)
  if response.redirect?
    uri = URI.parse(response["Location"])
    new_env = {}
    env.each do |k,v|
      if %w| HTTP_HOST SERVER_NAME SERVER_PORT |.include?(k)
        new_env[k] = v
      end
    end
    new_env["REQUEST_METHOD"] = "GET"
    session = Rack::Test::Session.new(@app)
    env = session.send(:env_for, uri.to_s, new_env)
    call(env)
  else
    [status, headers, body]
  end
end