Class: FaradayStack::FollowRedirects

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/faraday_stack/follow_redirects.rb

Constant Summary collapse

REDIRECTS =

TODO: 307

[301, 302, 303]
FOLLOW_LIMIT =

default value for max redirects followed

3

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ FollowRedirects

Returns a new instance of FollowRedirects.



17
18
19
20
21
# File 'lib/faraday_stack/follow_redirects.rb', line 17

def initialize(app, options = {})
  super(app)
  @options = options
  @follow_limit = options[:limit] || FOLLOW_LIMIT
end

Instance Method Details

#call(env) ⇒ Object



23
24
25
# File 'lib/faraday_stack/follow_redirects.rb', line 23

def call(env)
  process_response(@app.call(env), @follow_limit)
end

#process_response(response, follows) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/faraday_stack/follow_redirects.rb', line 27

def process_response(response, follows)
  response.on_complete do |env|
    if redirect? response
      raise RedirectLimitReached, response if follows.zero?
      env[:url] += response['location']
      env[:method] = :get
      response = process_response(@app.call(env), follows - 1)
    end
  end
  response
end

#redirect?(response) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/faraday_stack/follow_redirects.rb', line 39

def redirect?(response)
  REDIRECTS.include? response.status
end