Welcome to mirror list, hosted at ThFree Co, Russian Federation.

paginator.rb « bitbucket « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d0e23007ff82ff0609418b7915f2f7317f26ee6c (plain)
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
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.set_default_query_parameters(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