Class: RestCore::FollowRedirect

Inherits:
Object
  • Object
show all
Includes:
Middleware
Defined in:
lib/rest-core/middleware/follow_redirect.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.membersObject



4
# File 'lib/rest-core/middleware/follow_redirect.rb', line 4

def self.members; [:max_redirects]; end

Instance Method Details

#call(env, &k) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/rest-core/middleware/follow_redirect.rb', line 7

def call env, &k
  if env[DRY]
    app.call(env, &k)
  else
    app.call(env){ |res| process(res, k) }
  end
end

#process(res, k) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rest-core/middleware/follow_redirect.rb', line 15

def process res, k
  return k.call(res) if max_redirects(res) <= 0
  status = res[RESPONSE_STATUS]
  return k.call(res) if ![301,302,303,307].include?(status)
  return k.call(res) if  [301,302    ,307].include?(status) &&
                        ![:get, :head    ].include?(res[REQUEST_METHOD])

  location = [res[RESPONSE_HEADERS]['LOCATION']].flatten.first
  meth     = if status == 303
               :get
             else
               res[REQUEST_METHOD]
             end

  give_promise(call(res.merge(
    REQUEST_METHOD => meth    ,
    REQUEST_PATH   => location,
    REQUEST_QUERY  => {}      ,
    'max_redirects' => max_redirects(res) - 1), &k))
end