Class: Billrb::ListPage
- Inherits:
-
Object
- Object
- Billrb::ListPage
- Includes:
- Enumerable
- Defined in:
- lib/billrb/list_page.rb
Overview
One page of a list endpoint response. BILL paginates with ‘max`/`page` query params and returns `results` plus `nextPage`/`prevPage` tokens.
Instance Attribute Summary collapse
-
#next_page ⇒ Object
readonly
Returns the value of attribute next_page.
-
#prev_page ⇒ Object
readonly
Returns the value of attribute prev_page.
-
#results ⇒ Object
readonly
Returns the value of attribute results.
Instance Method Summary collapse
-
#auto_paging_each(&block) ⇒ Object
Iterates over every result, fetching subsequent pages as needed.
- #each ⇒ Object
- #fetch_next_page ⇒ Object
-
#initialize(resource_class, data, params: {}, client: Billrb.client, list_args: []) ⇒ ListPage
constructor
list_args carries leading positional arguments for nested resources (e.g. the customer id for CustomerBankAccount.list) so pagination can replay the same list call.
- #next_page? ⇒ Boolean
Constructor Details
#initialize(resource_class, data, params: {}, client: Billrb.client, list_args: []) ⇒ ListPage
list_args carries leading positional arguments for nested resources (e.g. the customer id for CustomerBankAccount.list) so pagination can replay the same list call.
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/billrb/list_page.rb', line 14 def initialize(resource_class, data, params: {}, client: Billrb.client, list_args: []) @resource_class = resource_class @client = client @params = params @list_args = list_args data = {} unless data.is_a?(Hash) @results = Array(data["results"]).map { |attrs| resource_class.new(attrs) } @next_page = data["nextPage"] @prev_page = data["prevPage"] end |
Instance Attribute Details
#next_page ⇒ Object (readonly)
Returns the value of attribute next_page.
9 10 11 |
# File 'lib/billrb/list_page.rb', line 9 def next_page @next_page end |
#prev_page ⇒ Object (readonly)
Returns the value of attribute prev_page.
9 10 11 |
# File 'lib/billrb/list_page.rb', line 9 def prev_page @prev_page end |
#results ⇒ Object (readonly)
Returns the value of attribute results.
9 10 11 |
# File 'lib/billrb/list_page.rb', line 9 def results @results end |
Instance Method Details
#auto_paging_each(&block) ⇒ Object
Iterates over every result, fetching subsequent pages as needed.
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/billrb/list_page.rb', line 40 def auto_paging_each(&block) return enum_for(:auto_paging_each) unless block page = self loop do page.results.each(&block) break unless page.next_page? page = page.fetch_next_page end end |
#each ⇒ Object
25 26 27 |
# File 'lib/billrb/list_page.rb', line 25 def each(&) results.each(&) end |
#fetch_next_page ⇒ Object
33 34 35 36 37 |
# File 'lib/billrb/list_page.rb', line 33 def fetch_next_page raise Error, "there is no next page" unless next_page? @resource_class.list(*@list_args, @params.merge(page: next_page), { client: @client }) end |
#next_page? ⇒ Boolean
29 30 31 |
# File 'lib/billrb/list_page.rb', line 29 def next_page? !next_page.nil? end |