Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/gitlab.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
require 'gitlab/objectified_hash'
require 'gitlab/configuration'
require 'gitlab/error'
require 'gitlab/page_links'
require 'gitlab/headers/page_links'
require 'gitlab/headers/total'
require 'gitlab/paginated_response'
require 'gitlab/file_response'
require 'gitlab/request'
Expand Down
37 changes: 37 additions & 0 deletions lib/gitlab/headers/page_links.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# 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
29 changes: 29 additions & 0 deletions lib/gitlab/headers/total.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

module Gitlab
module Headers
# Parses total header.
#
# @private
class Total
HEADER_TOTAL = 'x-total'
TOTAL_REGEX = /^\d+$/.freeze

attr_accessor :total

def initialize(headers)
header_total = headers[HEADER_TOTAL]

extract_total(header_total) if header_total
end

private

def extract_total(header_total)
TOTAL_REGEX.match(header_total.strip) do |match|
@total = match[0]
end
end
end
end
end
35 changes: 0 additions & 35 deletions lib/gitlab/page_links.rb

This file was deleted.

7 changes: 6 additions & 1 deletion lib/gitlab/paginated_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ def respond_to_missing?(method_name, include_private = false)
end

def parse_headers!(headers)
@links = PageLinks.new headers
@links = Headers::PageLinks.new headers
@total = Headers::Total.new headers
end

def each_page
Expand Down Expand Up @@ -58,6 +59,10 @@ def paginate_with_limit(limit, &block)
lazy_paginate.take(limit).each(&block)
end

def total
@total.total
end

def last_page?
!(@links.nil? || @links.last.nil?)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require 'spec_helper'

RSpec.describe Gitlab::PageLinks do
RSpec.describe Gitlab::Headers::PageLinks do
before do
@page_links = described_class.new('Link' => '<http://example.com/api/v3/projects?page=1&per_page=5>; rel="first", <http://example.com/api/v3/projects?page=20&per_page=5>; rel="last", <http://example.com/api/v3/projects?page=7&per_page=5>; rel="prev", <http://example.com/api/v3/projects?page=9&per_page=5>; rel="next"')
end
Expand Down
15 changes: 15 additions & 0 deletions spec/gitlab/headers/total_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Headers::Total do
before do
@total = described_class.new('x-total' => ' 8 ')
end

describe '.extract_total' do
it 'extracts total header appropriately' do
expect(@total.total).to eql('8')
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is #total a string and not an integer?

end
end
end
6 changes: 4 additions & 2 deletions spec/gitlab/paginated_response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
@paginated_response = described_class.new array
end

it 'responds to *_page and has_*_page methods' do
it 'responds to total, *_page and has_*_page methods' do
expect(@paginated_response).to respond_to :first_page
expect(@paginated_response).to respond_to :last_page
expect(@paginated_response).to respond_to :next_page
Expand All @@ -17,11 +17,12 @@
expect(@paginated_response).to respond_to :has_last_page?
expect(@paginated_response).to respond_to :has_next_page?
expect(@paginated_response).to respond_to :has_prev_page?
expect(@paginated_response).to respond_to :total
end

describe '.parse_headers!' do
it 'parses headers' do
@paginated_response.parse_headers!('Link' => '<http://example.com/api/v3/projects?page=1&per_page=5>; rel="first", <http://example.com/api/v3/projects?page=20&per_page=5>; rel="last"')
@paginated_response.parse_headers!('Link' => '<http://example.com/api/v3/projects?page=1&per_page=5>; rel="first", <http://example.com/api/v3/projects?page=20&per_page=5>; rel="last"', 'x-total' => '8')
client = @paginated_response.client = double('client')
first_page_response = double('first_page_response')
last_page_response = double('last_page_response')
Expand All @@ -36,6 +37,7 @@
expect(@paginated_response.last_page).to be last_page_response
expect(@paginated_response.next_page).to be_nil
expect(@paginated_response.prev_page).to be_nil
expect(@paginated_response.total).to eq('8')
end

context 'when the Link header endpoint does not match the configured endpoint' do
Expand Down