Skip to content

Commit e1453be

Browse files
committed
Merge pull request #46 from TiteiKo/add-page-header
Add page header
2 parents 88a1210 + 1cb4bc4 commit e1453be

File tree

6 files changed

+30
-0
lines changed

6 files changed

+30
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ ApiPagination.configure do |config|
3939

4040
# By default, this is set to 'Per-Page'
4141
config.per_page_header = 'X-Per-Page'
42+
43+
# Optional : set this to add a header with the current page number.
44+
config.page_header = 'X-Page'
4245
end
4346
```
4447

lib/api-pagination/configuration.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ class Configuration
44

55
attr_accessor :per_page_header
66

7+
attr_accessor :page_header
8+
79
def configure(&block)
810
yield self
911
end
1012

1113
def initialize
1214
@total_header = 'Total'
1315
@per_page_header = 'Per-Page'
16+
@page_header = nil
1417
end
1518

1619
def paginator

lib/grape/pagination.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ def paginate(collection)
2222

2323
total_header = ApiPagination.config.total_header
2424
per_page_header = ApiPagination.config.per_page_header
25+
page_header = ApiPagination.config.page_header
2526

2627
header 'Link', links.join(', ') unless links.empty?
2728
header total_header, ApiPagination.total_from(collection)
2829
header per_page_header, options[:per_page].to_s
30+
header page_header, options[:page].to_s unless page_header.nil?
2931

3032
return collection
3133
end

lib/rails/pagination.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,12 @@ def _paginate_collection(collection, options={})
4141

4242
total_header = ApiPagination.config.total_header
4343
per_page_header = ApiPagination.config.per_page_header
44+
page_header = ApiPagination.config.page_header
4445

4546
headers['Link'] = links.join(', ') unless links.empty?
4647
headers[total_header] = ApiPagination.total_from(collection)
4748
headers[per_page_header] = options[:per_page].to_s
49+
headers[page_header] = options[:page].to_s unless page_header.nil?
4850

4951
return collection
5052
end

spec/grape_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,20 @@
7171
before do
7272
ApiPagination.config.total_header = 'X-Total-Count'
7373
ApiPagination.config.per_page_header = 'X-Per-Page'
74+
ApiPagination.config.page_header = 'X-Page'
7475

7576
get '/numbers', count: 10
7677
end
7778

7879
after do
7980
ApiPagination.config.total_header = 'Total'
8081
ApiPagination.config.per_page_header = 'Per-Page'
82+
ApiPagination.config.page_header = nil
8183
end
8284

8385
let(:total) { last_response.header['X-Total-Count'].to_i }
8486
let(:per_page) { last_response.header['X-Per-Page'].to_i }
87+
let(:page) { last_response.header['X-Page'].to_i }
8588

8689
it 'should give a X-Total-Count header' do
8790
headers_keys = last_response.headers.keys
@@ -98,6 +101,13 @@
98101
expect(headers_keys).to include('X-Per-Page')
99102
expect(per_page).to eq(10)
100103
end
104+
105+
it 'should give a X-Page header' do
106+
headers_keys = last_response.headers.keys
107+
108+
expect(headers_keys).to include('X-Page')
109+
expect(page).to eq(1)
110+
end
101111
end
102112

103113
context 'with query string including array parameter' do

spec/rails_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,20 @@
7373
before do
7474
ApiPagination.config.total_header = 'X-Total-Count'
7575
ApiPagination.config.per_page_header = 'X-Per-Page'
76+
ApiPagination.config.page_header = 'X-Page'
7677

7778
get :index, count: 10
7879
end
7980

8081
after do
8182
ApiPagination.config.total_header = 'Total'
8283
ApiPagination.config.per_page_header = 'Per-Page'
84+
ApiPagination.config.page_header = nil
8385
end
8486

8587
let(:total) { response.header['X-Total-Count'].to_i }
8688
let(:per_page) { response.header['X-Per-Page'].to_i }
89+
let(:page) { response.header['X-Page'].to_i }
8790

8891
it 'should give a X-Total-Count header' do
8992
headers_keys = response.headers.keys
@@ -100,6 +103,13 @@
100103
expect(headers_keys).to include('X-Per-Page')
101104
expect(per_page).to eq(10)
102105
end
106+
107+
it 'should give a X-Page header' do
108+
headers_keys = response.headers.keys
109+
110+
expect(headers_keys).to include('X-Page')
111+
expect(page).to eq(1)
112+
end
103113
end
104114
end
105115
end

0 commit comments

Comments
 (0)