module TinyClient::PaginationSupport::ClassMethods

Add methods that allows to walk fully through collections thanks to limit/offset pagination. All methods return an enumerator that will query the server in batch based on the limit size and total number of items.

Public Instance Methods

get_all(params = {}, id = nil, name = nil, resource_class = nil) click to toggle source
# File lib/tiny_client/pagination_support.rb, line 26
def get_all(params = {}, id = nil, name = nil, resource_class = nil)
  Enumerator.new do |y|
    count = limit = params.fetch(:limit, @conf.limit || 100)
    offset = params.fetch(:offset, 0)
    while limit == count
      inner = get(params.merge(limit: limit, offset: offset), id, name, resource_class)
      loop { y << inner.next }
      offset += limit
      count = inner.count
    end
  end
end
get_in_batches(params = {}, id = nil, name = nil, resource_class = nil) click to toggle source
# File lib/tiny_client/pagination_support.rb, line 39
def get_in_batches(params = {}, id = nil, name = nil, resource_class = nil)
  Enumerator.new do |y|
    count = limit = params.fetch(:limit, @conf.limit || 100)
    offset = params.fetch(:offset, 0)
    while limit == count
      inner = get(params.merge(limit: limit, offset: offset), id, name, resource_class)
      loop { y << inner }
      offset += limit
      count = inner.count
    end
  end
end
index_all(params = {}) click to toggle source

Similar to {Resource.index} but return all resources available at this path. It use limit and offset params to retrieved all resources. ( buffered by the limit size)

# File lib/tiny_client/pagination_support.rb, line 16
def index_all(params = {})
  get_all(params)
end
index_in_batches(params = {}) click to toggle source

Similar to {index_all}, the return enumerator will yield on the buffered ( limit ) rather than each element.

# File lib/tiny_client/pagination_support.rb, line 22
def index_in_batches(params = {})
  get_in_batches(params)
end