Class: AhoSdk::Page

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/aho_sdk/page.rb

Overview

Paginated response with lazy iteration support

Examples:

Iterate through all pages

page = issuer.credentials.list
page.each do |credential|
  puts credential[:uuid]
end

Manual pagination

page = issuer.credentials.list
while page
  page.data.each { |c| puts c[:uuid] }
  page = page.next_page
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data:, meta:, fetch_next:) ⇒ Page

Returns a new instance of Page.

Parameters:

  • data (Array<Hash>)

    Items on this page

  • meta (Hash)

    Pagination metadata (current_page, per_page, total_count, total_pages)

  • fetch_next (Proc)

    Lambda to fetch the next page



29
30
31
32
33
# File 'lib/aho_sdk/page.rb', line 29

def initialize(data:, meta:, fetch_next:)
  @data = data
  @meta = meta
  @fetch_next = fetch_next
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



24
25
26
# File 'lib/aho_sdk/page.rb', line 24

def data
  @data
end

#metaObject (readonly)

Returns the value of attribute meta.



24
25
26
# File 'lib/aho_sdk/page.rb', line 24

def meta
  @meta
end

Instance Method Details

#current_pageInteger

Returns Current page number.

Returns:

  • (Integer)

    Current page number



63
64
65
# File 'lib/aho_sdk/page.rb', line 63

def current_page
  meta[:current_page]
end

#each {|Hash| ... } ⇒ Enumerator

Iterate through all items across all pages (lazy loading)

Yields:

  • (Hash)

    Each item

Returns:

  • (Enumerator)

    if no block given



38
39
40
41
42
43
44
45
46
47
# File 'lib/aho_sdk/page.rb', line 38

def each(&block)
  return enum_for(:each) unless block_given?

  page = self
  loop do
    page.data.each(&block)
    page = page.next_page
    break if page.nil?
  end
end

#last_page?Boolean

Returns true if this is the last page.

Returns:

  • (Boolean)

    true if this is the last page



58
59
60
# File 'lib/aho_sdk/page.rb', line 58

def last_page?
  meta[:current_page] >= meta[:total_pages]
end

#next_pagePage?

Fetch the next page

Returns:

  • (Page, nil)

    Next page or nil if this is the last page



51
52
53
54
55
# File 'lib/aho_sdk/page.rb', line 51

def next_page
  return nil if last_page?

  @fetch_next.call(meta[:current_page] + 1)
end

#per_pageInteger

Returns Number of items per page.

Returns:

  • (Integer)

    Number of items per page



78
79
80
# File 'lib/aho_sdk/page.rb', line 78

def per_page
  meta[:per_page]
end

#total_countInteger

Returns Total number of items across all pages.

Returns:

  • (Integer)

    Total number of items across all pages



73
74
75
# File 'lib/aho_sdk/page.rb', line 73

def total_count
  meta[:total_count]
end

#total_pagesInteger

Returns Total number of pages.

Returns:

  • (Integer)

    Total number of pages



68
69
70
# File 'lib/aho_sdk/page.rb', line 68

def total_pages
  meta[:total_pages]
end