Sha256: a956a4073a5e93b5786fba4b6dd4b9f8b7e28bb74c3937f6daaf6e3639bc981a

Contents?: true

Size: 764 Bytes

Versions: 7

Compression:

Stored size: 764 Bytes

Contents

# 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

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
gitlab-4.19.0 lib/gitlab/page_links.rb
fs-gitlab-4.18.2 lib/gitlab/page_links.rb
fs-gitlab-4.18.1 lib/gitlab/page_links.rb
gitlab-4.18.0 lib/gitlab/page_links.rb
gitlab-4.17.0 lib/gitlab/page_links.rb
gitlab-4.16.1 lib/gitlab/page_links.rb
gitlab-4.16.0 lib/gitlab/page_links.rb