gitlab-org--gitlab-foss/lib/github/collection.rb

30 lines
562 B
Ruby
Raw Normal View History

2017-04-11 21:33:54 -04:00
module Github
class Collection
2017-04-19 19:04:58 -04:00
attr_reader :options
def initialize(options)
@options = options
end
2017-04-12 21:43:38 -04:00
def fetch(url, query = {})
return [] if url.blank?
2017-04-11 21:33:54 -04:00
Enumerator.new do |yielder|
loop do
2017-04-12 21:43:38 -04:00
response = client.get(url, query)
2017-04-11 21:33:54 -04:00
response.body.each { |item| yielder << item }
2017-04-11 21:33:54 -04:00
raise StopIteration unless response.rels.key?(:next)
2017-04-12 21:43:38 -04:00
url = response.rels[:next]
2017-04-11 21:33:54 -04:00
end
end.lazy
end
private
def client
2017-04-19 19:04:58 -04:00
@client ||= Github::Client.new(options)
2017-04-11 21:33:54 -04:00
end
end
end