Class: Prodigi::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/prodigi/collection.rb

Overview

Represents a paginated collection of resources from the Prodigi API

Collections are returned by list endpoints and include pagination information to help traverse large result sets.

Examples:

Accessing collection data

orders = client.orders.list
orders.data        #=> [#<Prodigi::Order>, #<Prodigi::Order>, ...]
orders.has_more    #=> true
orders.next_url    #=> "https://api.prodigi.com/v4.0/orders?skip=10"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data:, has_more:, next_url:) ⇒ Collection

Initializes a new Collection

Parameters:

  • data (Array)

    The collection of resource objects

  • has_more (Boolean)

    Whether additional pages exist

  • next_url (String, nil)

    The URL for the next page of results



44
45
46
47
48
# File 'lib/prodigi/collection.rb', line 44

def initialize(data:, has_more:, next_url:)
  @data = data
  @has_more = has_more
  @next_url = @has_more == true ? next_url : nil
end

Instance Attribute Details

#dataArray (readonly)

The collection of resources

Returns:

  • (Array)

    the current value of data



18
19
20
# File 'lib/prodigi/collection.rb', line 18

def data
  @data
end

#has_moreBoolean (readonly)

Whether more results are available

Returns:

  • (Boolean)

    the current value of has_more



18
19
20
# File 'lib/prodigi/collection.rb', line 18

def has_more
  @has_more
end

#next_urlString? (readonly)

URL to fetch the next page of results

Returns:

  • (String, nil)

    the current value of next_url



18
19
20
# File 'lib/prodigi/collection.rb', line 18

def next_url
  @next_url
end

Class Method Details

.from_response(response, key:, type:) ⇒ Prodigi::Collection

Creates a Collection from an API response

Examples:

Collection.from_response(response, key: "orders", type: Order)

Parameters:

  • response (Faraday::Response)

    The HTTP response from the API

  • key (String)

    The JSON key containing the array of resources

  • type (Class)

    The class to instantiate for each resource (e.g., Prodigi::Order)

Returns:



30
31
32
33
34
35
36
37
# File 'lib/prodigi/collection.rb', line 30

def self.from_response(response, key:, type:)
  body = response.body
  new(
    data: body[key].map { |attrs| type.new(attrs) },
    has_more: body["hasMore"],
    next_url: body["nextUrl"]
  )
end