Class: Attio::APIResource::ListObject

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

Instance Method Summary collapse

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

#cursorObject (readonly)

Returns the value of attribute cursor.



375
376
377
# File 'lib/attio/api_resource.rb', line 375

def cursor
  @cursor
end

#dataObject (readonly)

Returns the value of attribute data.



375
376
377
# File 'lib/attio/api_resource.rb', line 375

def data
  @data
end

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

Parameters:

  • index (Integer)

    The index of the item to retrieve

Returns:

  • (APIResource, nil)

    The resource at the given 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

Yields:

Returns:

  • (Enumerator)

    If no block given



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

Yields:

Returns:

  • (Enumerator)

    If no block given



396
397
398
# File 'lib/attio/api_resource.rb', line 396

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

#empty?Boolean

Returns:

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

Parameters:

  • n (Integer) (defaults to: nil)

    Number of items to return (optional)

Returns:



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

Returns:

  • (Boolean)


440
441
442
# File 'lib/attio/api_resource.rb', line 440

def has_more?
  @has_more == true
end

#inspectString

Human-readable representation of the list

Returns:

  • (String)

    Inspection string with data and pagination info



467
468
469
# File 'lib/attio/api_resource.rb', line 467

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

#lastAPIResource?

Get the last item in the current page

Returns:

  • (APIResource, nil)

    The last resource or nil if empty



421
422
423
# File 'lib/attio/api_resource.rb', line 421

def last
  @data.last
end

#lengthInteger Also known as: size, count

Get the number of items in the current page

Returns:

  • (Integer)

    Number of items



406
407
408
# File 'lib/attio/api_resource.rb', line 406

def length
  @data.length
end

#next_pageListObject?

Fetch the next page of results

Returns:

  • (ListObject, nil)

    The next page or nil if no more pages



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_aArray<APIResource>

Convert current page to array

Returns:

  • (Array<APIResource>)

    Array of resources in current page



461
462
463
# File 'lib/attio/api_resource.rb', line 461

def to_a
  @data
end