class Namira::Middleware::Redirector
Performs following the redirect and handling multiple redirect errors.
Constants
- REDIRECT_STATUS
The HTTP status codes
Namira
will consider a redirect
Public Class Methods
new(app)
click to toggle source
# File lib/namira/middleware/redirector.rb, line 10 def initialize(app) @app = app end
Public Instance Methods
call(env)
click to toggle source
Called by the middleware runner.
@param env [Namira::Env] The request environment
# File lib/namira/middleware/redirector.rb, line 18 def call(env) @app.call(env) rescue Errors::HTTPError => error if redirect?(error, env) handle_redirect(env, error) else raise error end end
Private Instance Methods
handle_redirect(env, error)
click to toggle source
# File lib/namira/middleware/redirector.rb, line 30 def handle_redirect(env, error) count = env.redirect_count redirect_count_error(env) if count >= max_redirect(env) location = error.response.headers['Location'] redirect_location_error(env) if location.nil? env.uri = Addressable::URI.parse(location) env.redirect_count += 1 call(env) end
max_redirect(env)
click to toggle source
# File lib/namira/middleware/redirector.rb, line 40 def max_redirect(env) env.config[:max_redirect] || 3 end
redirect?(error, env)
click to toggle source
# File lib/namira/middleware/redirector.rb, line 60 def redirect?(error, env) return false unless env.config[:follow_redirect].nil? ? true : env.config[:follow_redirect] REDIRECT_STATUS.include?(error.status) end
redirect_count_error(env)
click to toggle source
# File lib/namira/middleware/redirector.rb, line 44 def redirect_count_error(env) raise Errors::RedirectError.new( "Max number of redirects #{env.redirect_count} for #{env.uri}", env.uri.to_s, env.redirect_count ) end
redirect_location_error(env)
click to toggle source
# File lib/namira/middleware/redirector.rb, line 52 def redirect_location_error(env) raise Errors::RedirectError.new( 'Request redirected but no location was supplied', nil, env.redirect_count ) end