class Luchadeer::Middleware::FollowRedirects
Constants
- FOLLOW_LIMIT
Public Instance Methods
call(env)
click to toggle source
# File lib/luchadeer/middleware/follow_redirects.rb, line 10 def call(env) follow_redirect(env, FOLLOW_LIMIT) end
Private Instance Methods
follow_redirect(env, follows)
click to toggle source
# File lib/luchadeer/middleware/follow_redirects.rb, line 16 def follow_redirect(env, follows) request_body = env[:body] response = @app.call(env) response.on_complete do |env| if follow_redirect?(env, response) raise Luchadeer::Error::RedirectLimitReached, response if follows.zero? env = update_env(env, request_body, response) response = follow_redirect(env, follows - 1) end end response end
follow_redirect?(env, response)
click to toggle source
# File lib/luchadeer/middleware/follow_redirects.rb, line 32 def follow_redirect?(env, response) env[:method] == :get and [301, 302, 303, 307].include? response.status end
update_env(env, request_body, response)
click to toggle source
# File lib/luchadeer/middleware/follow_redirects.rb, line 36 def update_env(env, request_body, response) env[:url] += response['location'] env[:method] = :get [:status, :response, :response_headers].each do |key| env.delete key end env end