Module: Crawlora::Pagination

Defined in:
lib/crawlora/pagination.rb

Overview

Pagination helpers shared by the client’s #paginate / #paginate_items.

Constant Summary collapse

PAGE_PARAM_NAMES =
%w[page offset].freeze

Class Method Summary collapse

Class Method Details

.default_items(response) ⇒ Object

Default item extractor: the response’s ‘data` list (Crawlora envelope), or the response itself when it is already an array.



32
33
34
35
36
37
# File 'lib/crawlora/pagination.rb', line 32

def default_items(response)
  return response["data"] if response.is_a?(Hash) && response["data"].is_a?(Array)
  return response if response.is_a?(Array)

  []
end

.default_start(page_param) ⇒ Object



26
27
28
# File 'lib/crawlora/pagination.rb', line 26

def default_start(page_param)
  page_param == "offset" ? 0 : 1
end

.detect_page_param(operation) ⇒ Object

First page/offset query parameter an operation exposes, or nil.



11
12
13
14
# File 'lib/crawlora/pagination.rb', line 11

def detect_page_param(operation)
  names = (operation["queryParams"] || []).map { |p| p["name"] }
  PAGE_PARAM_NAMES.find { |candidate| names.include?(candidate) }
end

.page_empty?(response) ⇒ Boolean

A page is empty when its ‘data` array (Crawlora envelope) or the page itself is empty/blank.

Returns:

  • (Boolean)


18
19
20
21
22
23
24
# File 'lib/crawlora/pagination.rb', line 18

def page_empty?(response)
  data = response.is_a?(Hash) && response.key?("data") ? response["data"] : response
  return true if data.nil?
  return data.empty? if data.respond_to?(:empty?)

  !data
end