Sha256: fcbca94d236a2bbc807e4af38392c57aa06a0359468f20f4b9c563a273d2db88

Contents?: true

Size: 978 Bytes

Versions: 13

Compression:

Stored size: 978 Bytes

Contents

require 'rest-core/middleware'

class RestCore::FollowRedirect
  def self.members; [:max_redirects]; end
  include RestCore::Middleware

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

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

    location = [res[RESPONSE_HEADERS]['LOCATION']].flatten.first
    meth     = if res[RESPONSE_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
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
rest-core-3.6.0 lib/rest-core/middleware/follow_redirect.rb
rest-core-3.5.92 lib/rest-core/middleware/follow_redirect.rb
rest-core-3.5.91 lib/rest-core/middleware/follow_redirect.rb
rest-core-3.5.9 lib/rest-core/middleware/follow_redirect.rb
rest-core-3.5.8 lib/rest-core/middleware/follow_redirect.rb
rest-core-3.5.7 lib/rest-core/middleware/follow_redirect.rb
rest-core-3.5.6 lib/rest-core/middleware/follow_redirect.rb
rest-core-3.5.5 lib/rest-core/middleware/follow_redirect.rb
rest-core-3.5.4 lib/rest-core/middleware/follow_redirect.rb
rest-core-3.5.3 lib/rest-core/middleware/follow_redirect.rb
rest-core-3.5.2 lib/rest-core/middleware/follow_redirect.rb
rest-core-3.5.1 lib/rest-core/middleware/follow_redirect.rb
rest-core-3.5.0 lib/rest-core/middleware/follow_redirect.rb