Class: AhoSdk::CursorPage

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

Overview

Cursor-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:) ⇒ CursorPage

Returns a new instance of CursorPage.

Parameters:

  • data (Array<Hash>)

    Items on this page

  • meta (Hash)

    Pagination metadata (cursor, has_more, next_cursor)

  • fetch_next (Proc)

    Lambda to fetch the next page given a cursor



29
30
31
32
33
# File 'lib/aho_sdk/cursor_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/cursor_page.rb', line 24

def data
  @data
end

#metaObject (readonly)

Returns the value of attribute meta.



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

def meta
  @meta
end

Instance Method Details

#cursorString?

Returns Cursor for the current page (if provided).

Returns:

  • (String, nil)

    Cursor for the current page (if provided)



71
72
73
# File 'lib/aho_sdk/cursor_page.rb', line 71

def cursor
  meta[:cursor] || meta["cursor"]
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/cursor_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

#has_more?Boolean

Returns true if there are more pages.

Returns:

  • (Boolean)

    true if there are more pages



61
62
63
# File 'lib/aho_sdk/cursor_page.rb', line 61

def has_more?
  meta[:has_more] || meta["has_more"] || false
end

#next_cursorString?

Returns Cursor for the next page.

Returns:

  • (String, nil)

    Cursor for the next page



66
67
68
# File 'lib/aho_sdk/cursor_page.rb', line 66

def next_cursor
  meta[:next_cursor] || meta["next_cursor"]
end

#next_pageCursorPage?

Fetch the next page

Returns:

  • (CursorPage, nil)

    Next page or nil if this is the last page



51
52
53
54
55
56
57
58
# File 'lib/aho_sdk/cursor_page.rb', line 51

def next_page
  return nil unless has_more?

  cursor = next_cursor
  return nil if cursor.nil?

  @fetch_next.call(cursor)
end

#per_pageInteger

Returns Number of items per page (if provided).

Returns:

  • (Integer)

    Number of items per page (if provided)



76
77
78
# File 'lib/aho_sdk/cursor_page.rb', line 76

def per_page
  meta[:per_page] || meta["per_page"]
end

#sizeInteger Also known as: length

Returns Number of items on this page.

Returns:

  • (Integer)

    Number of items on this page



81
82
83
# File 'lib/aho_sdk/cursor_page.rb', line 81

def size
  data.size
end