Class: Airwallex::ListObject

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/airwallex/list_object.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#dataObject (readonly)

Returns the value of attribute data.



7
8
9
# File 'lib/airwallex/list_object.rb', line 7

def data
  @data
end

#has_moreObject (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_cursorObject (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

#eachObject



17
18
19
# File 'lib/airwallex/list_object.rb', line 17

def each(&)
  @data.each(&)
end

#empty?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/airwallex/list_object.rb', line 32

def empty?
  @data.empty?
end

#firstObject



36
37
38
# File 'lib/airwallex/list_object.rb', line 36

def first
  @data.first
end

#inspectObject



81
82
83
# File 'lib/airwallex/list_object.rb', line 81

def inspect
  "#<#{self.class}[#{@data.size}] has_more=#{@has_more}>"
end

#lastObject



40
41
42
# File 'lib/airwallex/list_object.rb', line 40

def last
  @data.last
end

#next_pageObject

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

#sizeObject Also known as: length, count



25
26
27
# File 'lib/airwallex/list_object.rb', line 25

def size
  @data.size
end

#to_aObject



77
78
79
# File 'lib/airwallex/list_object.rb', line 77

def to_a
  @data
end