Class: Redd::Models::PaginatedListing

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/redd/models/paginated_listing.rb

Overview

An enumerable type that covers listings and expands forwards.

If you want to use the #[] operator, you'll need to convert the object to an Array. This can be done with either Enumerable#first or Enumerable#to_a.

Defined Under Namespace

Classes: RingBuffer

Instance Method Summary collapse

Constructor Details

#initialize(client, **options) {|after, limit| ... } ⇒ PaginatedListing

Create an expandable listing.

Parameters:

  • client (APIClient)

    the caller to use for streams

  • options (Hash)

Options Hash (**options):

  • :before (String)

    the listing's before parameter

  • :after (String)

    the listing's after parameter

  • :limit (Integer)

    the maximum number of items to fetch

Yield Parameters:

  • after (String)

    the fullname of the item to fetch after

  • limit (Integer)

    the number of items to fetch (max 100)

Yield Returns:

  • (Listing)

    the listing to return

Raises:

  • (ArgumentError)

42
43
44
45
46
47
48
49
50
# File 'lib/redd/models/paginated_listing.rb', line 42

def initialize(client, **options, &block)
  raise ArgumentError, 'block must be provided' unless block_given?

  @client = client
  @caller = block
  @before = options[:before]
  @after = options[:after]
  @limit = options[:limit] || 1000
end

Instance Method Details

#each {|Model| ... } ⇒ Enumerator

Go forward through the listing.

Yields:

  • (Model)

    the object returned in the listings

Returns:

  • (Enumerator)

    if a block wasn't provided


55
56
57
58
# File 'lib/redd/models/paginated_listing.rb', line 55

def each(&block)
  return _each(&block) if block_given?
  enum_for(:_each)
end

#stream {|Model| ... } ⇒ Enumerator

Note:

If you iterate through the stream, you'll loop forever. This may or may not be desirable.

Stream through the listing.

Yields:

  • (Model)

    the object returned in the listings

Returns:

  • (Enumerator)

    if a block wasn't provided


65
66
67
68
# File 'lib/redd/models/paginated_listing.rb', line 65

def stream(&block)
  return _stream(&block) if block_given?
  enum_for(:_stream)
end