File tree Expand file tree Collapse file tree 6 files changed +110
-6
lines changed Expand file tree Collapse file tree 6 files changed +110
-6
lines changed Original file line number Diff line number Diff line change @@ -100,6 +100,8 @@ class MoviesAPI < Grape::API
100
100
end
101
101
```
102
102
103
+ ## Headers
104
+
103
105
Then ` curl --include ` to see your header-based pagination in action:
104
106
105
107
``` bash
@@ -114,6 +116,27 @@ Per-Page: 10
114
116
# ...
115
117
```
116
118
119
+ If you want, you can customize the name of ` Total ` and ` Per-Page ` headers.
120
+ All you need to do is to set the ` total_header ` or ` per_page_header ` on ` ApiPagination ` .
121
+ If nothing is setted it defaults to ` Total ` and ` Per-Page ` .
122
+
123
+ ``` ruby
124
+ ApiPagination .total_header = ' X-Total-Count'
125
+ ApiPagination .per_page_header = ' X-Per-Page'
126
+ ```
127
+
128
+ ``` bash
129
+ $ curl --include ' https://localhost:3000/movies?page=5'
130
+ HTTP/1.1 200 OK
131
+ Link: < http://localhost:3000/movies? page=1> ; rel=" first" ,
132
+ < http://localhost:3000/movies? page=173> ; rel=" last" ,
133
+ < http://localhost:3000/movies? page=6> ; rel=" next" ,
134
+ < http://localhost:3000/movies? page=4> ; rel=" prev"
135
+ X-Total-Count: 4321
136
+ X-Per-Page: 10
137
+ # ...
138
+ ```
139
+
117
140
[ kaminari ] : https://github.com/amatsuda/kaminari
118
141
[ will_paginate ] : https://github.com/mislav/will_paginate
119
142
Original file line number Diff line number Diff line change 3
3
module ApiPagination
4
4
class << self
5
5
attr_reader :paginator
6
+ attr_writer :total_header , :per_page_header
6
7
7
8
def paginate ( collection , options = { } )
8
9
options [ :page ] = options [ :page ] . to_i
@@ -51,6 +52,14 @@ def total_from(collection)
51
52
when :will_paginate then collection . total_entries . to_s
52
53
end
53
54
end
55
+
56
+ def total_header
57
+ @total_header ||= 'Total'
58
+ end
59
+
60
+ def per_page_header
61
+ @per_page_header ||= 'Per-Page'
62
+ end
54
63
end
55
64
end
56
65
Original file line number Diff line number Diff line change @@ -20,9 +20,12 @@ def paginate(collection)
20
20
links << %(<#{ url } ?#{ new_params . to_param } >; rel="#{ k } ")
21
21
end
22
22
23
- header 'Link' , links . join ( ', ' ) unless links . empty?
24
- header 'Total' , ApiPagination . total_from ( collection )
25
- header 'Per-Page' , options [ :per_page ]
23
+ total_header = ApiPagination . total_header
24
+ per_page_header = ApiPagination . per_page_header
25
+
26
+ header 'Link' , links . join ( ', ' ) unless links . empty?
27
+ header total_header , ApiPagination . total_from ( collection )
28
+ header per_page_header , options [ :per_page ] . to_s
26
29
27
30
return collection
28
31
end
Original file line number Diff line number Diff line change @@ -39,9 +39,12 @@ def _paginate_collection(collection, options={})
39
39
links << %(<#{ url } ?#{ new_params . to_param } >; rel="#{ k } ")
40
40
end
41
41
42
- headers [ 'Link' ] = links . join ( ', ' ) unless links . empty?
43
- headers [ 'Total' ] = ApiPagination . total_from ( collection )
44
- headers [ 'Per-Page' ] = options [ :per_page ] . to_s
42
+ total_header = ApiPagination . total_header
43
+ per_page_header = ApiPagination . per_page_header
44
+
45
+ headers [ 'Link' ] = links . join ( ', ' ) unless links . empty?
46
+ headers [ total_header ] = ApiPagination . total_from ( collection )
47
+ headers [ per_page_header ] = options [ :per_page ] . to_s
45
48
46
49
return collection
47
50
end
Original file line number Diff line number Diff line change 66
66
end
67
67
end
68
68
end
69
+
70
+ context 'with custom response headers' do
71
+ before do
72
+ ApiPagination . total_header = 'X-Total-Count'
73
+ ApiPagination . per_page_header = 'X-Per-Page'
74
+
75
+ get '/numbers' , count : 10
76
+ end
77
+
78
+ after do
79
+ ApiPagination . total_header = nil
80
+ ApiPagination . per_page_header = nil
81
+ end
82
+
83
+ let ( :total ) { last_response . header [ 'X-Total-Count' ] . to_i }
84
+ let ( :per_page ) { last_response . header [ 'X-Per-Page' ] . to_i }
85
+
86
+ it 'should give a X-Total-Count header' do
87
+ headers_keys = last_response . headers . keys
88
+
89
+ expect ( headers_keys ) . not_to include ( 'Total' )
90
+ expect ( headers_keys ) . to include ( 'X-Total-Count' )
91
+ expect ( total ) . to eq ( 10 )
92
+ end
93
+
94
+ it 'should give a X-Per-Page header' do
95
+ headers_keys = last_response . headers . keys
96
+
97
+ expect ( headers_keys ) . not_to include ( 'Per-Page' )
98
+ expect ( headers_keys ) . to include ( 'X-Per-Page' )
99
+ expect ( per_page ) . to eq ( 10 )
100
+ end
101
+ end
69
102
end
70
103
end
Original file line number Diff line number Diff line change 68
68
expect ( response . body ) . to eq ( json )
69
69
end
70
70
end
71
+
72
+ context 'with custom response headers' do
73
+ before do
74
+ ApiPagination . total_header = 'X-Total-Count'
75
+ ApiPagination . per_page_header = 'X-Per-Page'
76
+
77
+ get :index , count : 10
78
+ end
79
+
80
+ after do
81
+ ApiPagination . total_header = nil
82
+ ApiPagination . per_page_header = nil
83
+ end
84
+
85
+ let ( :total ) { response . header [ 'X-Total-Count' ] . to_i }
86
+ let ( :per_page ) { response . header [ 'X-Per-Page' ] . to_i }
87
+
88
+ it 'should give a X-Total-Count header' do
89
+ headers_keys = response . headers . keys
90
+
91
+ expect ( headers_keys ) . not_to include ( 'Total' )
92
+ expect ( headers_keys ) . to include ( 'X-Total-Count' )
93
+ expect ( total ) . to eq ( 10 )
94
+ end
95
+
96
+ it 'should give a X-Per-Page header' do
97
+ headers_keys = response . headers . keys
98
+
99
+ expect ( headers_keys ) . not_to include ( 'Per-Page' )
100
+ expect ( headers_keys ) . to include ( 'X-Per-Page' )
101
+ expect ( per_page ) . to eq ( 10 )
102
+ end
103
+ end
71
104
end
72
105
end
You can’t perform that action at this time.
0 commit comments