class Fme::Api::FaradayFollowNextLinks
Constants
- ENV_TO_CLEAR
Attributes
max_pages[R]
Public Class Methods
new(app, max_pages = nil)
click to toggle source
Calls superclass method
# File lib/fme/api/faraday_follow_next_links.rb, line 9 def initialize(app, max_pages = nil) super(app) @max_pages = max_pages end
Public Instance Methods
call(env)
click to toggle source
# File lib/fme/api/faraday_follow_next_links.rb, line 14 def call(env) @app.call(env) && return unless :get == env[:method] perform_with_next_links(env, max_pages) end
handle_response(env, request_body, response_env, max_pages_left)
click to toggle source
# File lib/fme/api/faraday_follow_next_links.rb, line 35 def handle_response(env, request_body, response_env, max_pages_left) return unless env[:body].is_a?(Array) new_request_env = update_env(response_env.dup, request_body, next_link(env)) max_pages_left -= 1 unless max_pages_left.nil? env[:body] += perform_with_next_links(new_request_env, max_pages_left).body end
next_link(env)
click to toggle source
# File lib/fme/api/faraday_follow_next_links.rb, line 31 def next_link(env) env.response.headers.fetch('x-link-next', nil) end
perform_with_next_links(env, max_pages_left)
click to toggle source
# File lib/fme/api/faraday_follow_next_links.rb, line 19 def perform_with_next_links(env, max_pages_left) request_body = env[:body] response = @app.call(env) response.on_complete do |response_env| if next_link(env).present? raise FaradayMiddleware::RedirectLimitReached, response if max_pages_left == 0 handle_response(env, request_body, response_env, max_pages_left) end end response end
update_env(env, request_body, next_link)
click to toggle source
# File lib/fme/api/faraday_follow_next_links.rb, line 42 def update_env(env, request_body, next_link) env[:url] = URI.parse(next_link) env[:body] = request_body ENV_TO_CLEAR.each { |key| env.delete key } env end