Class: AbacatePay::Collection

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(items, pagination = nil) ⇒ Collection

Returns a new instance of Collection.

Parameters:

  • items (Array)

    The items on this page

  • pagination (Hash, nil) (defaults to: nil)

    The raw pagination object from the API



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_cursorString? (readonly)

Returns Cursor for the previous page, passed back as before.

Returns:

  • (String, nil)

    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

#itemsArray (readonly)

Returns The items on this page.

Returns:

  • (Array)

    The items on this page



26
27
28
# File 'lib/abacate_pay/collection.rb', line 26

def items
  @items
end

#next_cursorString? (readonly)

Returns Cursor for the next page, passed back as after.

Returns:

  • (String, nil)

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

Parameters:

  • index (Integer, Range)

    Index into this page

Returns:

  • (Object, Array, nil)


74
75
76
# File 'lib/abacate_pay/collection.rb', line 74

def [](index)
  items[index]
end

#each {|Object| ... } ⇒ Enumerator, self

Yields:

  • (Object)

    Each item on this page

Returns:

  • (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

Returns:

  • (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.

Returns:

  • (Boolean)


56
57
58
# File 'lib/abacate_pay/collection.rb', line 56

def has_more?
  @has_more
end

#inspectString

Returns:

  • (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

#sizeInteger Also known as: length, count

Returns Number of items on this page.

Returns:

  • (Integer)

    Number of items on this page



61
62
63
# File 'lib/abacate_pay/collection.rb', line 61

def size
  items.size
end

#to_aArray Also known as: to_ary

Returns A plain Array copy of this page.

Returns:

  • (Array)

    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.

Parameters:

  • new_items (Array)

    The mapped items

Returns:



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