api_pagination_headers 1.0.1 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/Gemfile.lock +1 -1
- data/README.md +4 -4
- data/lib/api_pagination_headers/version.rb +1 -1
- data/lib/api_pagination_headers.rb +23 -14
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2951f6ab1e6f4ae8b918ef019ebaf213ca871227
|
4
|
+
data.tar.gz: 84f12322efdfd46aa93e11f0b65eb6e38afb7639
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 56c710ff5058147cf1add25c6b6434dcd19460a45a0af8e899ba13abbd35f84b7e46862631287e2c24b510c9de0bb4e76b2db1d3c785b25789230bbe3e10a974
|
7
|
+
data.tar.gz: 40d2074b5382155d0e752c5c5c367fa65bbbbcec532b91822b8d0208eb1db3bf200aaf64ae424502d6b358c2c3a0e91e49b270b96159019c3e33d5eb9961e2cf
|
data/.gitignore
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,11 +1,12 @@
|
|
1
|
-
#api_pagination_headers
|
1
|
+
#api_pagination_headers
|
2
|
+
[](http://travis-ci.org/richardkall/api_pagination_headers) [](https://gemnasium.com/richardkall/api_pagination_headers) [](https://codeclimate.com/github/richardkall/api_pagination_headers)
|
2
3
|
|
3
4
|
api_pagination_headers is a gem that adds pagination info to a `Link` response header. This is considered to be the [best practice](http://www.vinaysahni.com/best-practices-for-a-pragmatic-restful-api#pagination) for REST APIs and is currently used by [GitHub](http://developer.github.com/v3/#pagination). It also adds the total number of records into a `X-Total-Count` header.
|
4
5
|
|
5
6
|
Example:
|
6
7
|
|
7
8
|
```
|
8
|
-
Link: <http://example.com/posts?page=2&per_page=10>;rel="next"
|
9
|
+
Link: <http://example.com/posts?page=2&per_page=10>; rel="next", <http://example.com/posts?page=2&per_page=10>; rel="last"
|
9
10
|
X-Total-Count: 11
|
10
11
|
```
|
11
12
|
|
@@ -15,10 +16,9 @@ Include in your Gemfile:
|
|
15
16
|
|
16
17
|
```
|
17
18
|
gem 'api_pagination_headers'
|
18
|
-
gem 'will_paginate'
|
19
19
|
```
|
20
20
|
|
21
|
-
|
21
|
+
Depending on [will_paginate](https://github.com/mislav/will_paginate) at the moment.
|
22
22
|
|
23
23
|
Support for [rails-api](https://github.com/rails-api/rails-api) is built in.
|
24
24
|
|
@@ -3,24 +3,33 @@ module ApiPaginationHeaders
|
|
3
3
|
|
4
4
|
def set_pagination_headers(name)
|
5
5
|
scope = instance_variable_get("@#{name}")
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
pages = set_page_numbers(scope)
|
7
|
+
links = create_links(pages)
|
8
|
+
|
9
|
+
headers['Link'] = links.join(', ') unless links.empty?
|
10
|
+
headers['X-Total-Count'] = "#{scope.total_entries}"
|
11
|
+
end
|
9
12
|
|
10
|
-
|
11
|
-
page[:first] = 1 if scope.total_pages > 1 && scope.current_page > 1
|
12
|
-
page[:prev] = scope.current_page - 1 if scope.current_page > 1
|
13
|
-
page[:next] = scope.current_page + 1 if scope.current_page < scope.total_pages
|
14
|
-
page[:last] = scope.total_pages if scope.total_pages > 1 && scope.current_page < scope.total_pages
|
13
|
+
private
|
15
14
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
15
|
+
def create_links(pages)
|
16
|
+
url_without_params = request.url.split('?').first
|
17
|
+
per_page = params[:per_page] ? params[:per_page].to_i : WillPaginate.per_page
|
18
|
+
links = []
|
19
|
+
pages.each do |key, value|
|
20
|
+
new_params = request.query_parameters.merge({ page: value, per_page: per_page })
|
21
|
+
links << "<#{url_without_params}?#{new_params.to_param}>; rel=\"#{key}\""
|
20
22
|
end
|
23
|
+
links
|
24
|
+
end
|
21
25
|
|
22
|
-
|
23
|
-
|
26
|
+
def set_page_numbers(scope)
|
27
|
+
pages = {}
|
28
|
+
pages[:first] = 1 if scope.total_pages > 1 && scope.current_page > 1
|
29
|
+
pages[:prev] = scope.current_page - 1 if scope.current_page > 1
|
30
|
+
pages[:next] = scope.current_page + 1 if scope.current_page < scope.total_pages
|
31
|
+
pages[:last] = scope.total_pages if scope.total_pages > 1 && scope.current_page < scope.total_pages
|
32
|
+
pages
|
24
33
|
end
|
25
34
|
end
|
26
35
|
|