class RestClientWrapper::Paginator::HeaderLink
Attributes
rest_client[RW]
Public Class Methods
new(per_page: Paginate::DEFAULT_PAGINATION_PAGE_SIZE)
click to toggle source
# File lib/rest_client_wrapper/paginators/header_link.rb, line 34 def initialize(per_page: Paginate::DEFAULT_PAGINATION_PAGE_SIZE) @rest_client = nil @config = { page: nil, per_page: per_page } end
Public Instance Methods
paginate(http_method:, uri:, segment_params: {}, query_params: {}, headers: {}, data: false) { |response| ... }
click to toggle source
# File lib/rest_client_wrapper/paginators/header_link.rb, line 39 def paginate(http_method:, uri:, segment_params: {}, query_params: {}, headers: {}, data: false) raise RestClientError.new("Client not set, unable to make API call", nil, nil) unless @rest_client query_params.reverse_merge!(@config) responses = [] loop.with_index(1) do |_, page| query_params[:page] = page response = @rest_client.make_request({ http_method: http_method, uri: uri, segment_params: segment_params, query_params: query_params, headers: headers }) block_given? ? yield(response) : (responses << response) links = _pagination_links(response) break unless links.key?(:next) end return data ? responses.map(&:body).flatten : responses end
Private Instance Methods
_pagination_links(response)
click to toggle source
# File lib/rest_client_wrapper/paginators/header_link.rb, line 56 def _pagination_links(response) re_uri = "\<(.*?)\>".freeze re_rel = "current|next|first|last".freeze links_a = response&.headers&.[](:link)&.split(",") || [] links_h = {} links_a.each do |rel_link| link_parts = rel_link.split(";") next unless link_parts.length == 2 uri_match = link_parts[0].match(re_uri) rel_match = link_parts[1].match(re_rel) next if (uri_match.nil? || rel_match.nil?) || (uri_match.captures.length != 1 || rel_match.length != 1) links_h[rel_match[0]] = uri_match.captures[0] end return links_h.symbolize_keys! end