api-pagination 4.1.1 → 4.2.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/lib/api-pagination.rb +3 -18
- data/lib/api-pagination/configuration.rb +6 -0
- data/lib/api-pagination/version.rb +2 -2
- data/lib/grape/pagination.rb +6 -3
- data/lib/rails/pagination.rb +4 -1
- data/spec/api-pagination_spec.rb +30 -0
- data/spec/grape_spec.rb +33 -0
- data/spec/rails_spec.rb +22 -0
- metadata +19 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e8bc41a88d9698b56c9329d03b153e4a26359a89
|
4
|
+
data.tar.gz: 5b12ab2abac9d7fa0ad8569b7c80811c35cc3d7c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 04d2fe7e9d7df6f1b5bf3ca3e623594d9f01934ec8aaf98147fd759a196a5c778d4fda5946e38ab5b1164d68db0b9d641b6473b23bb90b1ed54abc068f44687a
|
7
|
+
data.tar.gz: 02c3c45d64667438376d0243ab3671d36f1e0f6360881c1a37398e35a25a4252a7653b0bce98cce739aefc7e228c7eb430895ba4f60be7fb1591ba4bb88b8d28
|
data/lib/api-pagination.rb
CHANGED
@@ -10,7 +10,7 @@ module ApiPagination
|
|
10
10
|
|
11
11
|
case ApiPagination.config.paginator
|
12
12
|
when :kaminari
|
13
|
-
paginate_with_kaminari(collection, options)
|
13
|
+
paginate_with_kaminari(collection, options, options[:paginate_array_options] || {})
|
14
14
|
when :will_paginate
|
15
15
|
paginate_with_will_paginate(collection, options)
|
16
16
|
else
|
@@ -39,31 +39,16 @@ module ApiPagination
|
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
42
|
-
def paginator
|
43
|
-
warn "[DEPRECATION] ApiPagination.paginator is deprecated. Please use ApiPagination.config.paginator"
|
44
|
-
config.paginator
|
45
|
-
end
|
46
|
-
|
47
|
-
def per_page_header
|
48
|
-
warn "[DEPRECATION] ApiPagination.paginator is deprecated. Please use ApiPagination.config.per_page_header"
|
49
|
-
config.per_page_header
|
50
|
-
end
|
51
|
-
|
52
|
-
def total_header
|
53
|
-
warn "[DEPRECATION] ApiPagination.paginator is deprecated. Please use ApiPagination.config.total_header"
|
54
|
-
config.total_header
|
55
|
-
end
|
56
|
-
|
57
42
|
private
|
58
43
|
|
59
|
-
def paginate_with_kaminari(collection, options)
|
44
|
+
def paginate_with_kaminari(collection, options, paginate_array_options = {})
|
60
45
|
if Kaminari.config.max_per_page && options[:per_page] > Kaminari.config.max_per_page
|
61
46
|
options[:per_page] = Kaminari.config.max_per_page
|
62
47
|
elsif options[:per_page] <= 0
|
63
48
|
options[:per_page] = Kaminari.config.default_per_page
|
64
49
|
end
|
65
50
|
|
66
|
-
collection = Kaminari.paginate_array(collection) if collection.is_a?(Array)
|
51
|
+
collection = Kaminari.paginate_array(collection, paginate_array_options) if collection.is_a?(Array)
|
67
52
|
collection.page(options[:page]).per(options[:per_page])
|
68
53
|
end
|
69
54
|
|
@@ -4,6 +4,10 @@ module ApiPagination
|
|
4
4
|
|
5
5
|
attr_accessor :per_page_header
|
6
6
|
|
7
|
+
attr_accessor :page_header
|
8
|
+
|
9
|
+
attr_accessor :include_total
|
10
|
+
|
7
11
|
def configure(&block)
|
8
12
|
yield self
|
9
13
|
end
|
@@ -11,6 +15,8 @@ module ApiPagination
|
|
11
15
|
def initialize
|
12
16
|
@total_header = 'Total'
|
13
17
|
@per_page_header = 'Per-Page'
|
18
|
+
@page_header = nil
|
19
|
+
@include_total = true
|
14
20
|
end
|
15
21
|
|
16
22
|
def paginator
|
data/lib/grape/pagination.rb
CHANGED
@@ -15,17 +15,20 @@ module Grape
|
|
15
15
|
pages = ApiPagination.pages_from(collection)
|
16
16
|
|
17
17
|
pages.each do |k, v|
|
18
|
-
old_params = Rack::Utils.
|
18
|
+
old_params = Rack::Utils.parse_nested_query(request.query_string)
|
19
19
|
new_params = old_params.merge('page' => v)
|
20
20
|
links << %(<#{url}?#{new_params.to_param}>; rel="#{k}")
|
21
21
|
end
|
22
22
|
|
23
23
|
total_header = ApiPagination.config.total_header
|
24
24
|
per_page_header = ApiPagination.config.per_page_header
|
25
|
+
page_header = ApiPagination.config.page_header
|
26
|
+
include_total = ApiPagination.config.include_total
|
25
27
|
|
26
28
|
header 'Link', links.join(', ') unless links.empty?
|
27
|
-
header total_header, ApiPagination.total_from(collection)
|
29
|
+
header total_header, ApiPagination.total_from(collection) if include_total
|
28
30
|
header per_page_header, options[:per_page].to_s
|
31
|
+
header page_header, options[:page].to_s unless page_header.nil?
|
29
32
|
|
30
33
|
return collection
|
31
34
|
end
|
@@ -33,7 +36,7 @@ module Grape
|
|
33
36
|
|
34
37
|
base.class_eval do
|
35
38
|
def self.paginate(options = {})
|
36
|
-
route_setting :per_page,
|
39
|
+
route_setting :per_page, options[:per_page]
|
37
40
|
route_setting :max_per_page, options[:max_per_page]
|
38
41
|
params do
|
39
42
|
optional :page, :type => Integer, :default => 1,
|
data/lib/rails/pagination.rb
CHANGED
@@ -41,10 +41,13 @@ module Rails
|
|
41
41
|
|
42
42
|
total_header = ApiPagination.config.total_header
|
43
43
|
per_page_header = ApiPagination.config.per_page_header
|
44
|
+
page_header = ApiPagination.config.page_header
|
45
|
+
include_total = ApiPagination.config.include_total
|
44
46
|
|
45
47
|
headers['Link'] = links.join(', ') unless links.empty?
|
46
|
-
headers[total_header] = ApiPagination.total_from(collection)
|
48
|
+
headers[total_header] = ApiPagination.total_from(collection) if include_total
|
47
49
|
headers[per_page_header] = options[:per_page].to_s
|
50
|
+
headers[page_header] = options[:page].to_s unless page_header.nil?
|
48
51
|
|
49
52
|
return collection
|
50
53
|
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ApiPagination do
|
4
|
+
let(:collection) { (1..100).to_a }
|
5
|
+
let(:paginate_array_options) { { total_count: 1000 } }
|
6
|
+
|
7
|
+
context 'Using kaminari' do
|
8
|
+
before do
|
9
|
+
ApiPagination.config.paginator = :kaminari
|
10
|
+
end
|
11
|
+
|
12
|
+
after do
|
13
|
+
ApiPagination.config.paginator = ENV['PAGINATOR'].to_sym
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should accept paginate_array_options option' do
|
17
|
+
expect(Kaminari).to receive(:paginate_array)
|
18
|
+
.with(collection, paginate_array_options)
|
19
|
+
.and_call_original
|
20
|
+
|
21
|
+
ApiPagination.paginate(
|
22
|
+
collection,
|
23
|
+
{
|
24
|
+
per_page: 30,
|
25
|
+
paginate_array_options: paginate_array_options
|
26
|
+
}
|
27
|
+
)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/spec/grape_spec.rb
CHANGED
@@ -71,6 +71,7 @@ describe NumbersAPI do
|
|
71
71
|
before do
|
72
72
|
ApiPagination.config.total_header = 'X-Total-Count'
|
73
73
|
ApiPagination.config.per_page_header = 'X-Per-Page'
|
74
|
+
ApiPagination.config.page_header = 'X-Page'
|
74
75
|
|
75
76
|
get '/numbers', count: 10
|
76
77
|
end
|
@@ -78,10 +79,12 @@ describe NumbersAPI do
|
|
78
79
|
after do
|
79
80
|
ApiPagination.config.total_header = 'Total'
|
80
81
|
ApiPagination.config.per_page_header = 'Per-Page'
|
82
|
+
ApiPagination.config.page_header = nil
|
81
83
|
end
|
82
84
|
|
83
85
|
let(:total) { last_response.header['X-Total-Count'].to_i }
|
84
86
|
let(:per_page) { last_response.header['X-Per-Page'].to_i }
|
87
|
+
let(:page) { last_response.header['X-Page'].to_i }
|
85
88
|
|
86
89
|
it 'should give a X-Total-Count header' do
|
87
90
|
headers_keys = last_response.headers.keys
|
@@ -98,6 +101,36 @@ describe NumbersAPI do
|
|
98
101
|
expect(headers_keys).to include('X-Per-Page')
|
99
102
|
expect(per_page).to eq(10)
|
100
103
|
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
|
111
|
+
end
|
112
|
+
|
113
|
+
context 'configured not to include the total' do
|
114
|
+
before { ApiPagination.config.include_total = false }
|
115
|
+
|
116
|
+
it 'should not include a Total header' do
|
117
|
+
get '/numbers', count: 10
|
118
|
+
|
119
|
+
expect(last_response.header['Total']).to be_nil
|
120
|
+
end
|
121
|
+
|
122
|
+
after { ApiPagination.config.include_total = true }
|
123
|
+
end
|
124
|
+
|
125
|
+
context 'with query string including array parameter' do
|
126
|
+
before do
|
127
|
+
get '/numbers', { count: 100, parity: ['odd', 'even']}
|
128
|
+
end
|
129
|
+
|
130
|
+
it 'returns links with with same received parameters' do
|
131
|
+
expect(links).to include('<http://example.org/numbers?count=100&page=10&parity%5B%5D=odd&parity%5B%5D=even>; rel="last"')
|
132
|
+
expect(links).to include('<http://example.org/numbers?count=100&page=2&parity%5B%5D=odd&parity%5B%5D=even>; rel="next"')
|
133
|
+
end
|
101
134
|
end
|
102
135
|
end
|
103
136
|
end
|
data/spec/rails_spec.rb
CHANGED
@@ -73,6 +73,7 @@ describe NumbersController, :type => :controller do
|
|
73
73
|
before do
|
74
74
|
ApiPagination.config.total_header = 'X-Total-Count'
|
75
75
|
ApiPagination.config.per_page_header = 'X-Per-Page'
|
76
|
+
ApiPagination.config.page_header = 'X-Page'
|
76
77
|
|
77
78
|
get :index, count: 10
|
78
79
|
end
|
@@ -80,10 +81,12 @@ describe NumbersController, :type => :controller do
|
|
80
81
|
after do
|
81
82
|
ApiPagination.config.total_header = 'Total'
|
82
83
|
ApiPagination.config.per_page_header = 'Per-Page'
|
84
|
+
ApiPagination.config.page_header = nil
|
83
85
|
end
|
84
86
|
|
85
87
|
let(:total) { response.header['X-Total-Count'].to_i }
|
86
88
|
let(:per_page) { response.header['X-Per-Page'].to_i }
|
89
|
+
let(:page) { response.header['X-Page'].to_i }
|
87
90
|
|
88
91
|
it 'should give a X-Total-Count header' do
|
89
92
|
headers_keys = response.headers.keys
|
@@ -100,6 +103,25 @@ describe NumbersController, :type => :controller do
|
|
100
103
|
expect(headers_keys).to include('X-Per-Page')
|
101
104
|
expect(per_page).to eq(10)
|
102
105
|
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
|
113
|
+
end
|
114
|
+
|
115
|
+
context 'configured not to include the total' do
|
116
|
+
before { ApiPagination.config.include_total = false }
|
117
|
+
|
118
|
+
it 'should not include a Total header' do
|
119
|
+
get :index, count: 10
|
120
|
+
|
121
|
+
expect(response.header['Total']).to be_nil
|
122
|
+
end
|
123
|
+
|
124
|
+
after { ApiPagination.config.include_total = true }
|
103
125
|
end
|
104
126
|
end
|
105
127
|
end
|
metadata
CHANGED
@@ -1,83 +1,83 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: api-pagination
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 4.
|
4
|
+
version: 4.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Celis
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-01-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - ~>
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '3.0'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - ~>
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '3.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: grape
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: 0.10.0
|
34
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
40
|
version: 0.10.0
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: railties
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: 3.0.0
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: 3.0.0
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: actionpack
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- -
|
59
|
+
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: 3.0.0
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- -
|
66
|
+
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: 3.0.0
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: sequel
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- -
|
73
|
+
- - ">="
|
74
74
|
- !ruby/object:Gem::Version
|
75
75
|
version: 4.9.0
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- -
|
80
|
+
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: 4.9.0
|
83
83
|
description: Link header pagination for Rails and Grape APIs
|
@@ -87,12 +87,13 @@ executables: []
|
|
87
87
|
extensions: []
|
88
88
|
extra_rdoc_files: []
|
89
89
|
files:
|
90
|
+
- lib/api-pagination.rb
|
90
91
|
- lib/api-pagination/configuration.rb
|
91
92
|
- lib/api-pagination/hooks.rb
|
92
93
|
- lib/api-pagination/version.rb
|
93
|
-
- lib/api-pagination.rb
|
94
94
|
- lib/grape/pagination.rb
|
95
95
|
- lib/rails/pagination.rb
|
96
|
+
- spec/api-pagination_spec.rb
|
96
97
|
- spec/grape_spec.rb
|
97
98
|
- spec/rails_spec.rb
|
98
99
|
- spec/sequel_spec.rb
|
@@ -113,21 +114,22 @@ require_paths:
|
|
113
114
|
- lib
|
114
115
|
required_ruby_version: !ruby/object:Gem::Requirement
|
115
116
|
requirements:
|
116
|
-
- -
|
117
|
+
- - ">="
|
117
118
|
- !ruby/object:Gem::Version
|
118
119
|
version: '0'
|
119
120
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
120
121
|
requirements:
|
121
|
-
- -
|
122
|
+
- - ">="
|
122
123
|
- !ruby/object:Gem::Version
|
123
124
|
version: '0'
|
124
125
|
requirements: []
|
125
126
|
rubyforge_project:
|
126
|
-
rubygems_version: 2.
|
127
|
+
rubygems_version: 2.5.1
|
127
128
|
signing_key:
|
128
129
|
specification_version: 4
|
129
130
|
summary: Link header pagination for Rails and Grape APIs. Don't use the request body.
|
130
131
|
test_files:
|
132
|
+
- spec/api-pagination_spec.rb
|
131
133
|
- spec/grape_spec.rb
|
132
134
|
- spec/rails_spec.rb
|
133
135
|
- spec/sequel_spec.rb
|
@@ -138,3 +140,4 @@ test_files:
|
|
138
140
|
- spec/support/shared_examples/first_page.rb
|
139
141
|
- spec/support/shared_examples/last_page.rb
|
140
142
|
- spec/support/shared_examples/middle_page.rb
|
143
|
+
has_rdoc:
|