Class: Anthropic::Internal::Page

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

Overview

Examples:

if page.has_next?
  page = page.next_page
end
page.auto_paging_each do |batch|
  puts(batch)
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Type::BasePage

#to_enum

Constructor Details

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

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

Parameters:



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/anthropic/internal/page.rb', line 74

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]
  @first_id = page_data[:first_id]
  @last_id = page_data[:last_id]
end

Instance Attribute Details

#dataArray<generic<Elem>>?

Returns:

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


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

def data
  @data
end

#first_idString?

Returns:

  • (String, nil)


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

def first_id
  @first_id
end

#has_moreBoolean

Returns:



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

def has_more
  @has_more
end

#last_idString?

Returns:

  • (String, nil)


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

def last_id
  @last_id
end

Instance Method Details

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

Parameters:

  • blk (Proc)

Yield Parameters:

  • (generic<Elem>)


54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/anthropic/internal/page.rb', line 54

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)


90
91
92
93
94
95
96
# File 'lib/anthropic/internal/page.rb', line 90

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} first_id=#{first_id.inspect} last_id=#{last_id.inspect}>"
  # rubocop:enable Layout/LineLength
end

#next_pageself

Returns:

  • (self)

Raises:

  • (Anthropic::HTTP::Error)


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

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: first_id.nil? ? {after_id: last_id} : {before_id: first_id}}
  )
  @client.request(req)
end

#next_page?Boolean

Returns:



32
33
34
# File 'lib/anthropic/internal/page.rb', line 32

def next_page?
  has_more
end