Class: Airwallex::ListObject
- Inherits:
-
Object
- Object
- Airwallex::ListObject
- Includes:
- Enumerable
- Defined in:
- lib/airwallex/list_object.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#has_more ⇒ Object
readonly
Returns the value of attribute has_more.
-
#next_cursor ⇒ Object
readonly
Returns the value of attribute next_cursor.
Instance Method Summary collapse
- #[](index) ⇒ Object
-
#auto_paging_each(&block) ⇒ Object
Automatically iterate through all pages.
- #each ⇒ Object
- #empty? ⇒ Boolean
- #first ⇒ Object
-
#initialize(data:, has_more:, resource_class:, next_cursor: nil, params: {}) ⇒ ListObject
constructor
A new instance of ListObject.
- #inspect ⇒ Object
- #last ⇒ Object
-
#next_page ⇒ Object
Fetch the next page of results.
- #size ⇒ Object (also: #length, #count)
- #to_a ⇒ Object
Constructor Details
#initialize(data:, has_more:, resource_class:, next_cursor: nil, params: {}) ⇒ ListObject
Returns a new instance of ListObject.
9 10 11 12 13 14 15 |
# File 'lib/airwallex/list_object.rb', line 9 def initialize(data:, has_more:, resource_class:, next_cursor: nil, params: {}) @data = data.map { |item| resource_class.new(item) } @has_more = has_more @next_cursor = next_cursor @resource_class = resource_class @params = params end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
7 8 9 |
# File 'lib/airwallex/list_object.rb', line 7 def data @data end |
#has_more ⇒ Object (readonly)
Returns the value of attribute has_more.
7 8 9 |
# File 'lib/airwallex/list_object.rb', line 7 def has_more @has_more end |
#next_cursor ⇒ Object (readonly)
Returns the value of attribute next_cursor.
7 8 9 |
# File 'lib/airwallex/list_object.rb', line 7 def next_cursor @next_cursor end |
Instance Method Details
#[](index) ⇒ Object
21 22 23 |
# File 'lib/airwallex/list_object.rb', line 21 def [](index) @data[index] end |
#auto_paging_each(&block) ⇒ Object
Automatically iterate through all pages
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/airwallex/list_object.rb', line 64 def auto_paging_each(&block) return enum_for(:auto_paging_each) unless block_given? page = self loop do page.each(&block) break unless page.has_more page = page.next_page break if page.nil? || page.empty? end end |
#each ⇒ Object
17 18 19 |
# File 'lib/airwallex/list_object.rb', line 17 def each(&) @data.each(&) end |
#empty? ⇒ Boolean
32 33 34 |
# File 'lib/airwallex/list_object.rb', line 32 def empty? @data.empty? end |
#first ⇒ Object
36 37 38 |
# File 'lib/airwallex/list_object.rb', line 36 def first @data.first end |
#inspect ⇒ Object
81 82 83 |
# File 'lib/airwallex/list_object.rb', line 81 def inspect "#<#{self.class}[#{@data.size}] has_more=#{@has_more}>" end |
#last ⇒ Object
40 41 42 |
# File 'lib/airwallex/list_object.rb', line 40 def last @data.last end |
#next_page ⇒ Object
Fetch the next page of results
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/airwallex/list_object.rb', line 45 def next_page return nil unless @has_more next_params = @params.dup if @next_cursor # Cursor-based pagination next_params[:next_cursor] = @next_cursor else # Offset-based pagination page_size = @params[:page_size] || @params[:limit] || 20 current_offset = @params[:offset] || 0 next_params[:offset] = current_offset + page_size end @resource_class.list(next_params) end |
#size ⇒ Object Also known as: length, count
25 26 27 |
# File 'lib/airwallex/list_object.rb', line 25 def size @data.size end |
#to_a ⇒ Object
77 78 79 |
# File 'lib/airwallex/list_object.rb', line 77 def to_a @data end |