Module: Brapi::Resources::Paginated

Included in:
Quote, V2::Fii, V2::Treasury
Defined in:
lib/brapi/resources/paginated.rb

Overview

Adds auto-pagination helpers (‘#each_page`, `#each`, plus a fast `#count` / `#size`) to a Resource around an existing list-style method.

Usage:

class Brapi::Resources::V2::Fii < Brapi::Resource
  include Brapi::Resources::Paginated
  paginates items: :fiis

  def list(**params)
    # ...
  end
end

## Requirements on the host

The list-style method named by ‘via:` (default `:list`) must accept a `page:` keyword argument. The mixin sets this on every iteration to walk through pages; if the host’s ‘list` rejects `page:` the first iteration will raise `ArgumentError`.

## Pagination shapes

Two shapes are supported:

  1. Nested (FII / Treasury): the response has a ‘pagination` sub-object with `page`, `has_next_page`, `total_items`. This is the default.

  2. Flat (Quote#list): the response exposes ‘current_page` / `has_next_page` / `item_count` directly. Pass `has_next:`, `next_page:` and `count_from:` lambdas to override the readers.

## Safety

‘max_pages:` (default `DEFAULT_MAX_PAGES`) caps any walk — protects against runaway loops if the upstream forgets to set `has_next_page = false`. The cap is checked before each fetch so `max_pages: 0` yields nothing.

## #count behaviour

When called with no args and no block, ‘#count` fetches **only the first page** and reads `pagination.total_items` (or whatever `count_from:` returns), avoiding a full walk. When called with an item or a block, it delegates to the standard `Enumerable#count` (which walks every page).

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

DEFAULT_MAX_PAGES =
10_000

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



52
53
54
55
# File 'lib/brapi/resources/paginated.rb', line 52

def self.included(base)
  base.include(Enumerable)
  base.extend(ClassMethods)
end