Class: Attio::APIResource::ListObject
- Inherits:
-
Object
- Object
- Attio::APIResource::ListObject
- Includes:
- Enumerable
- Defined in:
- lib/attio/api_resource.rb
Overview
ListObject for handling paginated responses Container for API list responses with pagination support
Instance Attribute Summary collapse
-
#cursor ⇒ Object
readonly
Returns the value of attribute cursor.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#has_more ⇒ Object
readonly
Returns the value of attribute has_more.
-
#resource_class ⇒ Object
readonly
Returns the value of attribute resource_class.
Instance Method Summary collapse
-
#[](index) ⇒ APIResource?
Access item by index.
-
#auto_paging_each {|APIResource| ... } ⇒ Enumerator
Automatically fetch and iterate through all pages.
-
#each {|APIResource| ... } ⇒ Enumerator
Iterate over each resource in the current page.
- #empty? ⇒ Boolean
-
#first(n = nil) ⇒ APIResource, ...
Get the first item(s) in the current page.
- #has_more? ⇒ Boolean
-
#initialize(response, resource_class, params = {}, opts = {}) ⇒ ListObject
constructor
A new instance of ListObject.
-
#inspect ⇒ String
Human-readable representation of the list.
-
#last ⇒ APIResource?
Get the last item in the current page.
-
#length ⇒ Integer
(also: #size, #count)
Get the number of items in the current page.
-
#next_page ⇒ ListObject?
Fetch the next page of results.
-
#to_a ⇒ Array<APIResource>
Convert current page to array.
Constructor Details
#initialize(response, resource_class, params = {}, opts = {}) ⇒ ListObject
Returns a new instance of ListObject.
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 |
# File 'lib/attio/api_resource.rb', line 377 def initialize(response, resource_class, params = {}, opts = {}) @resource_class = resource_class @params = params @opts = opts @data = [] @has_more = false @cursor = nil if response.is_a?(Hash) raw_data = response["data"] || [] @data = raw_data.map { |attrs| resource_class.new(attrs, opts) } @has_more = response["has_more"] || false @cursor = response["cursor"] end end |
Instance Attribute Details
#cursor ⇒ Object (readonly)
Returns the value of attribute cursor.
375 376 377 |
# File 'lib/attio/api_resource.rb', line 375 def cursor @cursor end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
375 376 377 |
# File 'lib/attio/api_resource.rb', line 375 def data @data end |
#has_more ⇒ Object (readonly)
Returns the value of attribute has_more.
375 376 377 |
# File 'lib/attio/api_resource.rb', line 375 def has_more @has_more end |
#resource_class ⇒ Object (readonly)
Returns the value of attribute resource_class.
375 376 377 |
# File 'lib/attio/api_resource.rb', line 375 def resource_class @resource_class end |
Instance Method Details
#[](index) ⇒ APIResource?
Access item by index
428 429 430 |
# File 'lib/attio/api_resource.rb', line 428 def [](index) @data[index] end |
#auto_paging_each {|APIResource| ... } ⇒ Enumerator
Automatically fetch and iterate through all pages
447 448 449 450 451 452 453 454 455 456 457 |
# File 'lib/attio/api_resource.rb', line 447 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 unless page end end |
#each {|APIResource| ... } ⇒ Enumerator
Iterate over each resource in the current page
396 397 398 |
# File 'lib/attio/api_resource.rb', line 396 def each(&) @data.each(&) end |
#empty? ⇒ Boolean
400 401 402 |
# File 'lib/attio/api_resource.rb', line 400 def empty? @data.empty? end |
#first(n = nil) ⇒ APIResource, ...
Get the first item(s) in the current page
415 416 417 |
# File 'lib/attio/api_resource.rb', line 415 def first(n = nil) n.nil? ? @data.first : @data.first(n) end |
#has_more? ⇒ Boolean
440 441 442 |
# File 'lib/attio/api_resource.rb', line 440 def has_more? @has_more == true end |
#inspect ⇒ String
Human-readable representation of the list
467 468 469 |
# File 'lib/attio/api_resource.rb', line 467 def inspect "#<#{self.class.name} data=#{@data.inspect} has_more=#{@has_more}>" end |
#last ⇒ APIResource?
Get the last item in the current page
421 422 423 |
# File 'lib/attio/api_resource.rb', line 421 def last @data.last end |
#length ⇒ Integer Also known as: size, count
Get the number of items in the current page
406 407 408 |
# File 'lib/attio/api_resource.rb', line 406 def length @data.length end |
#next_page ⇒ ListObject?
Fetch the next page of results
434 435 436 437 438 |
# File 'lib/attio/api_resource.rb', line 434 def next_page return nil unless has_more? && cursor @resource_class.list(@params.merge(cursor: cursor), **@opts) end |
#to_a ⇒ Array<APIResource>
Convert current page to array
461 462 463 |
# File 'lib/attio/api_resource.rb', line 461 def to_a @data end |