Class: FaradayStack::FollowRedirects
- Inherits:
-
Faraday::Middleware
- Object
- Faraday::Middleware
- FaradayStack::FollowRedirects
- Defined in:
- lib/faraday_stack/follow_redirects.rb
Constant Summary collapse
- REDIRECTS =
[301, 302, 303, 307]
- FOLLOW_LIMIT =
default value for max redirects followed
3
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, options = {}) ⇒ FollowRedirects
constructor
A new instance of FollowRedirects.
- #process_response(response, follows) ⇒ Object
- #redirect?(response) ⇒ Boolean
Constructor Details
#initialize(app, options = {}) ⇒ FollowRedirects
Returns a new instance of FollowRedirects.
16 17 18 19 20 |
# File 'lib/faraday_stack/follow_redirects.rb', line 16 def initialize(app, = {}) super(app) @options = @follow_limit = [:limit] || FOLLOW_LIMIT end |
Instance Method Details
#call(env) ⇒ Object
22 23 24 |
# File 'lib/faraday_stack/follow_redirects.rb', line 22 def call(env) process_response(@app.call(env), @follow_limit) end |
#process_response(response, follows) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/faraday_stack/follow_redirects.rb', line 26 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
38 39 40 |
# File 'lib/faraday_stack/follow_redirects.rb', line 38 def redirect?(response) REDIRECTS.include? response.status end |