api_pagination_headers 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 35814c87aed688c6c42e3fa945a6bb03577c655b
4
- data.tar.gz: 4de3bfa064064a4a779911fbea668d333f63ad0c
3
+ metadata.gz: 2951f6ab1e6f4ae8b918ef019ebaf213ca871227
4
+ data.tar.gz: 84f12322efdfd46aa93e11f0b65eb6e38afb7639
5
5
  SHA512:
6
- metadata.gz: 204c618c1da93248ccff5ea218dab5ca501bd4930dbd43cbca5be9805cb893ffe564371266809df85911a8703250018dd2d42e1a14a135af5bb34bd0f1edd0ca
7
- data.tar.gz: 4d3918d4c766aeb69f582374000cc9c840be70473de6b122a6f91e940b1f890804befcde616dfd97451322c36353c300c20616990153c4400b20464ce12ce907
6
+ metadata.gz: 56c710ff5058147cf1add25c6b6434dcd19460a45a0af8e899ba13abbd35f84b7e46862631287e2c24b510c9de0bb4e76b2db1d3c785b25789230bbe3e10a974
7
+ data.tar.gz: 40d2074b5382155d0e752c5c5c367fa65bbbbcec532b91822b8d0208eb1db3bf200aaf64ae424502d6b358c2c3a0e91e49b270b96159019c3e33d5eb9961e2cf
data/.gitignore CHANGED
@@ -1,5 +1,6 @@
1
1
  .bundle/
2
2
  .DS_Store
3
+ .gem
3
4
  log/*.log
4
5
  pkg/
5
6
  spec/dummy/db/*.sqlite3
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- api_pagination_headers (1.0.0)
4
+ api_pagination_headers (1.1.0)
5
5
  rails (~> 4.0.0)
6
6
  will_paginate (~> 3.0.5)
7
7
 
data/README.md CHANGED
@@ -1,11 +1,12 @@
1
- #api_pagination_headers [![Build Status](https://travis-ci.org/richardkall/api_pagination_headers.png)](http://travis-ci.org/richardkall/api_pagination_headers) [![Dependency Status](https://gemnasium.com/richardkall/api_pagination_headers.png)](https://gemnasium.com/richardkall/api_pagination_headers)
1
+ #api_pagination_headers
2
+ [![Build Status](https://travis-ci.org/richardkall/api_pagination_headers.png)](http://travis-ci.org/richardkall/api_pagination_headers) [![Dependency Status](https://gemnasium.com/richardkall/api_pagination_headers.png)](https://gemnasium.com/richardkall/api_pagination_headers) [![Code Climate](https://codeclimate.com/github/richardkall/api_pagination_headers.png)](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",<http://example.com/posts?page=2&per_page=10>;rel="last"
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
- Only tested with [will_paginate](https://github.com/mislav/will_paginate) at the moment, but should be easily customized for any other pagination gem.
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
 
@@ -1,3 +1,3 @@
1
1
  module ApiPaginationHeaders
2
- VERSION = '1.0.1'
2
+ VERSION = '1.1.0'
3
3
  end
@@ -3,24 +3,33 @@ module ApiPaginationHeaders
3
3
 
4
4
  def set_pagination_headers(name)
5
5
  scope = instance_variable_get("@#{name}")
6
- request_params = request.query_parameters
7
- url_without_params = request.url.split('?').first
8
- per_page = params[:per_page] ? params[:per_page].to_i : WillPaginate.per_page
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
- page = {}
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
- pagination_links = []
17
- page.each do |key, value|
18
- new_request_hash = request_params.merge({ page: value, per_page: per_page })
19
- pagination_links << "<#{url_without_params}?#{new_request_hash.to_param}>; rel=\"#{key}\""
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
- headers['Link'] = pagination_links.join(', ') unless pagination_links.empty?
23
- headers['X-Total-Count'] = "#{scope.total_entries}"
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
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: api_pagination_headers
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Käll