class ShafClient::Middleware::Redirect

Public Class Methods

new(app) click to toggle source
# File lib/shaf_client/middleware/redirect.rb, line 7
def initialize(app)
  @app = app
end

Public Instance Methods

call(env) click to toggle source
# File lib/shaf_client/middleware/redirect.rb, line 11
def call(env)
  @app.call(env).on_complete do
    status = env[:status]
    location = env[:response_headers]['Location']
    next unless redirect? status
    next unless location
    update_env(env, status, location)
    @app.call(env)
  end
end
redirect?(status) click to toggle source
# File lib/shaf_client/middleware/redirect.rb, line 22
def redirect?(status)
  [301, 302, 303, 307, 308].include? status
end
update_env(env, status, location) click to toggle source
# File lib/shaf_client/middleware/redirect.rb, line 26
def update_env(env, status, location)
  if status == 303
    env[:method] = :get
    env[:body] = nil
  end
  env[:url] = URI(location)
end