Sha256: 42b75417b0695c7b5f357bd0fe88750361d0d8a666ef3e8f2818a0b4462f8704

Contents?: true

Size: 748 Bytes

Versions: 7

Compression:

Stored size: 748 Bytes

Contents

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

    attr_accessor(*METAS)

    def initialize(headers)
      link_header = headers[HEADER_LINK]

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

    private

    def extract_links(header)
      header.split(DELIM_LINKS).each do |link|
        LINK_REGEX.match(link.strip) do |match|
          url, meta = match[1], match[2]
          next if !url || !meta || METAS.index(meta).nil?
          self.send("#{meta}=", url)
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
gitlab-4.2.0 lib/gitlab/page_links.rb
gitlab-4.1.0 lib/gitlab/page_links.rb
gitlab-4.0.0 lib/gitlab/page_links.rb
gitlab-akerl-4.0.0 lib/gitlab/page_links.rb
gitlab-3.7.0 lib/gitlab/page_links.rb
gitlab-3.6.1 lib/gitlab/page_links.rb
gitlab-3.6.0 lib/gitlab/page_links.rb