Sha256: c6e94a428fb3798f9f0cc3fad165624cf5fa09aeb6006ede40b86b354d2ab596

Contents?: true

Size: 837 Bytes

Versions: 5

Compression:

Stored size: 837 Bytes

Contents

# frozen_string_literal: true

module Gitlab
  module Headers
    # 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
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
gitlab-4.20.1 lib/gitlab/headers/page_links.rb
gitlab-4.20.0 lib/gitlab/headers/page_links.rb
fs-gitlab-4.19.3 lib/gitlab/headers/page_links.rb
fs-gitlab-4.19.2 lib/gitlab/headers/page_links.rb
fs-gitlab-4.19.1 lib/gitlab/headers/page_links.rb