module MSS::Core::Collection::WithLimitAndNextToken

# Collection::WithLimitAndNextToken

This module is used by collections where the service may truncate responses but that also accept a upper limit of results to return in a single request.

See {MSS::Core::Collection} for documentation on the available methods.

Protected Instance Methods

_each_batch(options = {}) { |batch| ... } click to toggle source
# File lib/mss/core/collection/with_limit_and_next_token.rb, line 34
def _each_batch options = {}, &block

  limit = _extract_limit(options)
  batch_size = _extract_batch_size(options)
  next_token = _extract_next_token(options)

  total = 0  # count of items yeilded across all batches

  begin

    max = nil
    if limit or batch_size
      max = []
      max << (limit - total) if limit
      max << batch_size if batch_size
      max = max.min
    end

    batch = []
    next_token = _each_item(next_token, max, options.dup) do |item|

      total += 1
      batch << item

    end

    yield(batch)

  end until next_token.nil? or (limit and limit == total)

  next_token

end