Module: TinyClient::PaginationSupport::ClassMethods

Defined in:
lib/tiny_client/pagination_support.rb

Overview

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.

Instance Method Summary collapse

Instance Method Details

#get_all(params = {}, id = nil, name = nil, resource_class = nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# 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) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# 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 = {}) ⇒ Object

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)



16
17
18
# File 'lib/tiny_client/pagination_support.rb', line 16

def index_all(params = {})
  get_all(params)
end

#index_in_batches(params = {}) ⇒ Object

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



22
23
24
# File 'lib/tiny_client/pagination_support.rb', line 22

def index_in_batches(params = {})
  get_in_batches(params)
end