Class: HookSniff::Paginator

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/hooksniff/paginator.rb

Instance Method Summary collapse

Constructor Details

#initialize(fetch_page, limit: nil) ⇒ Paginator

Returns a new instance of Paginator.



15
16
17
18
# File 'lib/hooksniff/paginator.rb', line 15

def initialize(fetch_page, limit: nil)
  @fetch_page = fetch_page
  @limit = limit
end

Instance Method Details

#countObject

Count all items



41
42
43
# File 'lib/hooksniff/paginator.rb', line 41

def count
  each.count
end

#each(&block) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/hooksniff/paginator.rb', line 20

def each(&block)
  return enum_for(:each) unless block_given?

  iterator = nil

  loop do
    page = @fetch_page.call(limit: @limit, iterator: iterator)

    page.data.each(&block)

    break if page.done || page.iterator.nil? || page.iterator.empty?
    iterator = page.iterator
  end
end

#to_aObject

Collect all items into an array



36
37
38
# File 'lib/hooksniff/paginator.rb', line 36

def to_a
  each.to_a
end