Class: FinchAPI::ResponsesPage

Inherits:
Object
  • Object
show all
Includes:
Type::BasePage
Defined in:
lib/finch-api/responses_page.rb

Overview

Examples:

if responses_page.has_next?
  responses_page = responses_page.next_page
end
responses_page.auto_paging_each do |individual|
  puts(individual)
end
individuals =
  responses_page
  .to_enum
  .lazy
  .select { _1.object_id.even? }
  .map(&:itself)
  .take(2)
  .to_a

individuals => Array

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Type::BasePage

#to_enum

Constructor Details

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

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

Parameters:



37
38
39
40
41
42
43
44
45
46
# File 'lib/finch-api/responses_page.rb', line 37

def initialize(client:, req:, headers:, page_data:)
  super
  model = req.fetch(:model)

  case page_data
  in {responses: Array | nil => responses}
    @responses = responses&.map { FinchAPI::Type::Converter.coerce(model, _1) }
  else
  end
end

Instance Attribute Details

#responsesArray<Object>?

Returns:

  • (Array<Object>, nil)


29
30
31
# File 'lib/finch-api/responses_page.rb', line 29

def responses
  @responses
end

Instance Method Details

#auto_paging_each(&blk) ⇒ Object

Parameters:

  • blk (Proc)


60
61
62
63
64
65
66
67
68
69
70
# File 'lib/finch-api/responses_page.rb', line 60

def auto_paging_each(&blk)
  unless block_given?
    raise ArgumentError.new("A block must be given to ##{__method__}")
  end
  page = self
  loop do
    page.responses&.each { blk.call(_1) }
    break unless page.next_page?
    page = page.next_page
  end
end

#inspectString

Returns:

  • (String)


73
74
75
# File 'lib/finch-api/responses_page.rb', line 73

def inspect
  "#<#{self.class}:0x#{object_id.to_s(16)} responses=#{responses.inspect}>"
end

#next_pageFinchAPI::ResponsesPage

Returns:

Raises:

  • (FinchAPI::HTTP::Error)


55
56
57
# File 'lib/finch-api/responses_page.rb', line 55

def next_page
  RuntimeError.new("No more pages available.")
end

#next_page?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/finch-api/responses_page.rb', line 49

def next_page?
  false
end