module ZendeskAPI::Collection::Pagination

Contains all methods related to pagination in an attempt to slim down collection.rb

Constants

DEFAULT_PAGE_SIZE

Public Instance Methods

first_page?() click to toggle source
# File lib/zendesk_api/pagination.rb, line 27
def first_page?
  !@prev_page
end
has_more_results?(response)
Alias for: more_results?
last_page?() click to toggle source
# File lib/zendesk_api/pagination.rb, line 31
def last_page?
  !@next_page || @next_page == @query
end
more_results?(response) click to toggle source
# File lib/zendesk_api/pagination.rb, line 6
def more_results?(response)
  Helpers.present?(response["meta"]) && response["meta"]["has_more"]
end
Also aliased as: has_more_results?
page(number) click to toggle source

Changes the page option. Returns self, so it can be chained. No execution. @return [Collection] self

# File lib/zendesk_api/pagination.rb, line 21
def page(number)
  clear_cache if number
  @options["page"] = number
  self
end
per_page(count) click to toggle source

Changes the per_page option. Returns self, so it can be chained. No execution. @return [Collection] self

# File lib/zendesk_api/pagination.rb, line 13
def per_page(count)
  clear_cache if count
  @options["per_page"] = count
  self
end

Private Instance Methods

cbp_request?() click to toggle source

CBP requests look like: ‘/resources?page=100` OBP requests look like: `/resources?page=2`

# File lib/zendesk_api/pagination.rb, line 57
def cbp_request?
  @options["page"].is_a?(Hash)
end
cbp_response?(body) click to toggle source
# File lib/zendesk_api/pagination.rb, line 45
def cbp_response?(body)
  !!(body["meta"] && body["links"])
end
first_cbp_request?() click to toggle source
# File lib/zendesk_api/pagination.rb, line 69
def first_cbp_request?
  # @next_page will be nil when making the first cbp request
  @next_page.nil?
end
intentional_obp_request?() click to toggle source
# File lib/zendesk_api/pagination.rb, line 61
def intentional_obp_request?
  Helpers.present?(@options["page"]) && !cbp_request?
end
set_cbp_options() click to toggle source
# File lib/zendesk_api/pagination.rb, line 49
def set_cbp_options
  @options_per_page_was = @options.delete("per_page")
  # Default to CBP by using the page param as a map
  @options.page = { size: (@options_per_page_was || DEFAULT_PAGE_SIZE) }
end
set_cbp_response_options(body) click to toggle source
# File lib/zendesk_api/pagination.rb, line 87
def set_cbp_response_options(body)
  @options.page = {} unless cbp_request?
  # the line above means an intentional CBP request where page[size] is passed on the query
  # this is to cater for CBP responses where we don't specify page[size] but the endpoint
  # responds CBP by default. i.e  `client.trigger_categories.fetch`
  @options.page.merge!(
    before: body["meta"]["before_cursor"],
    after: body["meta"]["after_cursor"]
                      )
end
set_page_and_count(body) click to toggle source
# File lib/zendesk_api/pagination.rb, line 74
def set_page_and_count(body)
  @count = (body["count"] || @resources.size).to_i
  @next_page, @prev_page = page_links(body)

  if cbp_response?(body)
    set_cbp_response_options(body)
  elsif @next_page =~ /page=(\d+)/
    @options["page"] = Regexp.last_match(1).to_i - 1
  elsif @prev_page =~ /page=(\d+)/
    @options["page"] = Regexp.last_match(1).to_i + 1
  end
end
supports_cbp?() click to toggle source
# File lib/zendesk_api/pagination.rb, line 65
def supports_cbp?
  @resource_class.cbp_path_regexes.any? { |supported_path_regex| path.match?(supported_path_regex) }
end