grape-kaminari 0.1.9 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +0 -1
- data/.rubocop.yml +26 -0
- data/.travis.yml +6 -0
- data/CHANGELOG.md +48 -0
- data/Gemfile.lock +117 -0
- data/LICENSE.txt +1 -0
- data/README.md +9 -10
- data/Rakefile +6 -3
- data/grape-kaminari.gemspec +19 -19
- data/lib/grape-kaminari.rb +1 -11
- data/lib/grape/kaminari.rb +55 -35
- data/lib/grape/kaminari/max_value_validator.rb +7 -16
- data/lib/grape/kaminari/version.rb +1 -1
- data/spec/kaminari_spec.rb +89 -96
- data/spec/paginate_helper_spec.rb +51 -13
- data/spec/spec_helper.rb +2 -6
- metadata +55 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d524d760fab53973250ae175e7e484a22c0e1d76ca3a427b42d51118660fd708
|
4
|
+
data.tar.gz: 2c1e66d494b6b2dafdd9e7831022a7f1a6b8b10652d19acc897da2f3b150d52b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '065287dc5984a1817a7d92142c806e15e23f3aca99d510fee6dfecb710c7d3eacfe2e755d37605f4055d4ec175f4cd0994367dd2b2cdf3de54f26c2cc89961b4'
|
7
|
+
data.tar.gz: a0ac7c9bdac412351c1581794942982213252e1b2d0907ed1727c3be384353c926d9865da30e2cbd694211f0ce4858c2032533b9ca8a215c33fcd76cec658cca
|
data/.gitignore
CHANGED
data/.rubocop.yml
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: "2.5"
|
3
|
+
NewCops: enable
|
4
|
+
|
5
|
+
Metrics/AbcSize:
|
6
|
+
Enabled: false
|
7
|
+
Metrics/BlockLength:
|
8
|
+
Exclude:
|
9
|
+
- spec/**/*_spec.rb
|
10
|
+
Metrics/MethodLength:
|
11
|
+
Max: 20
|
12
|
+
|
13
|
+
Naming/FileName:
|
14
|
+
Exclude:
|
15
|
+
- lib/grape-kaminari.rb
|
16
|
+
|
17
|
+
Style/Documentation:
|
18
|
+
Enabled: false
|
19
|
+
Style/FrozenStringLiteralComment:
|
20
|
+
Enabled: false
|
21
|
+
Style/TrailingCommaInArguments:
|
22
|
+
EnforcedStyleForMultiline: consistent_comma
|
23
|
+
Style/TrailingCommaInArrayLiteral:
|
24
|
+
EnforcedStyleForMultiline: consistent_comma
|
25
|
+
Style/TrailingCommaInHashLiteral:
|
26
|
+
EnforcedStyleForMultiline: consistent_comma
|
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
### 0.5.0 (Next)
|
2
|
+
|
3
|
+
#### Features
|
4
|
+
|
5
|
+
* Your contribution here.
|
6
|
+
|
7
|
+
#### Fixes
|
8
|
+
|
9
|
+
* Your contribution here.
|
10
|
+
|
11
|
+
### 0.4.1
|
12
|
+
|
13
|
+
#### Features
|
14
|
+
|
15
|
+
* [#58](https://github.com/bsm/grape-kaminari/pull/58): Allow to paginate without counting - [@dim](https://github.com/dim).
|
16
|
+
|
17
|
+
### 0.4.0
|
18
|
+
|
19
|
+
#### Features
|
20
|
+
|
21
|
+
* [#57](https://github.com/bsm/grape-kaminari/pull/57): Introduce new params based API - [@dim](https://github.com/dim).
|
22
|
+
* [#56](https://github.com/bsm/grape-kaminari/pull/56): Introduce a CHANGELOG - [@dim](https://github.com/dim).
|
23
|
+
|
24
|
+
#### Fixes
|
25
|
+
|
26
|
+
* [#57](https://github.com/bsm/grape-kaminari/pull/57): Fix issues related to Grape v1.5 release - [@dim](https://github.com/dim).
|
27
|
+
|
28
|
+
### 0.3.0 (2020/08/10)
|
29
|
+
|
30
|
+
* [#54](https://github.com/bsm/grape-kaminari/pull/54): Inherit paginate helper - [@dim](https://github.com/dim).
|
31
|
+
|
32
|
+
### 0.2.1 (2020/06/18)
|
33
|
+
|
34
|
+
#### Fixes
|
35
|
+
|
36
|
+
* [#52](https://github.com/bsm/grape-kaminari/pull/52): Skip per-page range validation for non-integer values - [@dim](https://github.com/dim).
|
37
|
+
|
38
|
+
### 0.2.0 (2020/06/18)
|
39
|
+
|
40
|
+
Revived project, migrated to [github.com/bsm/grape-kaminari](github.com/bsm/grape-kaminari).
|
41
|
+
|
42
|
+
#### Features
|
43
|
+
|
44
|
+
* [#51](https://github.com/bsm/grape-kaminari/pull/51): Support for Grape 1.x - [@dim](https://github.com/dim).
|
45
|
+
|
46
|
+
#### Fixes
|
47
|
+
|
48
|
+
* [#51](https://github.com/bsm/grape-kaminari/pull/51): Fixed code style - [@dim](https://github.com/dim).
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
grape-kaminari (0.4.1)
|
5
|
+
grape (>= 1.0, != 1.4.0)
|
6
|
+
kaminari-grape
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
activesupport (6.0.3.4)
|
12
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
13
|
+
i18n (>= 0.7, < 2)
|
14
|
+
minitest (~> 5.1)
|
15
|
+
tzinfo (~> 1.1)
|
16
|
+
zeitwerk (~> 2.2, >= 2.2.2)
|
17
|
+
ast (2.4.1)
|
18
|
+
builder (3.2.4)
|
19
|
+
concurrent-ruby (1.1.7)
|
20
|
+
diff-lcs (1.4.4)
|
21
|
+
dry-configurable (0.11.6)
|
22
|
+
concurrent-ruby (~> 1.0)
|
23
|
+
dry-core (~> 0.4, >= 0.4.7)
|
24
|
+
dry-equalizer (~> 0.2)
|
25
|
+
dry-container (0.7.2)
|
26
|
+
concurrent-ruby (~> 1.0)
|
27
|
+
dry-configurable (~> 0.1, >= 0.1.3)
|
28
|
+
dry-core (0.4.9)
|
29
|
+
concurrent-ruby (~> 1.0)
|
30
|
+
dry-equalizer (0.3.0)
|
31
|
+
dry-inflector (0.2.0)
|
32
|
+
dry-logic (1.0.8)
|
33
|
+
concurrent-ruby (~> 1.0)
|
34
|
+
dry-core (~> 0.2)
|
35
|
+
dry-equalizer (~> 0.2)
|
36
|
+
dry-types (1.4.0)
|
37
|
+
concurrent-ruby (~> 1.0)
|
38
|
+
dry-container (~> 0.3)
|
39
|
+
dry-core (~> 0.4, >= 0.4.4)
|
40
|
+
dry-equalizer (~> 0.3)
|
41
|
+
dry-inflector (~> 0.1, >= 0.1.2)
|
42
|
+
dry-logic (~> 1.0, >= 1.0.2)
|
43
|
+
grape (1.5.0)
|
44
|
+
activesupport
|
45
|
+
builder
|
46
|
+
dry-types (>= 1.1)
|
47
|
+
mustermann-grape (~> 1.0.0)
|
48
|
+
rack (>= 1.3.0)
|
49
|
+
rack-accept
|
50
|
+
i18n (1.8.5)
|
51
|
+
concurrent-ruby (~> 1.0)
|
52
|
+
kaminari-core (1.2.1)
|
53
|
+
kaminari-grape (1.0.1)
|
54
|
+
grape
|
55
|
+
kaminari-core (~> 1.0)
|
56
|
+
minitest (5.14.2)
|
57
|
+
mustermann (1.1.1)
|
58
|
+
ruby2_keywords (~> 0.0.1)
|
59
|
+
mustermann-grape (1.0.1)
|
60
|
+
mustermann (>= 1.0.0)
|
61
|
+
parallel (1.20.0)
|
62
|
+
parser (2.7.2.0)
|
63
|
+
ast (~> 2.4.1)
|
64
|
+
rack (2.2.3)
|
65
|
+
rack-accept (0.4.5)
|
66
|
+
rack (>= 0.4)
|
67
|
+
rack-test (1.1.0)
|
68
|
+
rack (>= 1.0, < 3)
|
69
|
+
rainbow (3.0.0)
|
70
|
+
rake (13.0.1)
|
71
|
+
regexp_parser (1.8.2)
|
72
|
+
rexml (3.2.4)
|
73
|
+
rspec (3.10.0)
|
74
|
+
rspec-core (~> 3.10.0)
|
75
|
+
rspec-expectations (~> 3.10.0)
|
76
|
+
rspec-mocks (~> 3.10.0)
|
77
|
+
rspec-core (3.10.0)
|
78
|
+
rspec-support (~> 3.10.0)
|
79
|
+
rspec-expectations (3.10.0)
|
80
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
81
|
+
rspec-support (~> 3.10.0)
|
82
|
+
rspec-mocks (3.10.0)
|
83
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
84
|
+
rspec-support (~> 3.10.0)
|
85
|
+
rspec-support (3.10.0)
|
86
|
+
rubocop (1.2.0)
|
87
|
+
parallel (~> 1.10)
|
88
|
+
parser (>= 2.7.1.5)
|
89
|
+
rainbow (>= 2.2.2, < 4.0)
|
90
|
+
regexp_parser (>= 1.8)
|
91
|
+
rexml
|
92
|
+
rubocop-ast (>= 1.0.1)
|
93
|
+
ruby-progressbar (~> 1.7)
|
94
|
+
unicode-display_width (>= 1.4.0, < 2.0)
|
95
|
+
rubocop-ast (1.1.1)
|
96
|
+
parser (>= 2.7.1.5)
|
97
|
+
ruby-progressbar (1.10.1)
|
98
|
+
ruby2_keywords (0.0.2)
|
99
|
+
thread_safe (0.3.6)
|
100
|
+
tzinfo (1.2.8)
|
101
|
+
thread_safe (~> 0.1)
|
102
|
+
unicode-display_width (1.7.0)
|
103
|
+
zeitwerk (2.4.1)
|
104
|
+
|
105
|
+
PLATFORMS
|
106
|
+
ruby
|
107
|
+
|
108
|
+
DEPENDENCIES
|
109
|
+
bundler
|
110
|
+
grape-kaminari!
|
111
|
+
rack-test
|
112
|
+
rake
|
113
|
+
rspec
|
114
|
+
rubocop
|
115
|
+
|
116
|
+
BUNDLED WITH
|
117
|
+
2.1.4
|
data/LICENSE.txt
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
The MIT License (MIT)
|
2
2
|
|
3
3
|
Copyright (c) 2013-2014 Monterail.com LLC
|
4
|
+
Copyright (c) 2020 Black Square Media Ltd
|
4
5
|
|
5
6
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
6
7
|
this software and associated documentation files (the "Software"), to deal in
|
data/README.md
CHANGED
@@ -1,6 +1,9 @@
|
|
1
|
-
# Grape::Kaminari
|
1
|
+
# Grape::Kaminari
|
2
2
|
|
3
|
-
[
|
3
|
+
[![Build Status](https://travis-ci.org/bsm/grape-kaminari.png?branch=master)](https://travis-ci.org/bsm/grape-kaminari)
|
4
|
+
[![License](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
|
5
|
+
|
6
|
+
[kaminari](https://github.com/kaminari/kaminari) paginator integration for [grape](https://github.com/intridea/grape) API framework.
|
4
7
|
|
5
8
|
## Installation
|
6
9
|
|
@@ -32,7 +35,6 @@ class MyApi < Grape::API
|
|
32
35
|
resource :posts do
|
33
36
|
desc 'Return a list of posts.'
|
34
37
|
|
35
|
-
# Annotate action with `paginate`.
|
36
38
|
# This will add three optional params: page, per_page, and offset
|
37
39
|
#
|
38
40
|
# You can optionally overwrite the default :per_page setting (10)
|
@@ -42,8 +44,9 @@ class MyApi < Grape::API
|
|
42
44
|
# You can disable the offset parameter from appearing in the API
|
43
45
|
# documentation by setting it to false.
|
44
46
|
#
|
45
|
-
|
46
|
-
|
47
|
+
params do
|
48
|
+
use :pagination, per_page: 20, max_per_page: 30, offset: 5
|
49
|
+
end
|
47
50
|
get do
|
48
51
|
posts = Post.where(...)
|
49
52
|
|
@@ -87,8 +90,4 @@ X-Offset: 10
|
|
87
90
|
|
88
91
|
## Contributing
|
89
92
|
|
90
|
-
|
91
|
-
2. Create your feature branch (`git checkout -b my-new-feature`)
|
92
|
-
3. Commit your changes (`git commit -am 'Add some feature'`)
|
93
|
-
4. Push to the branch (`git push origin my-new-feature`)
|
94
|
-
5. Create new Pull Request
|
93
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/bsm/grape-kaminari.
|
data/Rakefile
CHANGED
data/grape-kaminari.gemspec
CHANGED
@@ -1,28 +1,28 @@
|
|
1
|
-
|
2
|
-
lib = File.expand_path('../lib', __FILE__)
|
1
|
+
lib = File.expand_path('lib', __dir__)
|
3
2
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
3
|
require 'grape/kaminari/version'
|
5
4
|
|
6
5
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name =
|
6
|
+
spec.name = 'grape-kaminari'
|
8
7
|
spec.version = Grape::Kaminari::VERSION
|
9
|
-
spec.authors = [
|
10
|
-
spec.email = [
|
11
|
-
spec.description =
|
12
|
-
spec.summary =
|
13
|
-
spec.homepage =
|
14
|
-
spec.license =
|
8
|
+
spec.authors = ['Tymon Tobolski', 'Black Square Media']
|
9
|
+
spec.email = ['info@blacksquaremedia.com']
|
10
|
+
spec.description = 'kaminari paginator integration for grape API framework'
|
11
|
+
spec.summary = 'kaminari integration for grape'
|
12
|
+
spec.homepage = ''
|
13
|
+
spec.license = 'MIT'
|
15
14
|
|
16
|
-
spec.files = `git ls-files`.split(
|
17
|
-
spec.
|
18
|
-
spec.
|
19
|
-
spec.
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.start_with?('spec/') }
|
16
|
+
spec.test_files = `git ls-files -z -- spec/*`.split("\x0")
|
17
|
+
spec.require_paths = ['lib']
|
18
|
+
spec.required_ruby_version = '>= 2.5'
|
20
19
|
|
20
|
+
spec.add_runtime_dependency 'grape', '>= 1.0', '!= 1.4.0'
|
21
|
+
spec.add_runtime_dependency 'kaminari-grape'
|
21
22
|
|
22
|
-
spec.
|
23
|
-
spec.
|
24
|
-
|
25
|
-
spec.add_development_dependency
|
26
|
-
spec.add_development_dependency
|
27
|
-
spec.add_development_dependency 'rspec', '~> 2.9'
|
23
|
+
spec.add_development_dependency 'bundler'
|
24
|
+
spec.add_development_dependency 'rack-test'
|
25
|
+
spec.add_development_dependency 'rake'
|
26
|
+
spec.add_development_dependency 'rspec'
|
27
|
+
spec.add_development_dependency 'rubocop'
|
28
28
|
end
|
data/lib/grape-kaminari.rb
CHANGED
data/lib/grape/kaminari.rb
CHANGED
@@ -1,45 +1,65 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
1
|
+
require 'grape'
|
2
|
+
require 'grape/kaminari/version'
|
3
|
+
require 'grape/kaminari/max_value_validator'
|
4
|
+
require 'kaminari/grape'
|
5
5
|
|
6
6
|
module Grape
|
7
7
|
module Kaminari
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
8
|
+
extend ActiveSupport::Concern
|
9
|
+
|
10
|
+
included do
|
11
|
+
helpers HelperMethods
|
12
|
+
end
|
13
|
+
|
14
|
+
module HelperMethods # :nodoc:
|
15
|
+
extend Grape::API::Helpers
|
16
|
+
|
17
|
+
params :pagination do |opts = {}|
|
18
|
+
opts.reverse_merge!(
|
19
|
+
per_page: ::Kaminari.config.default_per_page || 10,
|
20
|
+
max_per_page: ::Kaminari.config.max_per_page,
|
21
|
+
offset: 0,
|
22
|
+
)
|
23
|
+
|
24
|
+
optional :page, type: Integer, default: 1,
|
25
|
+
desc: 'Page offset to fetch.'
|
26
|
+
optional :per_page, type: Integer, default: opts[:per_page],
|
27
|
+
desc: 'Number of results to return per page.',
|
28
|
+
max_value: opts[:max_per_page]
|
29
|
+
|
30
|
+
if opts[:offset].is_a?(Integer)
|
31
|
+
optional :offset, type: Integer, default: opts[:offset],
|
32
|
+
desc: 'Pad a number of results.'
|
22
33
|
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def paginate(collection, without_count: false)
|
37
|
+
coll = collection.page(params[:page].to_i)
|
38
|
+
.per(params[:per_page].to_i)
|
39
|
+
.padding(params[:offset].to_i)
|
40
|
+
coll = coll.without_count if without_count && coll.respond_to?(:without_count)
|
41
|
+
|
42
|
+
unless without_count
|
43
|
+
header 'X-Total', coll.total_count.to_s
|
44
|
+
header 'X-Total-Pages', coll.total_pages.to_s
|
45
|
+
end
|
46
|
+
header 'X-Per-Page', coll.limit_value.to_s
|
47
|
+
header 'X-Page', coll.current_page.to_s
|
48
|
+
header 'X-Next-Page', coll.next_page.to_s
|
49
|
+
header 'X-Prev-Page', coll.prev_page.to_s
|
50
|
+
header 'X-Offset', params[:offset].to_s
|
51
|
+
|
52
|
+
coll
|
53
|
+
end
|
54
|
+
end
|
23
55
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
offset: 0
|
29
|
-
)
|
30
|
-
params do
|
31
|
-
optional :page, type: Integer, default: 1,
|
32
|
-
desc: 'Page offset to fetch.'
|
33
|
-
optional :per_page, type: Integer, default: options[:per_page],
|
34
|
-
desc: 'Number of results to return per page.',
|
35
|
-
max_value: options[:max_per_page]
|
36
|
-
if options[:offset].is_a? Numeric
|
37
|
-
optional :offset, type: Integer, default: options[:offset],
|
38
|
-
desc: 'Pad a number of results.'
|
39
|
-
end
|
40
|
-
end
|
56
|
+
module DSLMethods # :nodoc:
|
57
|
+
def paginate(opts = {})
|
58
|
+
params do
|
59
|
+
use(:pagination, opts)
|
41
60
|
end
|
42
61
|
end
|
43
62
|
end
|
63
|
+
Grape::API::Instance.extend(DSLMethods)
|
44
64
|
end
|
45
65
|
end
|
@@ -1,23 +1,14 @@
|
|
1
1
|
module Grape
|
2
2
|
module Kaminari
|
3
|
-
|
4
|
-
Grape::Validations::Base
|
5
|
-
else
|
6
|
-
Grape::Validations::SingleOptionValidator
|
7
|
-
end
|
8
|
-
|
9
|
-
class MaxValueValidator < base
|
3
|
+
class MaxValueValidator < Grape::Validations::Base
|
10
4
|
def validate_param!(attr_name, params)
|
11
|
-
return unless params[attr_name]
|
12
|
-
|
13
5
|
attr = params[attr_name]
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
end
|
6
|
+
return unless attr.is_a?(Integer) && @option && attr > @option
|
7
|
+
|
8
|
+
raise Grape::Exceptions::Validation.new(
|
9
|
+
params: [@scope.full_name(attr_name)],
|
10
|
+
message: "must be less than or equal #{@option}",
|
11
|
+
)
|
21
12
|
end
|
22
13
|
end
|
23
14
|
end
|
data/spec/kaminari_spec.rb
CHANGED
@@ -1,138 +1,131 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
class UnPaginatedAPI < Grape::API
|
4
|
-
# Intentionally not including Grape::Kaminari
|
5
|
-
end
|
6
|
-
|
7
3
|
class PaginatedAPI < Grape::API
|
8
4
|
include Grape::Kaminari
|
9
5
|
end
|
10
6
|
|
11
7
|
describe Grape::Kaminari do
|
8
|
+
subject { Class.new(PaginatedAPI) }
|
12
9
|
|
13
|
-
|
14
|
-
subject
|
15
|
-
|
16
|
-
it 'raises an error' do
|
17
|
-
expect { subject.paginate }.to raise_error(NoMethodError, /undefined method `paginate' for/i)
|
18
|
-
end
|
10
|
+
def declared_params
|
11
|
+
subject.namespace_stackable(:declared_params).flatten
|
19
12
|
end
|
20
13
|
|
21
|
-
|
22
|
-
subject {
|
23
|
-
|
24
|
-
|
25
|
-
subject.paginate
|
26
|
-
if Grape::Kaminari.post_0_9_0_grape?
|
27
|
-
expect(subject.inheritable_setting.route[:declared_params]).to eq([:page, :per_page, :offset])
|
28
|
-
else
|
29
|
-
expect(subject.settings[:declared_params]).to eq([:page, :per_page, :offset])
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
describe 'descriptions, validation, and defaults' do
|
34
|
-
before do
|
35
|
-
subject.paginate
|
36
|
-
subject.get '/' do; end
|
37
|
-
end
|
38
|
-
let(:params) {subject.routes.first.route_params}
|
39
|
-
|
40
|
-
it 'does not require :page' do
|
41
|
-
expect(params['page'][:required]).to eq(false)
|
42
|
-
end
|
43
|
-
|
44
|
-
it 'does not require :per_page' do
|
45
|
-
expect(params['per_page'][:required]).to eq(false)
|
46
|
-
end
|
47
|
-
|
48
|
-
it 'does not require :offset' do
|
49
|
-
expect(params['offset'][:required]).to eq(false)
|
50
|
-
end
|
14
|
+
it 'adds to declared parameters' do
|
15
|
+
subject.params { use :pagination }
|
16
|
+
expect(declared_params).to eq(%i[page per_page offset])
|
17
|
+
end
|
51
18
|
|
52
|
-
|
53
|
-
|
54
|
-
|
19
|
+
it 'may exclude :offset' do
|
20
|
+
subject.params { use :pagination, offset: false }
|
21
|
+
expect(declared_params).to eq(%i[page per_page])
|
22
|
+
end
|
55
23
|
|
56
|
-
|
57
|
-
|
58
|
-
|
24
|
+
it 'should support legacy declarations' do
|
25
|
+
subject.paginate
|
26
|
+
expect(declared_params).to eq(%i[page per_page offset])
|
27
|
+
end
|
59
28
|
|
60
|
-
|
61
|
-
|
62
|
-
|
29
|
+
it 'should not stumble across repeated declarations' do
|
30
|
+
subject.paginate offset: false
|
31
|
+
subject.params do
|
32
|
+
optional :extra
|
33
|
+
end
|
34
|
+
expect(declared_params).to eq(%i[page per_page extra])
|
35
|
+
end
|
63
36
|
|
64
|
-
|
65
|
-
|
66
|
-
end
|
37
|
+
describe 'descriptions, validation, and defaults' do
|
38
|
+
let(:params) { subject.routes.first.params }
|
67
39
|
|
68
|
-
|
69
|
-
|
70
|
-
|
40
|
+
before do
|
41
|
+
subject.params { use :pagination }
|
42
|
+
subject.get('/') { 'OK' }
|
43
|
+
end
|
71
44
|
|
72
|
-
|
73
|
-
|
74
|
-
|
45
|
+
it 'does not require :page' do
|
46
|
+
expect(params['page'][:required]).to eq(false)
|
47
|
+
end
|
75
48
|
|
76
|
-
|
77
|
-
|
78
|
-
|
49
|
+
it 'does not require :per_page' do
|
50
|
+
expect(params['per_page'][:required]).to eq(false)
|
51
|
+
end
|
79
52
|
|
80
|
-
|
81
|
-
|
82
|
-
|
53
|
+
it 'does not require :offset' do
|
54
|
+
expect(params['offset'][:required]).to eq(false)
|
55
|
+
end
|
83
56
|
|
84
|
-
|
85
|
-
|
86
|
-
|
57
|
+
it 'describes :page' do
|
58
|
+
expect(params['page'][:desc]).to eq('Page offset to fetch.')
|
59
|
+
end
|
87
60
|
|
61
|
+
it 'describes :per_page' do
|
62
|
+
expect(params['per_page'][:desc]).to eq('Number of results to return per page.')
|
63
|
+
end
|
88
64
|
|
65
|
+
it 'describes :offset' do
|
66
|
+
expect(params['offset'][:desc]).to eq('Pad a number of results.')
|
89
67
|
end
|
90
68
|
|
91
|
-
|
69
|
+
it 'validates :page as Integer' do
|
70
|
+
expect(params['page'][:type]).to eq('Integer')
|
71
|
+
end
|
92
72
|
|
93
|
-
|
94
|
-
|
95
|
-
|
73
|
+
it 'validates :per_page as Integer' do
|
74
|
+
expect(params['per_page'][:type]).to eq('Integer')
|
75
|
+
end
|
96
76
|
|
97
|
-
|
98
|
-
|
99
|
-
subject.get '/' do; end
|
77
|
+
it 'validates :offset as Integer' do
|
78
|
+
expect(params['offset'][:type]).to eq('Integer')
|
100
79
|
end
|
101
|
-
let(:params) {subject.routes.first.route_params}
|
102
80
|
|
103
|
-
it 'defaults :
|
104
|
-
expect(params['
|
81
|
+
it 'defaults :page to 1' do
|
82
|
+
expect(params['page'][:default]).to eq(1)
|
105
83
|
end
|
106
84
|
|
107
|
-
it '
|
108
|
-
|
109
|
-
expect(last_response.status).to eq 200
|
85
|
+
it 'defaults :per_page to Kaminari.config.default_per_page' do
|
86
|
+
expect(params['per_page'][:default]).to eq(::Kaminari.config.default_per_page)
|
110
87
|
end
|
111
88
|
|
112
|
-
it '
|
113
|
-
|
114
|
-
expect(last_response.status).to eq 400
|
115
|
-
expect(last_response.body).to match /per_page must be less than 999/
|
89
|
+
it 'defaults :offset to 0' do
|
90
|
+
expect(params['offset'][:default]).to eq(0)
|
116
91
|
end
|
92
|
+
end
|
93
|
+
end
|
117
94
|
|
118
|
-
|
119
|
-
|
95
|
+
describe 'custom paginated api' do
|
96
|
+
subject { Class.new(PaginatedAPI) }
|
97
|
+
let(:app) { subject }
|
98
|
+
let(:params) { subject.routes.first.params }
|
99
|
+
|
100
|
+
before do
|
101
|
+
subject.params do
|
102
|
+
use :pagination, per_page: 99, max_per_page: 999, offset: 9
|
120
103
|
end
|
104
|
+
subject.get('/') { 'OK' }
|
105
|
+
end
|
121
106
|
|
107
|
+
it 'defaults :per_page to customized value' do
|
108
|
+
expect(params['per_page'][:default]).to eq(99)
|
122
109
|
end
|
123
110
|
|
124
|
-
|
125
|
-
|
111
|
+
it 'succeeds when :per_page is within :max_value' do
|
112
|
+
get('/', page: 1, per_page: 999)
|
113
|
+
expect(last_response.status).to eq(200)
|
114
|
+
end
|
126
115
|
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
expect(subject.settings[:declared_params]).not_to include(:offset)
|
133
|
-
end
|
134
|
-
end
|
116
|
+
it 'ensures :per_page is within :max_value' do
|
117
|
+
get('/', page: 1, per_page: 1_000)
|
118
|
+
expect(last_response.status).to eq(400)
|
119
|
+
expect(last_response.body).to match(/per_page must be less than or equal 999/)
|
120
|
+
end
|
135
121
|
|
122
|
+
it 'ensures :per_page is numeric' do
|
123
|
+
get('/', page: 1, per_page: 'foo')
|
124
|
+
expect(last_response.status).to eq(400)
|
125
|
+
expect(last_response.body).to match(/per_page is invalid/)
|
136
126
|
end
|
137
127
|
|
128
|
+
it 'defaults :offset to customized value' do
|
129
|
+
expect(params['offset'][:default]).to eq(9)
|
130
|
+
end
|
138
131
|
end
|
@@ -4,25 +4,44 @@ require 'json'
|
|
4
4
|
class PaginatedAPI < Grape::API
|
5
5
|
include Grape::Kaminari
|
6
6
|
|
7
|
-
|
7
|
+
params do
|
8
|
+
use :pagination
|
9
|
+
end
|
8
10
|
get '' do
|
9
11
|
paginate(Kaminari.paginate_array((1..10).to_a))
|
10
12
|
end
|
11
13
|
|
12
|
-
|
14
|
+
params do
|
15
|
+
use :pagination, offset: false
|
16
|
+
end
|
13
17
|
get 'no-offset' do
|
14
18
|
paginate(Kaminari.paginate_array((1..10).to_a))
|
15
19
|
end
|
20
|
+
|
21
|
+
params do
|
22
|
+
use :pagination, offset: false
|
23
|
+
end
|
24
|
+
get 'no-count' do
|
25
|
+
paginate(Kaminari.paginate_array((1..10).to_a), without_count: true)
|
26
|
+
end
|
27
|
+
|
28
|
+
resource :sub do
|
29
|
+
params do
|
30
|
+
use :pagination, per_page: 2
|
31
|
+
end
|
32
|
+
get '/' do
|
33
|
+
paginate(Kaminari.paginate_array((1..10).to_a))
|
34
|
+
end
|
35
|
+
end
|
16
36
|
end
|
17
37
|
|
18
38
|
describe Grape::Kaminari do
|
19
39
|
subject { PaginatedAPI.new }
|
20
|
-
|
40
|
+
let(:app) { subject }
|
21
41
|
let(:json) { JSON.parse(last_response.body) }
|
22
42
|
let(:header) { last_response.header }
|
23
43
|
|
24
44
|
describe 'paginated helper' do
|
25
|
-
|
26
45
|
it 'returns the first page' do
|
27
46
|
get '/', page: 1, per_page: 3
|
28
47
|
expect(json).to eq [1, 2, 3]
|
@@ -40,18 +59,37 @@ describe Grape::Kaminari do
|
|
40
59
|
expect(json).to eq [1, 2, 3]
|
41
60
|
end
|
42
61
|
|
62
|
+
it 'works without count' do
|
63
|
+
get '/no-count', page: 2, per_page: 3
|
64
|
+
expect(json).to eq [4, 5, 6]
|
65
|
+
expect(header).to include(
|
66
|
+
'X-Per-Page' => '3',
|
67
|
+
'X-Page' => '2',
|
68
|
+
'X-Next-Page' => '3',
|
69
|
+
'X-Prev-Page' => '1',
|
70
|
+
)
|
71
|
+
expect(header).not_to include(
|
72
|
+
'X-Total',
|
73
|
+
'X-Total-Pages',
|
74
|
+
)
|
75
|
+
end
|
76
|
+
|
43
77
|
it 'sets headers' do
|
44
78
|
get '/', page: 3, per_page: 2, offset: 1
|
45
|
-
expect(header
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
79
|
+
expect(header).to include(
|
80
|
+
'X-Total' => '10',
|
81
|
+
'X-Total-Pages' => '5',
|
82
|
+
'X-Per-Page' => '2',
|
83
|
+
'X-Page' => '3',
|
84
|
+
'X-Next-Page' => '4',
|
85
|
+
'X-Prev-Page' => '2',
|
86
|
+
'X-Offset' => '1',
|
87
|
+
)
|
52
88
|
end
|
53
89
|
|
90
|
+
it 'can be inherited' do
|
91
|
+
get '/sub', page: 1
|
92
|
+
expect(json).to eq [1, 2]
|
93
|
+
end
|
54
94
|
end
|
55
|
-
|
56
|
-
|
57
95
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,19 +1,15 @@
|
|
1
|
-
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
-
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
1
|
require 'rspec'
|
4
|
-
require 'kaminari'
|
5
|
-
require 'grape'
|
6
2
|
require 'grape-kaminari'
|
7
3
|
require 'rack/test'
|
8
4
|
|
9
5
|
# Requires supporting files with custom matchers and macros, etc,
|
10
6
|
# in ./support/ and its subdirectories.
|
11
|
-
Dir[
|
7
|
+
Dir[File.expand_path('./support/**/*.rb', __dir__)].sort.each { |f| require f }
|
12
8
|
|
13
9
|
I18n.enforce_available_locales = false
|
14
10
|
|
15
11
|
RSpec.configure do |config|
|
16
12
|
config.include Rack::Test::Methods
|
17
|
-
config.
|
13
|
+
config.raise_errors_for_deprecations!
|
18
14
|
config.order = 'random'
|
19
15
|
end
|
metadata
CHANGED
@@ -1,14 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: grape-kaminari
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tymon Tobolski
|
8
|
+
- Black Square Media
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
|
-
date:
|
12
|
+
date: 2020-11-09 00:00:00.000000000 Z
|
12
13
|
dependencies:
|
13
14
|
- !ruby/object:Gem::Dependency
|
14
15
|
name: grape
|
@@ -16,16 +17,22 @@ dependencies:
|
|
16
17
|
requirements:
|
17
18
|
- - ">="
|
18
19
|
- !ruby/object:Gem::Version
|
19
|
-
version: '0'
|
20
|
+
version: '1.0'
|
21
|
+
- - "!="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.4.0
|
20
24
|
type: :runtime
|
21
25
|
prerelease: false
|
22
26
|
version_requirements: !ruby/object:Gem::Requirement
|
23
27
|
requirements:
|
24
28
|
- - ">="
|
25
29
|
- !ruby/object:Gem::Version
|
26
|
-
version: '0'
|
30
|
+
version: '1.0'
|
31
|
+
- - "!="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.4.0
|
27
34
|
- !ruby/object:Gem::Dependency
|
28
|
-
name: kaminari
|
35
|
+
name: kaminari-grape
|
29
36
|
requirement: !ruby/object:Gem::Requirement
|
30
37
|
requirements:
|
31
38
|
- - ">="
|
@@ -42,16 +49,30 @@ dependencies:
|
|
42
49
|
name: bundler
|
43
50
|
requirement: !ruby/object:Gem::Requirement
|
44
51
|
requirements:
|
45
|
-
- - "
|
52
|
+
- - ">="
|
46
53
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
54
|
+
version: '0'
|
48
55
|
type: :development
|
49
56
|
prerelease: false
|
50
57
|
version_requirements: !ruby/object:Gem::Requirement
|
51
58
|
requirements:
|
52
|
-
- - "
|
59
|
+
- - ">="
|
53
60
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rack-test
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
type: :development
|
70
|
+
prerelease: false
|
71
|
+
version_requirements: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
55
76
|
- !ruby/object:Gem::Dependency
|
56
77
|
name: rake
|
57
78
|
requirement: !ruby/object:Gem::Requirement
|
@@ -70,25 +91,43 @@ dependencies:
|
|
70
91
|
name: rspec
|
71
92
|
requirement: !ruby/object:Gem::Requirement
|
72
93
|
requirements:
|
73
|
-
- - "
|
94
|
+
- - ">="
|
74
95
|
- !ruby/object:Gem::Version
|
75
|
-
version: '
|
96
|
+
version: '0'
|
76
97
|
type: :development
|
77
98
|
prerelease: false
|
78
99
|
version_requirements: !ruby/object:Gem::Requirement
|
79
100
|
requirements:
|
80
|
-
- - "
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
- !ruby/object:Gem::Dependency
|
105
|
+
name: rubocop
|
106
|
+
requirement: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
81
109
|
- !ruby/object:Gem::Version
|
82
|
-
version: '
|
110
|
+
version: '0'
|
111
|
+
type: :development
|
112
|
+
prerelease: false
|
113
|
+
version_requirements: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
83
118
|
description: kaminari paginator integration for grape API framework
|
84
119
|
email:
|
85
|
-
-
|
120
|
+
- info@blacksquaremedia.com
|
86
121
|
executables: []
|
87
122
|
extensions: []
|
88
123
|
extra_rdoc_files: []
|
89
124
|
files:
|
90
125
|
- ".gitignore"
|
126
|
+
- ".rubocop.yml"
|
127
|
+
- ".travis.yml"
|
128
|
+
- CHANGELOG.md
|
91
129
|
- Gemfile
|
130
|
+
- Gemfile.lock
|
92
131
|
- LICENSE.txt
|
93
132
|
- README.md
|
94
133
|
- Rakefile
|
@@ -112,15 +151,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
112
151
|
requirements:
|
113
152
|
- - ">="
|
114
153
|
- !ruby/object:Gem::Version
|
115
|
-
version: '
|
154
|
+
version: '2.5'
|
116
155
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
117
156
|
requirements:
|
118
157
|
- - ">="
|
119
158
|
- !ruby/object:Gem::Version
|
120
159
|
version: '0'
|
121
160
|
requirements: []
|
122
|
-
|
123
|
-
rubygems_version: 2.4.5.1
|
161
|
+
rubygems_version: 3.1.2
|
124
162
|
signing_key:
|
125
163
|
specification_version: 4
|
126
164
|
summary: kaminari integration for grape
|