Class: FinchAPI::SinglePage

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

Overview

Examples:

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

payments => Array

Instance Method Summary collapse

Methods included from Type::BasePage

#to_enum

Constructor Details

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

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

Parameters:



34
35
36
37
38
39
40
41
42
43
# File 'lib/finch-api/single_page.rb', line 34

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

  case page_data
  in Array
    replace(page_data.map { FinchAPI::Type::Converter.coerce(model, _1) })
  else
  end
end

Instance Method Details

#auto_paging_each(&blk) ⇒ Object

Parameters:

  • blk (Proc)


57
58
59
60
61
62
63
64
65
66
67
# File 'lib/finch-api/single_page.rb', line 57

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

#inspectString

Returns:

  • (String)


70
71
72
# File 'lib/finch-api/single_page.rb', line 70

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

#next_pageFinchAPI::SinglePage

Returns:

Raises:

  • (FinchAPI::HTTP::Error)


52
53
54
# File 'lib/finch-api/single_page.rb', line 52

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

#next_page?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/finch-api/single_page.rb', line 46

def next_page?
  false
end