grape-kaminari 0.1.5 → 0.2.0
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/Gemfile.lock +117 -0
- data/LICENSE.txt +1 -0
- data/README.md +21 -9
- data/Rakefile +8 -1
- data/grape-kaminari.gemspec +19 -18
- data/lib/grape-kaminari.rb +1 -1
- data/lib/grape/kaminari.rb +40 -29
- data/lib/grape/kaminari/max_value_validator.rb +7 -6
- data/lib/grape/kaminari/version.rb +1 -1
- data/spec/kaminari_spec.rb +124 -0
- data/spec/paginate_helper_spec.rb +55 -0
- data/spec/spec_helper.rb +15 -0
- metadata +74 -23
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 39954bcb02e83af2783635756ce5d94942edbbd0550c3670e467ba39eb9f2b00
|
4
|
+
data.tar.gz: 6c15582e16ae68fa09d7e1fd9480188d96bd7be0cd4c48cdc376cdddc03ed06d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 107535d26da91163d588390787b298b41e886466c6904eb59edbea9cae52643f43c5f4d418976a347b6704cbfdd67f8b0013532c6dfacc98c3db4900058b37fe
|
7
|
+
data.tar.gz: dc4b45493fe12f1dfd454437d12714850d97445062296162ab177780ef1c5bf0bbd9bb1b3330fe87fb7a0318ac400d6c1491c9851fabf46f1532c6bb8c17a3e5
|
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/Gemfile.lock
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
grape-kaminari (0.2.0)
|
5
|
+
grape (>= 1.0)
|
6
|
+
kaminari-grape
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
activesupport (6.0.3.2)
|
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.6)
|
20
|
+
diff-lcs (1.3)
|
21
|
+
dry-configurable (0.11.5)
|
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.6)
|
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.3.3)
|
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.3)
|
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.1)
|
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.19.2)
|
62
|
+
parser (2.7.1.3)
|
63
|
+
ast (~> 2.4.0)
|
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.7.1)
|
72
|
+
rexml (3.2.4)
|
73
|
+
rspec (3.9.0)
|
74
|
+
rspec-core (~> 3.9.0)
|
75
|
+
rspec-expectations (~> 3.9.0)
|
76
|
+
rspec-mocks (~> 3.9.0)
|
77
|
+
rspec-core (3.9.2)
|
78
|
+
rspec-support (~> 3.9.3)
|
79
|
+
rspec-expectations (3.9.2)
|
80
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
81
|
+
rspec-support (~> 3.9.0)
|
82
|
+
rspec-mocks (3.9.1)
|
83
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
84
|
+
rspec-support (~> 3.9.0)
|
85
|
+
rspec-support (3.9.3)
|
86
|
+
rubocop (0.85.1)
|
87
|
+
parallel (~> 1.10)
|
88
|
+
parser (>= 2.7.0.1)
|
89
|
+
rainbow (>= 2.2.2, < 4.0)
|
90
|
+
regexp_parser (>= 1.7)
|
91
|
+
rexml
|
92
|
+
rubocop-ast (>= 0.0.3)
|
93
|
+
ruby-progressbar (~> 1.7)
|
94
|
+
unicode-display_width (>= 1.4.0, < 2.0)
|
95
|
+
rubocop-ast (0.0.3)
|
96
|
+
parser (>= 2.7.0.1)
|
97
|
+
ruby-progressbar (1.10.1)
|
98
|
+
ruby2_keywords (0.0.2)
|
99
|
+
thread_safe (0.3.6)
|
100
|
+
tzinfo (1.2.7)
|
101
|
+
thread_safe (~> 0.1)
|
102
|
+
unicode-display_width (1.7.0)
|
103
|
+
zeitwerk (2.3.0)
|
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
|
|
@@ -33,11 +36,16 @@ class MyApi < Grape::API
|
|
33
36
|
desc 'Return a list of posts.'
|
34
37
|
|
35
38
|
# Annotate action with `paginate`.
|
36
|
-
# This will add
|
39
|
+
# This will add three optional params: page, per_page, and offset
|
40
|
+
#
|
37
41
|
# You can optionally overwrite the default :per_page setting (10)
|
38
42
|
# and the :max_per_page(false/disabled) setting which will use a validator to
|
39
43
|
# check that per_page is below the given number.
|
40
|
-
|
44
|
+
#
|
45
|
+
# You can disable the offset parameter from appearing in the API
|
46
|
+
# documentation by setting it to false.
|
47
|
+
#
|
48
|
+
paginate per_page: 20, max_per_page: 30, offset: 5
|
41
49
|
|
42
50
|
get do
|
43
51
|
posts = Post.where(...)
|
@@ -46,6 +54,14 @@ class MyApi < Grape::API
|
|
46
54
|
# with arguments automatically passed from params
|
47
55
|
paginate(posts)
|
48
56
|
end
|
57
|
+
|
58
|
+
get do
|
59
|
+
things = ['a', 'standard', 'array', 'of', 'things', '...']
|
60
|
+
|
61
|
+
# Use `Kaminari.paginate_array` method to convert the array
|
62
|
+
# into an object that can be passed to `paginate` helper.
|
63
|
+
paginate(Kaminari.paginate_array(things))
|
64
|
+
end
|
49
65
|
end
|
50
66
|
end
|
51
67
|
```
|
@@ -74,8 +90,4 @@ X-Offset: 10
|
|
74
90
|
|
75
91
|
## Contributing
|
76
92
|
|
77
|
-
|
78
|
-
2. Create your feature branch (`git checkout -b my-new-feature`)
|
79
|
-
3. Commit your changes (`git commit -am 'Add some feature'`)
|
80
|
-
4. Push to the branch (`git push origin my-new-feature`)
|
81
|
-
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,27 +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'
|
21
|
+
spec.add_runtime_dependency 'kaminari-grape'
|
21
22
|
|
22
|
-
spec.
|
23
|
-
spec.
|
24
|
-
|
25
|
-
spec.add_development_dependency
|
26
|
-
spec.add_development_dependency
|
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'
|
27
28
|
end
|
data/lib/grape-kaminari.rb
CHANGED
@@ -1 +1 @@
|
|
1
|
-
require
|
1
|
+
require 'grape/kaminari'
|
data/lib/grape/kaminari.rb
CHANGED
@@ -1,40 +1,51 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
1
|
+
require 'grape'
|
2
|
+
require 'grape/kaminari/version'
|
3
|
+
require 'grape/kaminari/max_value_validator'
|
4
|
+
require 'kaminari/grape'
|
4
5
|
|
5
6
|
module Grape
|
6
7
|
module Kaminari
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
end
|
8
|
+
module HelperMethods # :nodoc:
|
9
|
+
def paginate(collection)
|
10
|
+
collection.page(params[:page].to_i)
|
11
|
+
.per(params[:per_page].to_i)
|
12
|
+
.padding(params[:offset].to_i)
|
13
|
+
.tap do |data|
|
14
|
+
header 'X-Total', data.total_count.to_s
|
15
|
+
header 'X-Total-Pages', data.total_pages.to_s
|
16
|
+
header 'X-Per-Page', data.limit_value.to_s
|
17
|
+
header 'X-Page', data.current_page.to_s
|
18
|
+
header 'X-Next-Page', data.next_page.to_s
|
19
|
+
header 'X-Prev-Page', data.prev_page.to_s
|
20
|
+
header 'X-Offset', params[:offset].to_s
|
21
21
|
end
|
22
|
+
end
|
23
|
+
end
|
22
24
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
25
|
+
module ClassMethods # :nodoc:
|
26
|
+
def paginate(**options)
|
27
|
+
options.reverse_merge!(
|
28
|
+
per_page: ::Kaminari.config.default_per_page || 10,
|
29
|
+
max_per_page: ::Kaminari.config.max_per_page,
|
30
|
+
offset: 0,
|
31
|
+
)
|
32
|
+
params do
|
33
|
+
optional :page, type: Integer, default: 1,
|
34
|
+
desc: 'Page offset to fetch.'
|
35
|
+
optional :per_page, type: Integer, default: options[:per_page],
|
36
|
+
desc: 'Number of results to return per page.',
|
37
|
+
max_value: options[:max_per_page]
|
38
|
+
if options[:offset].is_a? Numeric
|
39
|
+
optional :offset, type: Integer, default: options[:offset],
|
40
|
+
desc: 'Pad a number of results.'
|
35
41
|
end
|
36
42
|
end
|
37
43
|
end
|
38
44
|
end
|
45
|
+
|
46
|
+
def self.included(base)
|
47
|
+
base.helpers HelperMethods
|
48
|
+
base.extend ClassMethods
|
49
|
+
end
|
39
50
|
end
|
40
51
|
end
|
@@ -1,13 +1,14 @@
|
|
1
1
|
module Grape
|
2
2
|
module Kaminari
|
3
|
-
class MaxValueValidator < Grape::Validations::
|
3
|
+
class MaxValueValidator < Grape::Validations::Base
|
4
4
|
def validate_param!(attr_name, params)
|
5
|
-
return unless params[attr_name]
|
6
|
-
|
7
5
|
attr = params[attr_name]
|
8
|
-
|
9
|
-
|
10
|
-
|
6
|
+
return unless attr && @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
|
+
)
|
11
12
|
end
|
12
13
|
end
|
13
14
|
end
|
@@ -0,0 +1,124 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class UnPaginatedAPI < Grape::API
|
4
|
+
# Intentionally not including Grape::Kaminari
|
5
|
+
end
|
6
|
+
|
7
|
+
class PaginatedAPI < Grape::API
|
8
|
+
include Grape::Kaminari
|
9
|
+
end
|
10
|
+
|
11
|
+
describe Grape::Kaminari do
|
12
|
+
describe 'unpaginated api' do
|
13
|
+
subject { Class.new(UnPaginatedAPI) }
|
14
|
+
|
15
|
+
it 'raises an error' do
|
16
|
+
expect { subject.paginate }.to raise_error(NoMethodError, /undefined method `paginate' for/i)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe 'default paginated api' do
|
21
|
+
subject { Class.new(PaginatedAPI) }
|
22
|
+
|
23
|
+
it 'adds to declared parameters' do
|
24
|
+
subject.paginate
|
25
|
+
expect(subject.inheritable_setting.route[:declared_params]).to eq(%i[page per_page offset])
|
26
|
+
end
|
27
|
+
|
28
|
+
describe 'descriptions, validation, and defaults' do
|
29
|
+
let(:params) { subject.routes.first.params }
|
30
|
+
|
31
|
+
before do
|
32
|
+
subject.paginate
|
33
|
+
subject.get('/') {}
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'does not require :page' do
|
37
|
+
expect(params['page'][:required]).to eq(false)
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'does not require :per_page' do
|
41
|
+
expect(params['per_page'][:required]).to eq(false)
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'does not require :offset' do
|
45
|
+
expect(params['offset'][:required]).to eq(false)
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'describes :page' do
|
49
|
+
expect(params['page'][:desc]).to eq('Page offset to fetch.')
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'describes :per_page' do
|
53
|
+
expect(params['per_page'][:desc]).to eq('Number of results to return per page.')
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'describes :offset' do
|
57
|
+
expect(params['offset'][:desc]).to eq('Pad a number of results.')
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'validates :page as Integer' do
|
61
|
+
expect(params['page'][:type]).to eq('Integer')
|
62
|
+
end
|
63
|
+
|
64
|
+
it 'validates :per_page as Integer' do
|
65
|
+
expect(params['per_page'][:type]).to eq('Integer')
|
66
|
+
end
|
67
|
+
|
68
|
+
it 'validates :offset as Integer' do
|
69
|
+
expect(params['offset'][:type]).to eq('Integer')
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'defaults :page to 1' do
|
73
|
+
expect(params['page'][:default]).to eq(1)
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'defaults :per_page to Kaminari.config.default_per_page' do
|
77
|
+
expect(params['per_page'][:default]).to eq(::Kaminari.config.default_per_page)
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'defaults :offset to 0' do
|
81
|
+
expect(params['offset'][:default]).to eq(0)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
describe 'custom paginated api' do
|
87
|
+
subject { Class.new(PaginatedAPI) }
|
88
|
+
let(:app) { subject }
|
89
|
+
let(:params) { subject.routes.first.params }
|
90
|
+
|
91
|
+
before do
|
92
|
+
subject.paginate per_page: 99, max_per_page: 999, offset: 9
|
93
|
+
subject.get('/') {}
|
94
|
+
end
|
95
|
+
|
96
|
+
it 'defaults :per_page to customized value' do
|
97
|
+
expect(params['per_page'][:default]).to eq(99)
|
98
|
+
end
|
99
|
+
|
100
|
+
it 'succeeds when :per_page is within :max_value' do
|
101
|
+
get('/', page: 1, per_page: 999)
|
102
|
+
expect(last_response.status).to eq 200
|
103
|
+
end
|
104
|
+
|
105
|
+
it 'ensures :per_page is within :max_value' do
|
106
|
+
get('/', page: 1, per_page: 1_000)
|
107
|
+
expect(last_response.status).to eq 400
|
108
|
+
expect(last_response.body).to match(/per_page must be less than or equal 999/)
|
109
|
+
end
|
110
|
+
|
111
|
+
it 'defaults :offset to customized value' do
|
112
|
+
expect(params['offset'][:default]).to eq(9)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
describe 'paginated api without :offset' do
|
117
|
+
subject { Class.new(PaginatedAPI) }
|
118
|
+
|
119
|
+
it 'excludes :offset from declared params' do
|
120
|
+
subject.paginate offset: false
|
121
|
+
expect(subject.inheritable_setting.route[:declared_params]).not_to include(:offset)
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
class PaginatedAPI < Grape::API
|
5
|
+
include Grape::Kaminari
|
6
|
+
|
7
|
+
paginate
|
8
|
+
get '' do
|
9
|
+
paginate(Kaminari.paginate_array((1..10).to_a))
|
10
|
+
end
|
11
|
+
|
12
|
+
paginate offset: false
|
13
|
+
get 'no-offset' do
|
14
|
+
paginate(Kaminari.paginate_array((1..10).to_a))
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe Grape::Kaminari do
|
19
|
+
subject { PaginatedAPI.new }
|
20
|
+
def app
|
21
|
+
subject
|
22
|
+
end
|
23
|
+
let(:json) { JSON.parse(last_response.body) }
|
24
|
+
let(:header) { last_response.header }
|
25
|
+
|
26
|
+
describe 'paginated helper' do
|
27
|
+
it 'returns the first page' do
|
28
|
+
get '/', page: 1, per_page: 3
|
29
|
+
expect(json).to eq [1, 2, 3]
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'returns the second page' do
|
33
|
+
get '/', page: 2, per_page: 3
|
34
|
+
expect(json).to eq [4, 5, 6]
|
35
|
+
end
|
36
|
+
|
37
|
+
# This is here to ensure that Kaminari can handle `padding(false)`
|
38
|
+
# and still do the right thing.
|
39
|
+
it 'works when offset is false' do
|
40
|
+
get '/no-offset', page: 1, per_page: 3
|
41
|
+
expect(json).to eq [1, 2, 3]
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'sets headers' do
|
45
|
+
get '/', page: 3, per_page: 2, offset: 1
|
46
|
+
expect(header['X-Total']).to eq '10'
|
47
|
+
expect(header['X-Total-Pages']).to eq '5'
|
48
|
+
expect(header['X-Per-Page']).to eq '2'
|
49
|
+
expect(header['X-Page']).to eq '3'
|
50
|
+
expect(header['X-Next-Page']).to eq '4'
|
51
|
+
expect(header['X-Prev-Page']).to eq '2'
|
52
|
+
expect(header['X-Offset']).to eq '1'
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require 'grape-kaminari'
|
3
|
+
require 'rack/test'
|
4
|
+
|
5
|
+
# Requires supporting files with custom matchers and macros, etc,
|
6
|
+
# in ./support/ and its subdirectories.
|
7
|
+
Dir[File.expand_path('./support/**/*.rb', __dir__)].sort.each { |f| require f }
|
8
|
+
|
9
|
+
I18n.enforce_available_locales = false
|
10
|
+
|
11
|
+
RSpec.configure do |config|
|
12
|
+
config.include Rack::Test::Methods
|
13
|
+
config.raise_errors_for_deprecations!
|
14
|
+
config.order = 'random'
|
15
|
+
end
|
metadata
CHANGED
@@ -1,80 +1,126 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: grape-kaminari
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
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-06-18 00:00:00.000000000 Z
|
12
13
|
dependencies:
|
13
14
|
- !ruby/object:Gem::Dependency
|
14
15
|
name: grape
|
15
16
|
requirement: !ruby/object:Gem::Requirement
|
16
17
|
requirements:
|
17
|
-
- -
|
18
|
+
- - ">="
|
18
19
|
- !ruby/object:Gem::Version
|
19
|
-
version: '0'
|
20
|
+
version: '1.0'
|
20
21
|
type: :runtime
|
21
22
|
prerelease: false
|
22
23
|
version_requirements: !ruby/object:Gem::Requirement
|
23
24
|
requirements:
|
24
|
-
- -
|
25
|
+
- - ">="
|
25
26
|
- !ruby/object:Gem::Version
|
26
|
-
version: '0'
|
27
|
+
version: '1.0'
|
27
28
|
- !ruby/object:Gem::Dependency
|
28
|
-
name: kaminari
|
29
|
+
name: kaminari-grape
|
29
30
|
requirement: !ruby/object:Gem::Requirement
|
30
31
|
requirements:
|
31
|
-
- -
|
32
|
+
- - ">="
|
32
33
|
- !ruby/object:Gem::Version
|
33
34
|
version: '0'
|
34
35
|
type: :runtime
|
35
36
|
prerelease: false
|
36
37
|
version_requirements: !ruby/object:Gem::Requirement
|
37
38
|
requirements:
|
38
|
-
- -
|
39
|
+
- - ">="
|
39
40
|
- !ruby/object:Gem::Version
|
40
41
|
version: '0'
|
41
42
|
- !ruby/object:Gem::Dependency
|
42
43
|
name: bundler
|
43
44
|
requirement: !ruby/object:Gem::Requirement
|
44
45
|
requirements:
|
45
|
-
- -
|
46
|
+
- - ">="
|
46
47
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
48
|
+
version: '0'
|
48
49
|
type: :development
|
49
50
|
prerelease: false
|
50
51
|
version_requirements: !ruby/object:Gem::Requirement
|
51
52
|
requirements:
|
52
|
-
- -
|
53
|
+
- - ">="
|
53
54
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
55
|
+
version: '0'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: rack-test
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
55
70
|
- !ruby/object:Gem::Dependency
|
56
71
|
name: rake
|
57
72
|
requirement: !ruby/object:Gem::Requirement
|
58
73
|
requirements:
|
59
|
-
- -
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
- !ruby/object:Gem::Dependency
|
85
|
+
name: rspec
|
86
|
+
requirement: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
60
89
|
- !ruby/object:Gem::Version
|
61
90
|
version: '0'
|
62
91
|
type: :development
|
63
92
|
prerelease: false
|
64
93
|
version_requirements: !ruby/object:Gem::Requirement
|
65
94
|
requirements:
|
66
|
-
- -
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
- !ruby/object:Gem::Dependency
|
99
|
+
name: rubocop
|
100
|
+
requirement: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
type: :development
|
106
|
+
prerelease: false
|
107
|
+
version_requirements: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
67
110
|
- !ruby/object:Gem::Version
|
68
111
|
version: '0'
|
69
112
|
description: kaminari paginator integration for grape API framework
|
70
113
|
email:
|
71
|
-
-
|
114
|
+
- info@blacksquaremedia.com
|
72
115
|
executables: []
|
73
116
|
extensions: []
|
74
117
|
extra_rdoc_files: []
|
75
118
|
files:
|
76
|
-
- .gitignore
|
119
|
+
- ".gitignore"
|
120
|
+
- ".rubocop.yml"
|
121
|
+
- ".travis.yml"
|
77
122
|
- Gemfile
|
123
|
+
- Gemfile.lock
|
78
124
|
- LICENSE.txt
|
79
125
|
- README.md
|
80
126
|
- Rakefile
|
@@ -83,6 +129,9 @@ files:
|
|
83
129
|
- lib/grape/kaminari.rb
|
84
130
|
- lib/grape/kaminari/max_value_validator.rb
|
85
131
|
- lib/grape/kaminari/version.rb
|
132
|
+
- spec/kaminari_spec.rb
|
133
|
+
- spec/paginate_helper_spec.rb
|
134
|
+
- spec/spec_helper.rb
|
86
135
|
homepage: ''
|
87
136
|
licenses:
|
88
137
|
- MIT
|
@@ -93,18 +142,20 @@ require_paths:
|
|
93
142
|
- lib
|
94
143
|
required_ruby_version: !ruby/object:Gem::Requirement
|
95
144
|
requirements:
|
96
|
-
- -
|
145
|
+
- - ">="
|
97
146
|
- !ruby/object:Gem::Version
|
98
|
-
version: '
|
147
|
+
version: '2.5'
|
99
148
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
149
|
requirements:
|
101
|
-
- -
|
150
|
+
- - ">="
|
102
151
|
- !ruby/object:Gem::Version
|
103
152
|
version: '0'
|
104
153
|
requirements: []
|
105
|
-
|
106
|
-
rubygems_version: 2.1.11
|
154
|
+
rubygems_version: 3.1.4
|
107
155
|
signing_key:
|
108
156
|
specification_version: 4
|
109
157
|
summary: kaminari integration for grape
|
110
|
-
test_files:
|
158
|
+
test_files:
|
159
|
+
- spec/kaminari_spec.rb
|
160
|
+
- spec/paginate_helper_spec.rb
|
161
|
+
- spec/spec_helper.rb
|