Class: PuppetForge::V3::Base::PaginatedCollection Private

Inherits:
Array
  • Object
show all
Defined in:
lib/puppet_forge/v3/base/paginated_collection.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Enables navigation of the Forge API’s paginated datasets.

Constant Summary collapse

LIMIT =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Default pagination limit for API request

20

Instance Method Summary collapse

Constructor Details

#initialize(klass, data = [], metadata = {:total => 0, :offset => 0, :limit => LIMIT}, errors = nil) ⇒ PaginatedCollection

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of PaginatedCollection.

Parameters:

  • klass (PuppetForge::V3::Base)

    the class to page over

  • data (Array) (defaults to: [])

    the current data page

  • metadata (Hash<(:limit, :total, :offset)>) (defaults to: {:total => 0, :offset => 0, :limit => LIMIT})

    page metadata

  • errors (Object) (defaults to: nil)

    errors for the page request



16
17
18
19
20
21
22
23
24
25
# File 'lib/puppet_forge/v3/base/paginated_collection.rb', line 16

def initialize(klass, data = [],  = {:total => 0, :offset => 0, :limit => LIMIT}, errors = nil)
  super()
  @metadata = 
  @errors = errors
  @klass = klass

  data.each do |item|
    self << @klass.new(item)
  end
end

Instance Method Details

#allObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

For backwards compatibility, all returns the current object.



28
29
30
# File 'lib/puppet_forge/v3/base/paginated_collection.rb', line 28

def all
  self
end

#limitInteger

Returns the maximum size of any page in this dataset.

Returns:

  • (Integer)

    the maximum size of any page in this dataset



53
54
55
# File 'lib/puppet_forge/v3/base/paginated_collection.rb', line 53

[ :total, :limit, :offset ].each do |info|
  define_method(info) { @metadata[info] }
end

#offsetInteger

Returns the offset for the current page.

Returns:

  • (Integer)

    the offset for the current page



53
54
55
# File 'lib/puppet_forge/v3/base/paginated_collection.rb', line 53

[ :total, :limit, :offset ].each do |info|
  define_method(info) { @metadata[info] }
end

#totalInteger

Returns the size of the unpaginated dataset.

Returns:

  • (Integer)

    the size of the unpaginated dataset



53
54
55
# File 'lib/puppet_forge/v3/base/paginated_collection.rb', line 53

[ :total, :limit, :offset ].each do |info|
  define_method(info) { @metadata[info] }
end

#unpaginatedEnumerator

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

An enumerator that iterates over the entire collection, independent of API pagination. This will potentially result in several API requests.

Returns:

  • (Enumerator)

    an iterator for the entire collection



37
38
39
40
41
42
43
44
45
# File 'lib/puppet_forge/v3/base/paginated_collection.rb', line 37

def unpaginated
  page = @klass.get_collection(@metadata[:first])
  Enumerator.new do |emitter|
    loop do
      page.each { |x| emitter << x }
      break unless page = page.next
    end
  end
end