grape-kaminari 0.3.0 → 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 690c3b70419498572169fa3e2daee43ba5adef9c93e46ba8b0da6944f2afdcc0
4
- data.tar.gz: 49f722ee554da32f4ecc3351abc2e5edc44454882046cfadf39f0d99c7ba2ba5
3
+ metadata.gz: fbda16acc1309fb774a2c4567223c97726d212b321d6b0cccb199c3dc99457a0
4
+ data.tar.gz: 84435c93750e4879af86c18fd38e86f61e244eb722b95b27b5a4b84b276372b0
5
5
  SHA512:
6
- metadata.gz: c08980fd147c292e51c546c19166a095b012fe0727e95a30d5ac9a5682eecfd66fdbf907167b0c2c5160c854269728ecca34cee6808009660e02075eaed67d35
7
- data.tar.gz: ffdf0838305fd025f5fda2fb96ee6c58b9f46bfbe0fe511f13de9b06d9683f1f6651d45a23d8b923434974f65d930242bcfb00723a3042dffa9cdecd6dba8960
6
+ metadata.gz: d2c03beecbb06ce1d537714392ad926e53b39e98d433ed2994ec3d1def516a679b69b7262928c3a00a0870690eaea094ccdee5dd6305cc10722eae1b7b4df14d
7
+ data.tar.gz: 6f7f41b703eb9e06db050636f3d68d23adeceb59113c43ff0f7da13d733e994a61c65ca34c9c1792fa6b50350686f5ff1574bc38a0c273564ac9284f1be22a2d
@@ -0,0 +1,21 @@
1
+ name: Test
2
+ on:
3
+ push:
4
+ branches:
5
+ - main
6
+ pull_request:
7
+ branches:
8
+ - main
9
+ jobs:
10
+ ruby:
11
+ runs-on: ubuntu-latest
12
+ strategy:
13
+ matrix:
14
+ ruby-version: ["2.7", "3.0", "3.1"]
15
+ steps:
16
+ - uses: actions/checkout@v2
17
+ - uses: ruby/setup-ruby@v1
18
+ with:
19
+ ruby-version: ${{ matrix.ruby-version }}
20
+ bundler-cache: true
21
+ - run: bundle exec rake
data/.gitignore CHANGED
@@ -14,3 +14,4 @@ spec/reports
14
14
  test/tmp
15
15
  test/version_tmp
16
16
  tmp
17
+ *~
data/.rubocop.yml CHANGED
@@ -1,26 +1,13 @@
1
- AllCops:
2
- TargetRubyVersion: "2.5"
3
- NewCops: enable
1
+ inherit_gem:
2
+ rubocop-bsm:
3
+ - default.yml
4
+ inherit_mode:
5
+ merge:
6
+ - Exclude
4
7
 
5
- Metrics/AbcSize:
6
- Enabled: false
7
- Metrics/BlockLength:
8
- Exclude:
9
- - spec/**/*_spec.rb
10
- Metrics/MethodLength:
11
- Max: 20
8
+ AllCops:
9
+ TargetRubyVersion: "2.7"
12
10
 
13
11
  Naming/FileName:
14
12
  Exclude:
15
13
  - 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/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 CHANGED
@@ -1,65 +1,60 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- grape-kaminari (0.3.0)
5
- grape (>= 1.0, != 1.4.0)
4
+ grape-kaminari (0.4.3)
5
+ grape (>= 1.6.1)
6
6
  kaminari-grape
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
- activesupport (6.0.3.2)
11
+ activesupport (7.0.0)
12
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)
13
+ i18n (>= 1.6, < 2)
14
+ minitest (>= 5.1)
15
+ tzinfo (~> 2.0)
16
+ ast (2.4.2)
18
17
  builder (3.2.4)
19
- concurrent-ruby (1.1.7)
20
- diff-lcs (1.4.4)
21
- dry-configurable (0.11.6)
18
+ concurrent-ruby (1.1.9)
19
+ diff-lcs (1.5.0)
20
+ dry-configurable (0.13.0)
22
21
  concurrent-ruby (~> 1.0)
23
- dry-core (~> 0.4, >= 0.4.7)
24
- dry-equalizer (~> 0.2)
25
- dry-container (0.7.2)
22
+ dry-core (~> 0.6)
23
+ dry-container (0.9.0)
26
24
  concurrent-ruby (~> 1.0)
27
- dry-configurable (~> 0.1, >= 0.1.3)
28
- dry-core (0.4.9)
25
+ dry-configurable (~> 0.13, >= 0.13.0)
26
+ dry-core (0.7.1)
29
27
  concurrent-ruby (~> 1.0)
30
- dry-equalizer (0.3.0)
31
- dry-inflector (0.2.0)
32
- dry-logic (1.0.6)
28
+ dry-inflector (0.2.1)
29
+ dry-logic (1.2.0)
33
30
  concurrent-ruby (~> 1.0)
34
- dry-core (~> 0.2)
35
- dry-equalizer (~> 0.2)
36
- dry-types (1.4.0)
31
+ dry-core (~> 0.5, >= 0.5)
32
+ dry-types (1.5.1)
37
33
  concurrent-ruby (~> 1.0)
38
34
  dry-container (~> 0.3)
39
- dry-core (~> 0.4, >= 0.4.4)
40
- dry-equalizer (~> 0.3)
35
+ dry-core (~> 0.5, >= 0.5)
41
36
  dry-inflector (~> 0.1, >= 0.1.2)
42
37
  dry-logic (~> 1.0, >= 1.0.2)
43
- grape (1.3.3)
38
+ grape (1.6.2)
44
39
  activesupport
45
40
  builder
46
41
  dry-types (>= 1.1)
47
42
  mustermann-grape (~> 1.0.0)
48
43
  rack (>= 1.3.0)
49
44
  rack-accept
50
- i18n (1.8.5)
45
+ i18n (1.8.11)
51
46
  concurrent-ruby (~> 1.0)
52
- kaminari-core (1.2.1)
47
+ kaminari-core (1.2.2)
53
48
  kaminari-grape (1.0.1)
54
49
  grape
55
50
  kaminari-core (~> 1.0)
56
- minitest (5.14.1)
51
+ minitest (5.15.0)
57
52
  mustermann (1.1.1)
58
53
  ruby2_keywords (~> 0.0.1)
59
54
  mustermann-grape (1.0.1)
60
55
  mustermann (>= 1.0.0)
61
- parallel (1.19.2)
62
- parser (2.7.1.4)
56
+ parallel (1.21.0)
57
+ parser (3.1.0.0)
63
58
  ast (~> 2.4.1)
64
59
  rack (2.2.3)
65
60
  rack-accept (0.4.5)
@@ -67,40 +62,50 @@ GEM
67
62
  rack-test (1.1.0)
68
63
  rack (>= 1.0, < 3)
69
64
  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)
65
+ rake (13.0.6)
66
+ regexp_parser (2.2.0)
67
+ rexml (3.2.5)
68
+ rspec (3.10.0)
69
+ rspec-core (~> 3.10.0)
70
+ rspec-expectations (~> 3.10.0)
71
+ rspec-mocks (~> 3.10.0)
72
+ rspec-core (3.10.1)
73
+ rspec-support (~> 3.10.0)
74
+ rspec-expectations (3.10.1)
80
75
  diff-lcs (>= 1.2.0, < 2.0)
81
- rspec-support (~> 3.9.0)
82
- rspec-mocks (3.9.1)
76
+ rspec-support (~> 3.10.0)
77
+ rspec-mocks (3.10.2)
83
78
  diff-lcs (>= 1.2.0, < 2.0)
84
- rspec-support (~> 3.9.0)
85
- rspec-support (3.9.3)
86
- rubocop (0.89.0)
79
+ rspec-support (~> 3.10.0)
80
+ rspec-support (3.10.3)
81
+ rubocop (1.24.1)
87
82
  parallel (~> 1.10)
88
- parser (>= 2.7.1.1)
83
+ parser (>= 3.0.0.0)
89
84
  rainbow (>= 2.2.2, < 4.0)
90
- regexp_parser (>= 1.7)
85
+ regexp_parser (>= 1.8, < 3.0)
91
86
  rexml
92
- rubocop-ast (>= 0.1.0, < 1.0)
87
+ rubocop-ast (>= 1.15.1, < 2.0)
93
88
  ruby-progressbar (~> 1.7)
94
- unicode-display_width (>= 1.4.0, < 2.0)
95
- rubocop-ast (0.3.0)
96
- parser (>= 2.7.1.4)
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.4.0)
89
+ unicode-display_width (>= 1.4.0, < 3.0)
90
+ rubocop-ast (1.15.1)
91
+ parser (>= 3.0.1.1)
92
+ rubocop-bsm (0.6.0)
93
+ rubocop (~> 1.0)
94
+ rubocop-performance
95
+ rubocop-rake
96
+ rubocop-rspec
97
+ rubocop-performance (1.13.1)
98
+ rubocop (>= 1.7.0, < 2.0)
99
+ rubocop-ast (>= 0.4.0)
100
+ rubocop-rake (0.6.0)
101
+ rubocop (~> 1.0)
102
+ rubocop-rspec (2.7.0)
103
+ rubocop (~> 1.19)
104
+ ruby-progressbar (1.11.0)
105
+ ruby2_keywords (0.0.5)
106
+ tzinfo (2.0.4)
107
+ concurrent-ruby (~> 1.0)
108
+ unicode-display_width (2.1.0)
104
109
 
105
110
  PLATFORMS
106
111
  ruby
@@ -111,7 +116,7 @@ DEPENDENCIES
111
116
  rack-test
112
117
  rake
113
118
  rspec
114
- rubocop
119
+ rubocop-bsm
115
120
 
116
121
  BUNDLED WITH
117
- 2.1.4
122
+ 2.2.27
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Grape::Kaminari
2
2
 
3
- [![Build Status](https://travis-ci.org/bsm/grape-kaminari.png?branch=master)](https://travis-ci.org/bsm/grape-kaminari)
3
+ [![Test](https://github.com/bsm/grape-kaminari/actions/workflows/test.yml/badge.svg)](https://github.com/bsm/grape-kaminari/actions/workflows/test.yml)
4
4
  [![License](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
5
5
 
6
6
  [kaminari](https://github.com/kaminari/kaminari) paginator integration for [grape](https://github.com/intridea/grape) API framework.
@@ -35,7 +35,6 @@ class MyApi < Grape::API
35
35
  resource :posts do
36
36
  desc 'Return a list of posts.'
37
37
 
38
- # Annotate action with `paginate`.
39
38
  # This will add three optional params: page, per_page, and offset
40
39
  #
41
40
  # You can optionally overwrite the default :per_page setting (10)
@@ -45,8 +44,9 @@ class MyApi < Grape::API
45
44
  # You can disable the offset parameter from appearing in the API
46
45
  # documentation by setting it to false.
47
46
  #
48
- paginate per_page: 20, max_per_page: 30, offset: 5
49
-
47
+ params do
48
+ use :pagination, per_page: 20, max_per_page: 30, offset: 5
49
+ end
50
50
  get do
51
51
  posts = Post.where(...)
52
52
 
@@ -12,17 +12,18 @@ Gem::Specification.new do |spec|
12
12
  spec.homepage = ''
13
13
  spec.license = 'MIT'
14
14
 
15
- spec.files = `git ls-files -z`.split("\x0").reject { |f| f.start_with?('spec/') }
15
+ spec.files = `git ls-files -z`.split("\x0").reject {|f| f.start_with?('spec/') }
16
16
  spec.test_files = `git ls-files -z -- spec/*`.split("\x0")
17
17
  spec.require_paths = ['lib']
18
- spec.required_ruby_version = '>= 2.5'
18
+ spec.required_ruby_version = '>= 2.7'
19
19
 
20
- spec.add_runtime_dependency 'grape', '>= 1.0', '!= 1.4.0'
20
+ spec.add_runtime_dependency 'grape', '>= 1.6.1'
21
21
  spec.add_runtime_dependency 'kaminari-grape'
22
22
 
23
23
  spec.add_development_dependency 'bundler'
24
24
  spec.add_development_dependency 'rack-test'
25
25
  spec.add_development_dependency 'rake'
26
26
  spec.add_development_dependency 'rspec'
27
- spec.add_development_dependency 'rubocop'
27
+ spec.add_development_dependency 'rubocop-bsm'
28
+ spec.metadata['rubygems_mfa_required'] = 'true'
28
29
  end
@@ -1,6 +1,6 @@
1
1
  module Grape
2
2
  module Kaminari
3
- class MaxValueValidator < Grape::Validations::Base
3
+ class MaxValueValidator < Grape::Validations::Validators::Base
4
4
  def validate_param!(attr_name, params)
5
5
  attr = params[attr_name]
6
6
  return unless attr.is_a?(Integer) && @option && attr > @option
@@ -1,5 +1,5 @@
1
1
  module Grape
2
2
  module Kaminari
3
- VERSION = '0.3.0'.freeze
3
+ VERSION = '0.4.3'.freeze
4
4
  end
5
5
  end
@@ -9,45 +9,57 @@ module Grape
9
9
 
10
10
  included do
11
11
  helpers HelperMethods
12
- base_instance.extend DSLMethods
13
12
  end
14
13
 
15
14
  module HelperMethods # :nodoc:
16
- def paginate(collection)
17
- collection.page(params[:page].to_i)
18
- .per(params[:per_page].to_i)
19
- .padding(params[:offset].to_i)
20
- .tap do |data|
21
- header 'X-Total', data.total_count.to_s
22
- header 'X-Total-Pages', data.total_pages.to_s
23
- header 'X-Per-Page', data.limit_value.to_s
24
- header 'X-Page', data.current_page.to_s
25
- header 'X-Next-Page', data.next_page.to_s
26
- header 'X-Prev-Page', data.prev_page.to_s
27
- header 'X-Offset', params[:offset].to_s
28
- end
29
- end
30
- end
15
+ extend Grape::API::Helpers
31
16
 
32
- module DSLMethods # :nodoc:
33
- def paginate(**options)
34
- options.reverse_merge!(
17
+ params :pagination do |opts = {}|
18
+ opts.reverse_merge!(
35
19
  per_page: ::Kaminari.config.default_per_page || 10,
36
20
  max_per_page: ::Kaminari.config.max_per_page,
37
21
  offset: 0,
38
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.'
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
55
+
56
+ module DSLMethods # :nodoc:
57
+ def paginate(opts = {})
39
58
  params do
40
- optional :page, type: Integer, default: 1,
41
- desc: 'Page offset to fetch.'
42
- optional :per_page, type: Integer, default: options[:per_page],
43
- desc: 'Number of results to return per page.',
44
- max_value: options[:max_per_page]
45
- if options[:offset].is_a? Numeric
46
- optional :offset, type: Integer, default: options[:offset],
47
- desc: 'Pad a number of results.'
48
- end
59
+ use(:pagination, opts)
49
60
  end
50
61
  end
51
62
  end
63
+ Grape::API::Instance.extend(DSLMethods)
52
64
  end
53
65
  end
@@ -0,0 +1,94 @@
1
+ require 'spec_helper'
2
+ require 'json'
3
+
4
+ describe Grape::Kaminari do
5
+ let(:app) do
6
+ Class.new(Grape::API) do
7
+ include Grape::Kaminari
8
+
9
+ params do
10
+ use :pagination
11
+ end
12
+ get '' do
13
+ paginate(Kaminari.paginate_array((1..10).to_a))
14
+ end
15
+
16
+ params do
17
+ use :pagination, offset: false
18
+ end
19
+ get 'no-offset' do
20
+ paginate(Kaminari.paginate_array((1..10).to_a))
21
+ end
22
+
23
+ params do
24
+ use :pagination, offset: false
25
+ end
26
+ get 'no-count' do
27
+ paginate(Kaminari.paginate_array((1..10).to_a), without_count: true)
28
+ end
29
+
30
+ resource :sub do
31
+ params do
32
+ use :pagination, per_page: 2
33
+ end
34
+ get '/' do
35
+ paginate(Kaminari.paginate_array((1..10).to_a))
36
+ end
37
+ end
38
+ end
39
+ end
40
+ let(:json) { JSON.parse(last_response.body) }
41
+ let(:header) { last_response.header }
42
+
43
+ describe 'paginated helper' do
44
+ it 'returns the first page' do
45
+ get '/', page: 1, per_page: 3
46
+ expect(json).to eq [1, 2, 3]
47
+ end
48
+
49
+ it 'returns the second page' do
50
+ get '/', page: 2, per_page: 3
51
+ expect(json).to eq [4, 5, 6]
52
+ end
53
+
54
+ # This is here to ensure that Kaminari can handle `padding(false)`
55
+ # and still do the right thing.
56
+ it 'works when offset is false' do
57
+ get '/no-offset', page: 1, per_page: 3
58
+ expect(json).to eq [1, 2, 3]
59
+ end
60
+
61
+ it 'works without count' do
62
+ get '/no-count', page: 2, per_page: 3
63
+ expect(json).to eq [4, 5, 6]
64
+ expect(header).to include(
65
+ 'X-Per-Page' => '3',
66
+ 'X-Page' => '2',
67
+ 'X-Next-Page' => '3',
68
+ 'X-Prev-Page' => '1',
69
+ )
70
+ expect(header).not_to include(
71
+ 'X-Total',
72
+ 'X-Total-Pages',
73
+ )
74
+ end
75
+
76
+ it 'sets headers' do
77
+ get '/', page: 3, per_page: 2, offset: 1
78
+ expect(header).to include(
79
+ 'X-Total' => '10',
80
+ 'X-Total-Pages' => '5',
81
+ 'X-Per-Page' => '2',
82
+ 'X-Page' => '3',
83
+ 'X-Next-Page' => '4',
84
+ 'X-Prev-Page' => '2',
85
+ 'X-Offset' => '1',
86
+ )
87
+ end
88
+
89
+ it 'can be inherited' do
90
+ get '/sub', page: 1
91
+ expect(json).to eq [1, 2]
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,130 @@
1
+ require 'spec_helper'
2
+
3
+ describe Grape::Kaminari do
4
+ subject do
5
+ Class.new(Grape::API) do
6
+ include Grape::Kaminari
7
+ end
8
+ end
9
+
10
+ def declared_params
11
+ subject.namespace_stackable(:declared_params).flatten
12
+ end
13
+
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
18
+
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
23
+
24
+ it 'supports legacy declarations' do
25
+ subject.paginate
26
+ expect(declared_params).to eq(%i[page per_page offset])
27
+ end
28
+
29
+ it 'does 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
36
+
37
+ describe 'descriptions, validation, and defaults' do
38
+ let(:params) { subject.routes.first.params }
39
+
40
+ before do
41
+ subject.params { use :pagination }
42
+ subject.get('/') { 'OK' }
43
+ end
44
+
45
+ it 'does not require :page' do
46
+ expect(params['page'][:required]).to eq(false)
47
+ end
48
+
49
+ it 'does not require :per_page' do
50
+ expect(params['per_page'][:required]).to eq(false)
51
+ end
52
+
53
+ it 'does not require :offset' do
54
+ expect(params['offset'][:required]).to eq(false)
55
+ end
56
+
57
+ it 'describes :page' do
58
+ expect(params['page'][:desc]).to eq('Page offset to fetch.')
59
+ end
60
+
61
+ it 'describes :per_page' do
62
+ expect(params['per_page'][:desc]).to eq('Number of results to return per page.')
63
+ end
64
+
65
+ it 'describes :offset' do
66
+ expect(params['offset'][:desc]).to eq('Pad a number of results.')
67
+ end
68
+
69
+ it 'validates :page as Integer' do
70
+ expect(params['page'][:type]).to eq('Integer')
71
+ end
72
+
73
+ it 'validates :per_page as Integer' do
74
+ expect(params['per_page'][:type]).to eq('Integer')
75
+ end
76
+
77
+ it 'validates :offset as Integer' do
78
+ expect(params['offset'][:type]).to eq('Integer')
79
+ end
80
+
81
+ it 'defaults :page to 1' do
82
+ expect(params['page'][:default]).to eq(1)
83
+ end
84
+
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)
87
+ end
88
+
89
+ it 'defaults :offset to 0' do
90
+ expect(params['offset'][:default]).to eq(0)
91
+ end
92
+ end
93
+
94
+ describe 'custom paginated api' do
95
+ let(:app) { subject }
96
+ let(:params) { subject.routes.first.params }
97
+
98
+ before do
99
+ subject.params do
100
+ use :pagination, per_page: 99, max_per_page: 999, offset: 9
101
+ end
102
+ subject.get('/') { 'OK' }
103
+ end
104
+
105
+ it 'defaults :per_page to customized value' do
106
+ expect(params['per_page'][:default]).to eq(99)
107
+ end
108
+
109
+ it 'succeeds when :per_page is within :max_value' do
110
+ get('/', page: 1, per_page: 999)
111
+ expect(last_response.status).to eq(200)
112
+ end
113
+
114
+ it 'ensures :per_page is within :max_value' do
115
+ get('/', page: 1, per_page: 1_000)
116
+ expect(last_response.status).to eq(400)
117
+ expect(last_response.body).to match(/per_page must be less than or equal 999/)
118
+ end
119
+
120
+ it 'ensures :per_page is numeric' do
121
+ get('/', page: 1, per_page: 'foo')
122
+ expect(last_response.status).to eq(400)
123
+ expect(last_response.body).to match(/per_page is invalid/)
124
+ end
125
+
126
+ it 'defaults :offset to customized value' do
127
+ expect(params['offset'][:default]).to eq(9)
128
+ end
129
+ end
130
+ end
data/spec/spec_helper.rb CHANGED
@@ -4,7 +4,7 @@ require 'rack/test'
4
4
 
5
5
  # Requires supporting files with custom matchers and macros, etc,
6
6
  # in ./support/ and its subdirectories.
7
- Dir[File.expand_path('./support/**/*.rb', __dir__)].sort.each { |f| require f }
7
+ Dir[File.expand_path('./support/**/*.rb', __dir__)].sort.each {|f| require f }
8
8
 
9
9
  I18n.enforce_available_locales = false
10
10
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grape-kaminari
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tymon Tobolski
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2020-08-10 00:00:00.000000000 Z
12
+ date: 2022-01-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: grape
@@ -17,20 +17,14 @@ dependencies:
17
17
  requirements:
18
18
  - - ">="
19
19
  - !ruby/object:Gem::Version
20
- version: '1.0'
21
- - - "!="
22
- - !ruby/object:Gem::Version
23
- version: 1.4.0
20
+ version: 1.6.1
24
21
  type: :runtime
25
22
  prerelease: false
26
23
  version_requirements: !ruby/object:Gem::Requirement
27
24
  requirements:
28
25
  - - ">="
29
26
  - !ruby/object:Gem::Version
30
- version: '1.0'
31
- - - "!="
32
- - !ruby/object:Gem::Version
33
- version: 1.4.0
27
+ version: 1.6.1
34
28
  - !ruby/object:Gem::Dependency
35
29
  name: kaminari-grape
36
30
  requirement: !ruby/object:Gem::Requirement
@@ -102,7 +96,7 @@ dependencies:
102
96
  - !ruby/object:Gem::Version
103
97
  version: '0'
104
98
  - !ruby/object:Gem::Dependency
105
- name: rubocop
99
+ name: rubocop-bsm
106
100
  requirement: !ruby/object:Gem::Requirement
107
101
  requirements:
108
102
  - - ">="
@@ -122,9 +116,10 @@ executables: []
122
116
  extensions: []
123
117
  extra_rdoc_files: []
124
118
  files:
119
+ - ".github/workflows/test.yml"
125
120
  - ".gitignore"
126
121
  - ".rubocop.yml"
127
- - ".travis.yml"
122
+ - CHANGELOG.md
128
123
  - Gemfile
129
124
  - Gemfile.lock
130
125
  - LICENSE.txt
@@ -135,13 +130,14 @@ files:
135
130
  - lib/grape/kaminari.rb
136
131
  - lib/grape/kaminari/max_value_validator.rb
137
132
  - lib/grape/kaminari/version.rb
138
- - spec/kaminari_spec.rb
139
- - spec/paginate_helper_spec.rb
133
+ - spec/grape/kaminari_params_spec.rb
134
+ - spec/grape/kaminari_spec.rb
140
135
  - spec/spec_helper.rb
141
136
  homepage: ''
142
137
  licenses:
143
138
  - MIT
144
- metadata: {}
139
+ metadata:
140
+ rubygems_mfa_required: 'true'
145
141
  post_install_message:
146
142
  rdoc_options: []
147
143
  require_paths:
@@ -150,18 +146,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
150
146
  requirements:
151
147
  - - ">="
152
148
  - !ruby/object:Gem::Version
153
- version: '2.5'
149
+ version: '2.7'
154
150
  required_rubygems_version: !ruby/object:Gem::Requirement
155
151
  requirements:
156
152
  - - ">="
157
153
  - !ruby/object:Gem::Version
158
154
  version: '0'
159
155
  requirements: []
160
- rubygems_version: 3.1.2
156
+ rubygems_version: 3.2.15
161
157
  signing_key:
162
158
  specification_version: 4
163
159
  summary: kaminari integration for grape
164
160
  test_files:
165
- - spec/kaminari_spec.rb
166
- - spec/paginate_helper_spec.rb
161
+ - spec/grape/kaminari_params_spec.rb
162
+ - spec/grape/kaminari_spec.rb
167
163
  - spec/spec_helper.rb
data/.travis.yml DELETED
@@ -1,6 +0,0 @@
1
- language: ruby
2
- cache: bundler
3
- rvm:
4
- - 2.7
5
- - 2.6
6
- - 2.5
@@ -1,130 +0,0 @@
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 'ensures :per_page is numeric' do
112
- get('/', page: 1, per_page: 'foo')
113
- expect(last_response.status).to eq(400)
114
- expect(last_response.body).to match(/per_page is invalid/)
115
- end
116
-
117
- it 'defaults :offset to customized value' do
118
- expect(params['offset'][:default]).to eq(9)
119
- end
120
- end
121
-
122
- describe 'paginated api without :offset' do
123
- subject { Class.new(PaginatedAPI) }
124
-
125
- it 'excludes :offset from declared params' do
126
- subject.paginate offset: false
127
- expect(subject.inheritable_setting.route[:declared_params]).not_to include(:offset)
128
- end
129
- end
130
- end
@@ -1,65 +0,0 @@
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
-
17
- resource :sub do
18
- paginate per_page: 2
19
- get '/' do
20
- paginate(Kaminari.paginate_array((1..10).to_a))
21
- end
22
- end
23
- end
24
-
25
- describe Grape::Kaminari do
26
- subject { PaginatedAPI.new }
27
- let(:app) { subject }
28
- let(:json) { JSON.parse(last_response.body) }
29
- let(:header) { last_response.header }
30
-
31
- describe 'paginated helper' do
32
- it 'returns the first page' do
33
- get '/', page: 1, per_page: 3
34
- expect(json).to eq [1, 2, 3]
35
- end
36
-
37
- it 'returns the second page' do
38
- get '/', page: 2, per_page: 3
39
- expect(json).to eq [4, 5, 6]
40
- end
41
-
42
- # This is here to ensure that Kaminari can handle `padding(false)`
43
- # and still do the right thing.
44
- it 'works when offset is false' do
45
- get '/no-offset', page: 1, per_page: 3
46
- expect(json).to eq [1, 2, 3]
47
- end
48
-
49
- it 'sets headers' do
50
- get '/', page: 3, per_page: 2, offset: 1
51
- expect(header['X-Total']).to eq '10'
52
- expect(header['X-Total-Pages']).to eq '5'
53
- expect(header['X-Per-Page']).to eq '2'
54
- expect(header['X-Page']).to eq '3'
55
- expect(header['X-Next-Page']).to eq '4'
56
- expect(header['X-Prev-Page']).to eq '2'
57
- expect(header['X-Offset']).to eq '1'
58
- end
59
-
60
- it 'can be inherited' do
61
- get '/sub', page: 1
62
- expect(json).to eq [1, 2]
63
- end
64
- end
65
- end