Skip to content

Commit 876fb65

Browse files
committed
Provide a Per-Page header
This header helps end users know, even if they are on a page that isn't full, how many objects a single page may contain in the response. This helps play nicely with some frontend pagination libraries that would communicate with an API using api-pagination. Closes #26 Signed-off-by: David Celis <[email protected]>
1 parent d2082ce commit 876fb65

File tree

4 files changed

+21
-15
lines changed

4 files changed

+21
-15
lines changed

lib/grape/pagination.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ def paginate(collection)
1919
links << %(<#{url}?#{new_params.to_param}>; rel="#{k}")
2020
end
2121

22-
header 'Link', links.join(', ') unless links.empty?
23-
header 'Total', ApiPagination.total_from(collection)
22+
header 'Link', links.join(', ') unless links.empty?
23+
header 'Total', ApiPagination.total_from(collection)
24+
header 'Per-Page', options[:per_page]
2425

2526
return collection
2627
end

lib/rails/pagination.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ def _paginate_collection(collection, options={})
3939
links << %(<#{url}?#{new_params.to_param}>; rel="#{k}")
4040
end
4141

42-
headers['Link'] = links.join(', ') unless links.empty?
43-
headers['Total'] = ApiPagination.total_from(collection)
42+
headers['Link'] = links.join(', ') unless links.empty?
43+
headers['Total'] = ApiPagination.total_from(collection)
44+
headers['Per-Page'] = options[:per_page]
4445

4546
return collection
4647
end
4748
end
4849
end
49-

spec/grape_spec.rb

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
describe 'GET #index' do
99
let(:links) { last_response.headers['Link'].split(', ') }
1010
let(:total) { last_response.headers['Total'].to_i }
11+
let(:per_page) { last_response.headers['Per-Page'].to_i }
1112

1213
context 'without enough items to give more than one page' do
13-
before { get :numbers, :count => 10 }
14+
before { get '/numbers', :count => 10 }
1415

1516
it 'should not paginate' do
1617
expect(last_response.headers.keys).not_to include('Link')
@@ -20,33 +21,37 @@
2021
expect(total).to eq(10)
2122
end
2223

24+
it 'should give a Per-Page header' do
25+
expect(per_page).to eq(10)
26+
end
27+
2328
it 'should list all numbers in the response body' do
2429
body = '[1,2,3,4,5,6,7,8,9,10]'
2530
expect(last_response.body).to eq(body)
2631
end
2732
end
2833

2934
context 'with existing Link headers' do
30-
before { get :numbers, :count => 30, :with_headers => true }
35+
before { get '/numbers', :count => 30, :with_headers => true }
3136

3237
it_behaves_like 'an endpoint with existing Link headers'
3338
end
3439

3540
context 'with enough items to paginate' do
3641
context 'when on the first page' do
37-
before { get :numbers, :count => 100 }
42+
before { get '/numbers', :count => 100 }
3843

3944
it_behaves_like 'an endpoint with a first page'
4045
end
4146

4247
context 'when on the last page' do
43-
before { get :numbers, :count => 100, :page => 10 }
48+
before { get '/numbers', :count => 100, :page => 10 }
4449

4550
it_behaves_like 'an endpoint with a last page'
4651
end
4752

4853
context 'when somewhere comfortably in the middle' do
49-
before { get :numbers, :count => 100, :page => 2 }
54+
before { get '/numbers', :count => 100, :page => 2 }
5055

5156
it_behaves_like 'an endpoint with a middle page'
5257
end

spec/rails_spec.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
describe 'GET #index' do
1111
let(:links) { response.headers['Link'].split(', ') }
1212
let(:total) { response.headers['Total'].to_i }
13+
let(:per_page) { response.headers['Per-Page'].to_i }
1314

1415
context 'without enough items to give more than one page' do
1516
before { get :index, :count => 10 }
@@ -22,6 +23,10 @@
2223
expect(total).to eq(10)
2324
end
2425

26+
it 'should give a Per-Page header' do
27+
expect(per_page).to eq(10)
28+
end
29+
2530
it 'should list all numbers in the response body' do
2631
body = '[1,2,3,4,5,6,7,8,9,10]'
2732
expect(response.body).to eq(body)
@@ -65,8 +70,3 @@
6570
end
6671
end
6772
end
68-
69-
70-
71-
72-

0 commit comments

Comments
 (0)