Class: Anthropic::Internal::PageCursor

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

Overview

Examples:

if page_cursor.has_next?
  page_cursor = page_cursor.next_page
end
page_cursor.auto_paging_each do |skill|
  puts(skill)
end

Generic:

  • Elem

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Type::BasePage

#to_enum

Constructor Details

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

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 PageCursor.

Parameters:



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/anthropic/internal/page_cursor.rb', line 68

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

  case page_data
  in {data: Array => data}
    @data = data.map { Anthropic::Internal::Type::Converter.coerce(@model, _1) }
  else
  end
  @has_more = page_data[:has_more]
  @next_page_ = page_data[:next_page]
end

Instance Attribute Details

#dataArray<generic<Elem>>?

Returns:

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


20
21
22
# File 'lib/anthropic/internal/page_cursor.rb', line 20

def data
  @data
end

#has_moreBoolean

Returns:



23
24
25
# File 'lib/anthropic/internal/page_cursor.rb', line 23

def has_more
  @has_more
end

#next_page_String?

Returns:

  • (String, nil)


26
27
28
# File 'lib/anthropic/internal/page_cursor.rb', line 26

def next_page_
  @next_page_
end

Instance Method Details

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

Parameters:

  • blk (Proc)

Yield Parameters:

  • (generic<Elem>)


48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/anthropic/internal/page_cursor.rb', line 48

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

  page = self
  loop do
    page.data&.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)


83
84
85
86
87
88
89
# File 'lib/anthropic/internal/page_cursor.rb', line 83

def inspect
  # rubocop:disable Layout/LineLength
  model = Anthropic::Internal::Type::Converter.inspect(@model, depth: 1)

  "#<#{self.class}[#{model}]:0x#{object_id.to_s(16)} has_more=#{has_more.inspect} next_page_=#{next_page_.inspect}>"
  # rubocop:enable Layout/LineLength
end

#next_pageself

Returns:

  • (self)

Raises:

  • (Anthropic::HTTP::Error)


35
36
37
38
39
40
41
42
43
# File 'lib/anthropic/internal/page_cursor.rb', line 35

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

  req = Anthropic::Internal::Util.deep_merge(@req, {query: {page: next_page_}})
  @client.request(req)
end

#next_page?Boolean

Returns:



29
30
31
# File 'lib/anthropic/internal/page_cursor.rb', line 29

def next_page?
  has_more
end