diff options
author | Douglas Barbosa Alexandre <dbalexandre@gmail.com> | 2016-08-22 15:48:31 -0300 |
---|---|---|
committer | Stan Hu <stanhu@gmail.com> | 2016-11-21 16:47:25 -0800 |
commit | e2f7f32a60a7663d12b5dae0320f640150f354e7 (patch) | |
tree | fafec7cd55481fa66d6e5757b89c507e2edf8e8b /lib/bitbucket/paginator.rb | |
parent | f1f5863bfc5727dba4767a54a299b0cf526b025a (diff) | |
download | gitlab-ce-e2f7f32a60a7663d12b5dae0320f640150f354e7.tar.gz |
Add support for Bitbucket pagination when fetching collections
Diffstat (limited to 'lib/bitbucket/paginator.rb')
-rw-r--r-- | lib/bitbucket/paginator.rb | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/lib/bitbucket/paginator.rb b/lib/bitbucket/paginator.rb new file mode 100644 index 00000000000..a1672d9eaa1 --- /dev/null +++ b/lib/bitbucket/paginator.rb @@ -0,0 +1,38 @@ +module Bitbucket + class Paginator + PAGE_LENGTH = 50 # The minimum length is 10 and the maximum is 100. + + def initialize(connection, url, type) + @connection = connection + @type = type + @url = url + @page = nil + + connection.query(pagelen: PAGE_LENGTH, sort: :created_on) + end + + def next + raise StopIteration unless has_next_page? + + @page = fetch_next_page + @page.items + end + + private + + attr_reader :connection, :page, :url, :type + + def has_next_page? + page.nil? || page.next? + end + + def page_url + page.nil? ? url : page.next + end + + def fetch_next_page + parsed_response = connection.get(page_url) + Page.new(parsed_response, type) + end + end +end |