File: page_links.rb

package info (click to toggle)
ruby-gitlab 4.17.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,328 kB
  • sloc: ruby: 10,920; makefile: 7; sh: 4
file content (35 lines) | stat: -rw-r--r-- 764 bytes parent folder | download | duplicates (2)
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
# frozen_string_literal: true

module Gitlab
  # Parses link header.
  #
  # @private
  class PageLinks
    HEADER_LINK = 'Link'
    DELIM_LINKS = ','
    LINK_REGEX = /<([^>]+)>; rel="([^"]+)"/.freeze
    METAS = %w[last next first prev].freeze

    attr_accessor(*METAS)

    def initialize(headers)
      link_header = headers[HEADER_LINK]

      extract_links(link_header) if link_header && link_header =~ /(next|first|last|prev)/
    end

    private

    def extract_links(header)
      header.split(DELIM_LINKS).each do |link|
        LINK_REGEX.match(link.strip) do |match|
          url = match[1]
          meta = match[2]
          next if !url || !meta || METAS.index(meta).nil?

          send("#{meta}=", url)
        end
      end
    end
  end
end