Class: Uc3DmpApiCore::Paginator
- Inherits:
-
Object
- Object
- Uc3DmpApiCore::Paginator
- Defined in:
- lib/uc3-dmp-api-core/paginator.rb
Overview
Use Rails’ ActiveResource to communicate with the DMPHub REST API
Constant Summary collapse
- DEFAULT_PAGE =
1
- DEFAULT_PER_PAGE =
25
- MAXIMUM_PER_PAGE =
250
Class Method Summary collapse
-
.paginate(results:, params: {}) ⇒ Object
rubocop:disable Metrics/AbcSize.
-
.pagination_meta(url:, item_count: 0, params: {}) ⇒ Object
Construct the pagination meta information that will be included in the response rubocop:disable Metrics/AbcSize.
Class Method Details
.paginate(results:, params: {}) ⇒ Object
rubocop:disable Metrics/AbcSize
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/uc3-dmp-api-core/paginator.rb', line 12 def paginate(results:, params: {}) return results unless results.is_a?(Array) && results.any? && params.is_a?(Hash) current = _current_page(item_count: results.length, params:) # Just return as is if there is only one page return results if current[:total_pages] == 1 || current[:per_page] >= results.length # Calculate the offset and extract those results offset = current[:page] == 1 ? 0 : (current[:page] - 1) * current[:per_page] results[offset, current[:per_page]] end |
.pagination_meta(url:, item_count: 0, params: {}) ⇒ Object
Construct the pagination meta information that will be included in the response rubocop:disable Metrics/AbcSize
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/uc3-dmp-api-core/paginator.rb', line 27 def (url:, item_count: 0, params: {}) prms = _current_page(item_count:, params:) hash = { page: prms[:page], per_page: prms[:per_page], total_items: item_count } return hash if prms[:total_pages] == 1 || item_count <= prms[:per_page] prv = prms[:page] - 1 nxt = prms[:page] + 1 last = prms[:total_pages] hash[:first] = _build_link(url:, target_page: 1, per_page: prms[:per_page]) if prms[:page] > 1 hash[:prev] = _build_link(url:, target_page: prv, per_page: prms[:per_page]) if prms[:page] > 1 hash[:next] = _build_link(url:, target_page: nxt, per_page: prms[:per_page]) if prms[:page] < last hash[:last] = _build_link(url:, target_page: last, per_page: prms[:per_page]) if prms[:page] < last hash.compact end |