summaryrefslogtreecommitdiff
path: root/lib/gitlab/pagination/keyset/header_builder.rb
blob: 69c468207f6b26f4638aea4fce158780b3e83c1f (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
39
40
41
42
43
44
45
# frozen_string_literal: true

module Gitlab
  module Pagination
    module Keyset
      class HeaderBuilder
        attr_reader :request_context
        delegate :params, :header, :request, to: :request_context

        def initialize(request_context)
          @request_context = request_context
        end

        def add_next_page_header(query_params)
          link = next_page_link(page_href(query_params))
          header('Links', link)
          header('Link', link)
        end

        private

        def next_page_link(href)
          %(<#{href}>; rel="next")
        end

        def page_href(query_params)
          base_request_uri.tap do |uri|
            uri.query = updated_params(query_params).to_query
          end.to_s
        end

        def base_request_uri
          @base_request_uri ||= URI.parse(request.url).tap do |uri|
            uri.host = Gitlab.config.gitlab.host
            uri.port = Gitlab.config.gitlab.port
          end
        end

        def updated_params(query_params)
          params.merge(query_params)
        end
      end
    end
  end
end