Class: Knockapi::Internal::EntriesCursor

Inherits:
Object
  • Object
show all
Includes:
Type::BasePage
Defined in:
lib/knockapi/internal/entries_cursor.rb

Overview

Examples:

if entries_cursor.has_next?
  entries_cursor = entries_cursor.next_page
end
entries_cursor.auto_paging_each do |user|
  puts(user)
end

Defined Under Namespace

Classes: PageInfo

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Type::BasePage

#to_enum

Constructor Details

#initialize(client:, req:, headers:, page_data:) ⇒ EntriesCursor

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of EntriesCursor.

Parameters:



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/knockapi/internal/entries_cursor.rb', line 65

def initialize(client:, req:, headers:, page_data:)
  super

  case page_data
  in {entries: Array => entries}
    @entries = entries.map { Knockapi::Internal::Type::Converter.coerce(@model, _1) }
  else
  end
  case page_data
  in {page_info: Hash | nil => page_info}
    @page_info =
      Knockapi::Internal::Type::Converter.coerce(Knockapi::Internal::EntriesCursor::PageInfo, page_info)
  else
  end
end

Instance Attribute Details

#entriesArray<generic<Elem>>?

Returns:

  • (Array<generic<Elem>>, nil)


20
21
22
# File 'lib/knockapi/internal/entries_cursor.rb', line 20

def entries
  @entries
end

#page_infoPageInfo

Returns:



23
24
25
# File 'lib/knockapi/internal/entries_cursor.rb', line 23

def page_info
  @page_info
end

Instance Method Details

#auto_paging_each(&blk) {|| ... } ⇒ Object

Parameters:

  • blk (Proc)

Yield Parameters:

  • (generic<Elem>)


45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/knockapi/internal/entries_cursor.rb', line 45

def auto_paging_each(&blk)
  unless block_given?
    raise ArgumentError.new("A block must be given to ##{__method__}")
  end

  page = self
  loop do
    page.entries&.each(&blk)

    break unless page.next_page?
    page = page.next_page
  end
end

#inspectString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (String)


84
85
86
87
88
# File 'lib/knockapi/internal/entries_cursor.rb', line 84

def inspect
  model = Knockapi::Internal::Type::Converter.inspect(@model, depth: 1)

  "#<#{self.class}[#{model}]:0x#{object_id.to_s(16)}>"
end

#next_pageself

Returns:

  • (self)

Raises:

  • (Knockapi::HTTP::Error)


32
33
34
35
36
37
38
39
40
# File 'lib/knockapi/internal/entries_cursor.rb', line 32

def next_page
  unless next_page?
    message = "No more pages available. Please check #next_page? before calling ##{__method__}"
    raise RuntimeError.new(message)
  end

  req = Knockapi::Internal::Util.deep_merge(@req, {query: {after: page_info&.after}})
  @client.request(req)
end

#next_page?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/knockapi/internal/entries_cursor.rb', line 26

def next_page?
  !entries.to_a.empty? && !page_info&.after.to_s.empty?
end