diff options
author | Steve Azzopardi <sazzopardi@gitlab.com> | 2018-11-09 12:25:22 +0300 |
---|---|---|
committer | Steve Azzopardi <sazzopardi@gitlab.com> | 2018-11-09 12:25:22 +0300 |
commit | ccbc5569f5d3c92cb34a1d4534bff6cf30452ee0 (patch) | |
tree | b93d928961983b53b6a1702866353cdf5a28d504 /lib/bitbucket_server/paginator.rb | |
parent | 7c4546a1161cd4cd5371f988f62c2e11812e54e6 (diff) | |
parent | 52fba2e9c2a512948938b1fbb214e85593ea5f3e (diff) |
Merge branch '11-5-stable-prepare-rc4' into '11-5-stable'
Prepare 11.5 RC4 release
See merge request gitlab-org/gitlab-ce!22900
Diffstat (limited to 'lib/bitbucket_server/paginator.rb')
-rw-r--r-- | lib/bitbucket_server/paginator.rb | 27 |
1 files changed, 21 insertions, 6 deletions
diff --git a/lib/bitbucket_server/paginator.rb b/lib/bitbucket_server/paginator.rb index c351fb2f11f..aa5f84f44b3 100644 --- a/lib/bitbucket_server/paginator.rb +++ b/lib/bitbucket_server/paginator.rb @@ -4,34 +4,49 @@ module BitbucketServer class Paginator PAGE_LENGTH = 25 - def initialize(connection, url, type) + attr_reader :page_offset + + def initialize(connection, url, type, page_offset: 0, limit: nil) @connection = connection @type = type @url = url @page = nil + @page_offset = page_offset + @limit = limit || PAGE_LENGTH + @total = 0 end def items raise StopIteration unless has_next_page? + raise StopIteration if over_limit? @page = fetch_next_page + @total += @page.items.count @page.items end + def has_next_page? + page.nil? || page.next? + end + private - attr_reader :connection, :page, :url, :type + attr_reader :connection, :page, :url, :type, :limit - def has_next_page? - page.nil? || page.next? + def over_limit? + @limit.positive? && @total >= @limit end def next_offset - page.nil? ? 0 : page.next + page.nil? ? starting_offset : page.next + end + + def starting_offset + [0, page_offset - 1].max * limit end def fetch_next_page - parsed_response = connection.get(@url, start: next_offset, limit: PAGE_LENGTH) + parsed_response = connection.get(@url, start: next_offset, limit: @limit) Page.new(parsed_response, type) end end |