Class: AbacatePay::Collection
- Inherits:
-
Object
- Object
- AbacatePay::Collection
- Includes:
- Enumerable
- Defined in:
- lib/abacate_pay/collection.rb
Overview
A page of results plus the cursor needed to fetch the next one.
List endpoints return at most 100 items and report whether more exist. The SDK used to return a bare Array and drop that metadata, which made it impossible to page past the first 100 records.
Behaves like an Array everywhere an Array was returned before, so existing code keeps working:
customers = AbacatePay.customers.list
customers.each { |c| puts c.id } # Enumerable
customers.size # items on this page
customers.has_more? # is there another page?
To walk every page without handling cursors:
AbacatePay.customers.each_page { |page| page.each { |c| puts c.id } }
AbacatePay.customers.auto_paging_each { |customer| puts customer.id }
Instance Attribute Summary collapse
-
#before_cursor ⇒ String?
readonly
Cursor for the previous page, passed back as
before. -
#items ⇒ Array
readonly
The items on this page.
-
#next_cursor ⇒ String?
readonly
Cursor for the next page, passed back as
after.
Instance Method Summary collapse
- #[](index) ⇒ Object, ...
- #each {|Object| ... } ⇒ Enumerator, self
- #empty? ⇒ Boolean
-
#has_more? ⇒ Boolean
Whether the API reported further pages after this one.
-
#initialize(items, pagination = nil) ⇒ Collection
constructor
A new instance of Collection.
- #inspect ⇒ String
-
#size ⇒ Integer
(also: #length, #count)
Number of items on this page.
-
#to_a ⇒ Array
(also: #to_ary)
A plain Array copy of this page.
-
#with_items(new_items) ⇒ Collection
Returns a new Collection with different items and the same cursor.
Constructor Details
#initialize(items, pagination = nil) ⇒ Collection
Returns a new instance of Collection.
36 37 38 39 40 41 42 |
# File 'lib/abacate_pay/collection.rb', line 36 def initialize(items, pagination = nil) @items = Array(items) pagination = {} unless pagination.is_a?(Hash) @has_more = pagination["hasMore"] || false @next_cursor = pagination["next"] @before_cursor = pagination["before"] end |
Instance Attribute Details
#before_cursor ⇒ String? (readonly)
Returns Cursor for the previous page, passed back as before.
32 33 34 |
# File 'lib/abacate_pay/collection.rb', line 32 def before_cursor @before_cursor end |
#items ⇒ Array (readonly)
Returns The items on this page.
26 27 28 |
# File 'lib/abacate_pay/collection.rb', line 26 def items @items end |
#next_cursor ⇒ String? (readonly)
Returns Cursor for the next page, passed back as after.
29 30 31 |
# File 'lib/abacate_pay/collection.rb', line 29 def next_cursor @next_cursor end |
Instance Method Details
#[](index) ⇒ Object, ...
74 75 76 |
# File 'lib/abacate_pay/collection.rb', line 74 def [](index) items[index] end |
#each {|Object| ... } ⇒ Enumerator, self
46 47 48 49 50 51 |
# File 'lib/abacate_pay/collection.rb', line 46 def each(&) return to_enum(:each) unless block_given? items.each(&) self end |
#empty? ⇒ Boolean
68 69 70 |
# File 'lib/abacate_pay/collection.rb', line 68 def empty? items.empty? end |
#has_more? ⇒ Boolean
Whether the API reported further pages after this one.
56 57 58 |
# File 'lib/abacate_pay/collection.rb', line 56 def has_more? @has_more end |
#inspect ⇒ String
94 95 96 |
# File 'lib/abacate_pay/collection.rb', line 94 def inspect "#<#{self.class.name} size=#{size} has_more=#{has_more?} next=#{next_cursor.inspect}>" end |
#size ⇒ Integer Also known as: length, count
Returns Number of items on this page.
61 62 63 |
# File 'lib/abacate_pay/collection.rb', line 61 def size items.size end |
#to_a ⇒ Array Also known as: to_ary
Returns A plain Array copy of this page.
79 80 81 |
# File 'lib/abacate_pay/collection.rb', line 79 def to_a items.dup end |
#with_items(new_items) ⇒ Collection
Returns a new Collection with different items and the same cursor. Used to turn raw hashes into resources without losing pagination.
89 90 91 |
# File 'lib/abacate_pay/collection.rb', line 89 def with_items(new_items) self.class.new(new_items, "hasMore" => has_more?, "next" => next_cursor, "before" => before_cursor) end |