api_pagination_headers 1.1.2 → 2.0.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/.travis.yml +1 -1
- data/Gemfile.lock +8 -4
- data/README.md +38 -5
- data/api_pagination_headers.gemspec +2 -1
- data/lib/api_pagination_headers.rb +13 -2
- data/lib/api_pagination_headers/config.rb +24 -0
- data/lib/api_pagination_headers/version.rb +1 -1
- data/spec/controllers/posts_controller_spec.rb +12 -12
- metadata +27 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 18140f8cc85e119d447bed481450005ae984883a
|
4
|
+
data.tar.gz: df38764d04880176f3b71e21b388725d0a2587da
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 207a5bbda68f0525df3a006f728285317b40b4a23a4d8a6cf18a179b5245131a646791532fc2e0d3da643aa7e4512ac9533401a8b18ae07458e638278c9c8db7
|
7
|
+
data.tar.gz: bcec9e9f6a401184c3b4b2d8d48fcdd8f8ddfc10137257b4f5fd04a272bed91a5bdc0c8ca8ea66e29d2ad0363b512e13ae45ef0f809251a1027d0299c0394bc6
|
data/.travis.yml
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,9 +1,8 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
api_pagination_headers (
|
4
|
+
api_pagination_headers (2.0.0)
|
5
5
|
rails (~> 4.0)
|
6
|
-
will_paginate (~> 3.0.5)
|
7
6
|
|
8
7
|
GEM
|
9
8
|
remote: https://rubygems.org/
|
@@ -43,13 +42,16 @@ GEM
|
|
43
42
|
railties (>= 3.0.0)
|
44
43
|
hike (1.2.3)
|
45
44
|
i18n (0.6.9)
|
45
|
+
kaminari (0.16.1)
|
46
|
+
actionpack (>= 3.0.0)
|
47
|
+
activesupport (>= 3.0.0)
|
46
48
|
mail (2.5.4)
|
47
49
|
mime-types (~> 1.16)
|
48
50
|
treetop (~> 1.4.8)
|
49
51
|
mime-types (1.25.1)
|
50
52
|
minitest (4.7.5)
|
51
53
|
multi_json (1.9.2)
|
52
|
-
polyglot (0.3.
|
54
|
+
polyglot (0.3.5)
|
53
55
|
rack (1.5.2)
|
54
56
|
rack-test (0.6.2)
|
55
57
|
rack (>= 1.0)
|
@@ -79,7 +81,7 @@ GEM
|
|
79
81
|
rspec-core (~> 2.14.0)
|
80
82
|
rspec-expectations (~> 2.14.0)
|
81
83
|
rspec-mocks (~> 2.14.0)
|
82
|
-
sprockets (2.12.
|
84
|
+
sprockets (2.12.2)
|
83
85
|
hike (~> 1.2)
|
84
86
|
multi_json (~> 1.0)
|
85
87
|
rack (~> 1.0)
|
@@ -104,5 +106,7 @@ PLATFORMS
|
|
104
106
|
DEPENDENCIES
|
105
107
|
api_pagination_headers!
|
106
108
|
factory_girl_rails
|
109
|
+
kaminari
|
107
110
|
rspec-rails
|
108
111
|
sqlite3
|
112
|
+
will_paginate
|
data/README.md
CHANGED
@@ -1,29 +1,39 @@
|
|
1
1
|
#api_pagination_headers [](http://travis-ci.org/richardkall/api_pagination_headers) [](https://gemnasium.com/richardkall/api_pagination_headers)
|
2
2
|
|
3
|
-
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 `
|
3
|
+
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 `Total-Count` header.
|
4
4
|
|
5
5
|
Example:
|
6
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
|
-
|
9
|
+
Total-Count: 11
|
10
10
|
```
|
11
11
|
|
12
12
|
##Installation
|
13
13
|
|
14
14
|
Include in your Gemfile:
|
15
15
|
|
16
|
-
|
16
|
+
* Rails 4
|
17
|
+
|
18
|
+
```ruby
|
17
19
|
gem 'api_pagination_headers'
|
18
20
|
```
|
19
21
|
|
20
|
-
|
22
|
+
* Rails 3.2
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
gem 'api_pagination_headers', branch: 'rails-3-2'
|
26
|
+
```
|
27
|
+
|
28
|
+
Depending on [Kaminari](https://github.com/amatsuda/kaminari) or [will_paginate](https://github.com/mislav/will_paginate) at the moment. Support for [rails-api](https://github.com/rails-api/rails-api) is built in.
|
21
29
|
|
22
30
|
##Usage
|
23
31
|
|
24
32
|
In the controller:
|
25
33
|
|
26
|
-
|
34
|
+
* Rails 4
|
35
|
+
|
36
|
+
```ruby
|
27
37
|
class PostsController < ApplicationController
|
28
38
|
after_action only: [:index] { set_pagination_headers(:posts) }
|
29
39
|
|
@@ -34,5 +44,28 @@ class PostsController < ApplicationController
|
|
34
44
|
end
|
35
45
|
```
|
36
46
|
|
47
|
+
* Rails 3.2
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
class PostsController < ApplicationController
|
51
|
+
after_filter only: [:index] { set_pagination_headers(:posts) }
|
52
|
+
|
53
|
+
def index
|
54
|
+
@posts = Post.paginate(per_page: params[:per_page], page: params[:page])
|
55
|
+
respond_with @posts
|
56
|
+
end
|
57
|
+
end
|
58
|
+
```
|
59
|
+
|
60
|
+
##Configuration
|
61
|
+
|
62
|
+
You can change the `Total-Count` header title by configuring the gem.
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
ApiPaginationHeaders.configure do |config|
|
66
|
+
config.total_count_header = 'X-Total-Count'
|
67
|
+
end
|
68
|
+
```
|
69
|
+
|
37
70
|
##License
|
38
71
|
Copyright © 2014 [Richard Käll](http://richardkall.se). Licensed under the MIT license.
|
@@ -16,9 +16,10 @@ Gem::Specification.new do |s|
|
|
16
16
|
s.test_files = `git ls-files -- {spec}/*`.split("\n")
|
17
17
|
|
18
18
|
s.add_dependency 'rails', '~> 4.0'
|
19
|
-
s.add_dependency 'will_paginate', '~> 3.0.5'
|
20
19
|
|
21
20
|
s.add_development_dependency 'factory_girl_rails'
|
22
21
|
s.add_development_dependency 'sqlite3'
|
23
22
|
s.add_development_dependency 'rspec-rails'
|
23
|
+
s.add_development_dependency 'kaminari'
|
24
|
+
s.add_development_dependency 'will_paginate'
|
24
25
|
end
|
@@ -1,3 +1,8 @@
|
|
1
|
+
require 'active_support/configurable'
|
2
|
+
|
3
|
+
require 'api_pagination_headers/version'
|
4
|
+
require 'api_pagination_headers/config'
|
5
|
+
|
1
6
|
module ApiPaginationHeaders
|
2
7
|
protected
|
3
8
|
|
@@ -7,14 +12,20 @@ module ApiPaginationHeaders
|
|
7
12
|
links = create_links(pages, scope)
|
8
13
|
|
9
14
|
headers['Link'] = links.join(', ') unless links.empty?
|
10
|
-
headers[
|
15
|
+
headers[ApiPaginationHeaders.config.total_count_header] = defined?(Kaminari) ? "#{scope.total_count}" : "#{scope.total_entries}"
|
11
16
|
end
|
12
17
|
|
13
18
|
private
|
14
19
|
|
15
20
|
def create_links(pages, scope)
|
16
21
|
url_without_params = request.url.split('?').first
|
17
|
-
|
22
|
+
|
23
|
+
if params[:per_page]
|
24
|
+
per_page = params[:per_page].to_i
|
25
|
+
else
|
26
|
+
per_page = defined?(Kaminari) ? scope.default_per_page : scope.per_page
|
27
|
+
end
|
28
|
+
|
18
29
|
links = []
|
19
30
|
pages.each do |key, value|
|
20
31
|
new_params = request.query_parameters.merge({ page: value, per_page: per_page })
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'active_support/configurable'
|
2
|
+
|
3
|
+
module ApiPaginationHeaders
|
4
|
+
# Configures global settings for ApiPaginationHeaders
|
5
|
+
# ApiPaginationHeaders.configure do |config|
|
6
|
+
# config.total_count_header = 'X-Total-Count'
|
7
|
+
# end
|
8
|
+
def self.configure(&block)
|
9
|
+
yield @config ||= ApiPaginationHeaders::Configuration.new
|
10
|
+
end
|
11
|
+
|
12
|
+
# Global settings for ApiPaginationHeaders
|
13
|
+
def self.config
|
14
|
+
@config ||= ApiPaginationHeaders::Configuration.new
|
15
|
+
end
|
16
|
+
|
17
|
+
class Configuration #:nodoc:
|
18
|
+
include ActiveSupport::Configurable
|
19
|
+
|
20
|
+
TOTAL_COUNT_HEADER = 'Total-Count'
|
21
|
+
|
22
|
+
config_accessor(:total_count_header) { TOTAL_COUNT_HEADER }
|
23
|
+
end
|
24
|
+
end
|
@@ -9,9 +9,9 @@ describe PostsController, '#index' do
|
|
9
9
|
expect(response.headers).not_to include('Link')
|
10
10
|
end
|
11
11
|
|
12
|
-
it 'responds with
|
12
|
+
it 'responds with Total-Count header' do
|
13
13
|
get :index, format: :json
|
14
|
-
expect(response.headers['
|
14
|
+
expect(response.headers['Total-Count']).to eq('1')
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
@@ -24,9 +24,9 @@ describe PostsController, '#index' do
|
|
24
24
|
expect(response.headers['Link']).to eq("<#{posts_url(page: 2, per_page: 1, format: :json)}>; rel=\"next\", <#{posts_url(page: 2, per_page: 1, format: :json)}>; rel=\"last\"")
|
25
25
|
end
|
26
26
|
|
27
|
-
it 'responds with
|
27
|
+
it 'responds with Total-Count header' do
|
28
28
|
get :index, format: :json
|
29
|
-
expect(response.headers['
|
29
|
+
expect(response.headers['Total-Count']).to eq('2')
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
@@ -36,9 +36,9 @@ describe PostsController, '#index' do
|
|
36
36
|
expect(response.headers['Link']).to eq("<#{posts_url(page: 1, per_page: 1, format: :json)}>; rel=\"first\", <#{posts_url(page: 1, per_page: 1, format: :json)}>; rel=\"prev\"")
|
37
37
|
end
|
38
38
|
|
39
|
-
it 'responds with
|
39
|
+
it 'responds with Total-Count header' do
|
40
40
|
get :index, format: :json
|
41
|
-
expect(response.headers['
|
41
|
+
expect(response.headers['Total-Count']).to eq('2')
|
42
42
|
end
|
43
43
|
end
|
44
44
|
end
|
@@ -52,9 +52,9 @@ describe PostsController, '#index' do
|
|
52
52
|
expect(response.headers['Link']).to eq("<#{posts_url(page: 2, per_page: 1, format: :json)}>; rel=\"next\", <#{posts_url(page: 3, per_page: 1, format: :json)}>; rel=\"last\"")
|
53
53
|
end
|
54
54
|
|
55
|
-
it 'responds with
|
55
|
+
it 'responds with Total-Count header' do
|
56
56
|
get :index, format: :json
|
57
|
-
expect(response.headers['
|
57
|
+
expect(response.headers['Total-Count']).to eq('3')
|
58
58
|
end
|
59
59
|
end
|
60
60
|
|
@@ -64,9 +64,9 @@ describe PostsController, '#index' do
|
|
64
64
|
expect(response.headers['Link']).to eq("<#{posts_url(page: 1, per_page: 1, format: :json)}>; rel=\"first\", <#{posts_url(page: 1, per_page: 1, format: :json)}>; rel=\"prev\", <#{posts_url(page: 3, per_page: 1, format: :json)}>; rel=\"next\", <#{posts_url(page: 3, per_page: 1, format: :json)}>; rel=\"last\"")
|
65
65
|
end
|
66
66
|
|
67
|
-
it 'responds with
|
67
|
+
it 'responds with Total-Count header' do
|
68
68
|
get :index, format: :json
|
69
|
-
expect(response.headers['
|
69
|
+
expect(response.headers['Total-Count']).to eq('3')
|
70
70
|
end
|
71
71
|
end
|
72
72
|
|
@@ -76,9 +76,9 @@ describe PostsController, '#index' do
|
|
76
76
|
expect(response.headers['Link']).to eq("<#{posts_url(page: 1, per_page: 1, format: :json)}>; rel=\"first\", <#{posts_url(page: 2, per_page: 1, format: :json)}>; rel=\"prev\"")
|
77
77
|
end
|
78
78
|
|
79
|
-
it 'responds with
|
79
|
+
it 'responds with Total-Count header' do
|
80
80
|
get :index, format: :json
|
81
|
-
expect(response.headers['
|
81
|
+
expect(response.headers['Total-Count']).to eq('3')
|
82
82
|
end
|
83
83
|
end
|
84
84
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: api_pagination_headers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Richard Käll
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-10-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -25,21 +25,21 @@ dependencies:
|
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '4.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: factory_girl_rails
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
34
|
-
type: :
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
40
|
+
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: sqlite3
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - ">="
|
@@ -53,7 +53,7 @@ dependencies:
|
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
56
|
+
name: rspec-rails
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - ">="
|
@@ -67,7 +67,21 @@ dependencies:
|
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
|
-
name:
|
70
|
+
name: kaminari
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: will_paginate
|
71
85
|
requirement: !ruby/object:Gem::Requirement
|
72
86
|
requirements:
|
73
87
|
- - ">="
|
@@ -96,6 +110,7 @@ files:
|
|
96
110
|
- Rakefile
|
97
111
|
- api_pagination_headers.gemspec
|
98
112
|
- lib/api_pagination_headers.rb
|
113
|
+
- lib/api_pagination_headers/config.rb
|
99
114
|
- lib/api_pagination_headers/version.rb
|
100
115
|
- spec/controllers/comments_controller_spec.rb
|
101
116
|
- spec/controllers/posts_controller_spec.rb
|
@@ -144,7 +159,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
144
159
|
version: '0'
|
145
160
|
requirements: []
|
146
161
|
rubyforge_project:
|
147
|
-
rubygems_version: 2.
|
162
|
+
rubygems_version: 2.4.2
|
148
163
|
signing_key:
|
149
164
|
specification_version: 4
|
150
165
|
summary: Link header pagination for APIs
|