Class: ShafClient::Middleware::Redirect

Inherits:
Object
  • Object
show all
Defined in:
lib/shaf_client/middleware/redirect.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Redirect

Returns a new instance of Redirect.



7
8
9
# File 'lib/shaf_client/middleware/redirect.rb', line 7

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# 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) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# 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) ⇒ Object



26
27
28
29
30
31
32
# 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