api-pagination 4.7.0 → 4.7.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/api-pagination/version.rb +1 -1
- data/lib/grape/pagination.rb +9 -4
- data/spec/grape_spec.rb +21 -1
- data/spec/support/numbers_api.rb +19 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f4cf55ba5db9b488ae0e9e16e9d14b839a8ada930697c39c00d1192f7cfc5456
|
4
|
+
data.tar.gz: a79c32b444ab9b6b84d3f5bbf6312feb46a031ae5ee8b930b3dd553456b207d2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6208062d94a1f57d62be3cbf2cd552d6fe86a2af30ae3dde1d2efe1f3ff2c629215ec69aa6c5fd04452ce99be6e79c9395af76a0821e5f4fb039d44c470b8d44
|
7
|
+
data.tar.gz: c91fa40ad83fade4d1d76f0977828b7263d07b6d5d3b1b47e76a65f9fb3bd8530ed143548b6b89e8ba1b1f73b0a04a3498cd0bfb0e3f4541a379aad4e4b5ac38
|
data/lib/grape/pagination.rb
CHANGED
@@ -39,11 +39,16 @@ module Grape
|
|
39
39
|
def self.paginate(options = {})
|
40
40
|
route_setting :per_page, options[:per_page]
|
41
41
|
route_setting :max_per_page, options[:max_per_page]
|
42
|
+
|
43
|
+
enforce_max_per_page = options[:max_per_page] && options[:enforce_max_per_page]
|
44
|
+
per_page_values = enforce_max_per_page ? 0..options[:max_per_page] : nil
|
45
|
+
|
42
46
|
params do
|
43
|
-
optional :page, :type
|
44
|
-
:desc
|
45
|
-
optional :per_page, :type
|
46
|
-
:desc
|
47
|
+
optional :page, :type => Integer, :default => 1,
|
48
|
+
:desc => 'Page of results to fetch.'
|
49
|
+
optional :per_page, :type => Integer,
|
50
|
+
:desc => 'Number of results to return per page.',
|
51
|
+
:values => per_page_values
|
47
52
|
end
|
48
53
|
end
|
49
54
|
end
|
data/spec/grape_spec.rb
CHANGED
@@ -56,15 +56,35 @@ describe NumbersAPI do
|
|
56
56
|
it_behaves_like 'an endpoint with a middle page'
|
57
57
|
end
|
58
58
|
|
59
|
-
context '
|
59
|
+
context 'without a max_per_page setting' do
|
60
60
|
before { get '/numbers', :count => 100, :per_page => 30 }
|
61
61
|
|
62
|
+
it 'should list all numbers within per page in the response body' do
|
63
|
+
body = '[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30]'
|
64
|
+
|
65
|
+
expect(last_response.body).to eq(body)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
context 'with a max_per_page setting not enforced' do
|
70
|
+
before { get '/numbers_with_max_per_page', :count => 100, :per_page => 30 }
|
71
|
+
|
62
72
|
it 'should not go above the max_per_page_limit' do
|
63
73
|
body = '[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]'
|
64
74
|
|
65
75
|
expect(last_response.body).to eq(body)
|
66
76
|
end
|
67
77
|
end
|
78
|
+
|
79
|
+
context 'with a max_per_page setting enforced' do
|
80
|
+
before { get '/numbers_with_enforced_max_per_page', :count => 100, :per_page => 30 }
|
81
|
+
|
82
|
+
it 'should not allow value above the max_per_page_limit' do
|
83
|
+
body = '{"error":"per_page does not have a valid value"}'
|
84
|
+
|
85
|
+
expect(last_response.body).to eq(body)
|
86
|
+
end
|
87
|
+
end
|
68
88
|
end
|
69
89
|
|
70
90
|
context 'with custom response headers' do
|
data/spec/support/numbers_api.rb
CHANGED
@@ -5,7 +5,7 @@ class NumbersAPI < Grape::API
|
|
5
5
|
format :json
|
6
6
|
|
7
7
|
desc 'Return some paginated set of numbers'
|
8
|
-
paginate :per_page => 10
|
8
|
+
paginate :per_page => 10
|
9
9
|
params do
|
10
10
|
requires :count, :type => Integer
|
11
11
|
optional :with_headers, :default => false, :type => Boolean
|
@@ -20,4 +20,22 @@ class NumbersAPI < Grape::API
|
|
20
20
|
|
21
21
|
paginate (1..params[:count]).to_a
|
22
22
|
end
|
23
|
+
|
24
|
+
desc 'Return some paginated set of numbers with max_per_page'
|
25
|
+
paginate :per_page => 10, :max_per_page => 25
|
26
|
+
params do
|
27
|
+
requires :count, :type => Integer
|
28
|
+
end
|
29
|
+
get :numbers_with_max_per_page do
|
30
|
+
paginate (1..params[:count]).to_a
|
31
|
+
end
|
32
|
+
|
33
|
+
desc 'Return some paginated set of numbers with max_per_page enforced'
|
34
|
+
paginate :per_page => 10, :max_per_page => 25, :enforce_max_per_page => true
|
35
|
+
params do
|
36
|
+
requires :count, :type => Integer
|
37
|
+
end
|
38
|
+
get :numbers_with_enforced_max_per_page do
|
39
|
+
paginate (1..params[:count]).to_a
|
40
|
+
end
|
23
41
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: api-pagination
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 4.7.
|
4
|
+
version: 4.7.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Celis
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-04-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -124,7 +124,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
124
124
|
version: '0'
|
125
125
|
requirements: []
|
126
126
|
rubyforge_project:
|
127
|
-
rubygems_version: 2.7.
|
127
|
+
rubygems_version: 2.7.6
|
128
128
|
signing_key:
|
129
129
|
specification_version: 4
|
130
130
|
summary: Link header pagination for Rails and Grape APIs. Don't use the request body.
|