-
Notifications
You must be signed in to change notification settings - Fork 4
/
collection.rb
53 lines (42 loc) · 1.16 KB
/
collection.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
require "forwardable"
module Tacokit
class Collection
include Enumerable
extend Forwardable
MAX = 1000
def_delegators :@collection, :[], :empty?, :size
def initialize(client, method, path, options)
@client = client
@method = method
@path = path
@options = options
@before = options.fetch(:before, nil)
@limit = options.fetch(:limit, 50)
@max = options.fetch(:max, MAX)
@collection = []
fetch_next_page
end
def each(start = 0)
return to_enum(:each, start) unless block_given?
Array(@collection[start..-1]).each do |element|
yield(element)
end
unless last?
start = [@collection.size, start].max
fetch_next_page
each(start, &Proc.new)
end
self
end
private
def last?
@last_response_empty || @collection.length >= @max
end
def fetch_next_page
response = @client.send(@method, @path, @options.merge(before: @before, limit: @limit))
@last_response_empty = response.empty?
@collection += response
@before = response.last["id"] unless @last_response_empty
end
end
end