Class: FrederickAPI::V2::Helpers::Paginator

Inherits:
JsonApiClient::Paginating::Paginator
  • Object
show all
Defined in:
lib/frederick_api/v2/helpers/paginator.rb

Overview

Custom paginator for json api client Fixes param names for pagination Also adds ability to get all records from a paginated API

Instance Method Summary collapse

Instance Method Details

#all_recordsObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/frederick_api/v2/helpers/paginator.rb', line 13

def all_records
  current_result_set = nil
  results = self.result_set.to_a
  first_resource = self.result_set.first

  (total_pages - current_page).times do
    first_resource.class.with_headers(first_resource.custom_headers) do
      current_result_set = next_result_set(current_result_set)
      raise 'next link not found' unless current_result_set
      results.push(*current_result_set.to_a)
    end
  end
  results
end

#current_pageObject



46
47
48
# File 'lib/frederick_api/v2/helpers/paginator.rb', line 46

def current_page
  params.fetch("page.#{page_param}", 1).to_i
end

#per_pageObject



40
41
42
43
44
# File 'lib/frederick_api/v2/helpers/paginator.rb', line 40

def per_page
  params.fetch("page.#{per_page_param}") do
    result_set.length
  end.to_i
end

#total_pagesObject



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/frederick_api/v2/helpers/paginator.rb', line 28

def total_pages
  if links['last']
    uri = result_set.links.link_url_for('last')
    last_params = params_for_uri(uri)
    last_params.fetch("page.#{page_param}") do
      current_page
    end.to_i
  else
    current_page
  end
end