Class: GoCardless::Paginator

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/gocardless/paginator.rb

Constant Summary collapse

DEFAULT_PAGE_NUMBER =
1
DEFAULT_PAGE_SIZE =
30

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, resource_class, path, query) ⇒ Paginator

Returns a new instance of Paginator.


14
15
16
17
18
19
# File 'lib/gocardless/paginator.rb', line 14

def initialize(client, resource_class, path, query)
  @client = client
  @resource_class = resource_class
  @path, @query = path, query
  @page_number, @per_page = DEFAULT_PAGE_NUMBER, DEFAULT_PAGE_SIZE
end

Instance Attribute Details

#num_pagesObject (readonly)

Returns the value of attribute num_pages


9
10
11
# File 'lib/gocardless/paginator.rb', line 9

def num_pages
  @num_pages
end

#num_recordsObject (readonly)

Returns the value of attribute num_records


8
9
10
# File 'lib/gocardless/paginator.rb', line 8

def num_records
  @num_records
end

Instance Method Details

#countObject

Return the total number of records. May trigger an HTTP request.


65
66
67
68
# File 'lib/gocardless/paginator.rb', line 65

def count
  load_page(1) if @num_records.nil?  # load pagination metadata
  @num_records
end

#each(&block) ⇒ Object

Yield every record from every current page, auto-fetching new pages as the iteration happens. Records are returned as instances of the appropriate resource classes (e.g. Subscription).


50
51
52
# File 'lib/gocardless/paginator.rb', line 50

def each(&block)
  each_page { |page| page.each(&block) }
end

#each_pageObject

Fetch and yield each page of results.


55
56
57
58
59
60
61
62
# File 'lib/gocardless/paginator.rb', line 55

def each_page
  page_obj = load_page(1)
  loop do
    yield page_obj
    break unless page_obj.has_next?
    page_obj = load_page(page_obj.next_page)
  end
end

#load_page(page_num) ⇒ Object Also known as: page

Fetch and return a single page.


35
36
37
38
39
40
41
42
43
# File 'lib/gocardless/paginator.rb', line 35

def load_page(page_num)
  params = @query.merge(pagination_params(page_num))
  response = @client.api_request(:get, @path, :params => params)

   = (response)
  @num_records, @num_pages = ['records'], ['pages']

  Page.new(@resource_class, response.parsed, ['links'])
end

#page_countObject

Return the total number of pages. May trigger an HTTP request.


71
72
73
74
# File 'lib/gocardless/paginator.rb', line 71

def page_count
  load_page(1) if @num_records.nil?  # load pagination metadata
  @num_pages
end

#per_page(results_per_page = nil) ⇒ Object

Set the number of records per page (page size), if an argument is provided. Returns the current per_page value, whether an argument is provided or not.


24
25
26
27
28
29
30
31
32
# File 'lib/gocardless/paginator.rb', line 24

def per_page(results_per_page = nil)
  if results_per_page
    @num_records, @num_pages = nil, nil
    @per_page = results_per_page
    self
  else
    @per_page
  end
end