Sha256: 2e480430b53d0509e0e7d30a3f86a52b93fd4fd3e0d80c15e89f622952dfd17c

Contents?: true

Size: 760 Bytes

Versions: 2

Compression:

Stored size: 760 Bytes

Contents

# frozen_string_literal: true

module Gitlab
  # Parses link header.
  #
  # @private
  class PageLinks
    HEADER_LINK = 'Link'
    DELIM_LINKS = ','
    LINK_REGEX = /<([^>]+)>; rel=\"([^\"]+)\"/
    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

2 entries across 2 versions & 1 rubygems

Version Path
gitlab-4.6.1 lib/gitlab/page_links.rb
gitlab-4.6.0 lib/gitlab/page_links.rb