grape-pagy 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 52b012ea708cc341716fae9dc594335ef2b2de57498dca9643e89cb38caf97e2
4
+ data.tar.gz: 264bcfea9ce1e8e7310c12e2e68e9e70a5482d6a7d2fd6f506c5d8ed9bac6715
5
+ SHA512:
6
+ metadata.gz: ac1d11277bb770f7a56f9b88d966ba2e948d593fec46f28140a1b7961c2a14df95b2c7e48f48a59a4dafa8008c4498e89ea7d1050956398a54a9a1816bdfd9fc
7
+ data.tar.gz: 8e23241b263e2fa2164674847b915517d93831ede7da6a49add2b4f5252ddb2a3621d215922799649335d79459a2727c8372fdfd563e8a0b64f4300fd29f6a26
@@ -0,0 +1,56 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ # Ignore Byebug command history file.
17
+ .byebug_history
18
+
19
+ ## Specific to RubyMotion:
20
+ .dat*
21
+ .repl_history
22
+ build/
23
+ *.bridgesupport
24
+ build-iPhoneOS/
25
+ build-iPhoneSimulator/
26
+
27
+ ## Specific to RubyMotion (use of CocoaPods):
28
+ #
29
+ # We recommend against adding the Pods directory to your .gitignore. However
30
+ # you should judge for yourself, the pros and cons are mentioned at:
31
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
32
+ #
33
+ # vendor/Pods/
34
+
35
+ ## Documentation cache and generated files:
36
+ /.yardoc/
37
+ /_yardoc/
38
+ /doc/
39
+ /rdoc/
40
+
41
+ ## Environment normalization:
42
+ /.bundle/
43
+ /vendor/bundle
44
+ /lib/bundler/man/
45
+
46
+ # for a library or gem, you might want to ignore these files since the code is
47
+ # intended to run in multiple environments; otherwise, check them in:
48
+ # Gemfile.lock
49
+ # .ruby-version
50
+ # .ruby-gemset
51
+
52
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
53
+ .rvmrc
54
+
55
+ # Used by RuboCop. Remote config files pulled in from inherit_from directive.
56
+ .rubocop-https?--*
@@ -0,0 +1,8 @@
1
+ inherit_from:
2
+ - https://gitlab.com/bsm/misc/raw/master/rubocop/default.yml
3
+
4
+ AllCops:
5
+ TargetRubyVersion: "2.5"
6
+
7
+ Naming/FileName:
8
+ Exclude: ["lib/grape-pagy.rb"]
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ cache: bundler
3
+ rvm:
4
+ - 2.7
5
+ - 2.6
6
+ - 2.5
@@ -0,0 +1,3 @@
1
+ ### 0.1.0
2
+
3
+ * Initial release
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
@@ -0,0 +1,114 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ grape-pagy (0.1.0)
5
+ grape (>= 1.5)
6
+ pagy
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activesupport (6.0.3.3)
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
+ minitest (5.14.2)
53
+ mustermann (1.1.1)
54
+ ruby2_keywords (~> 0.0.1)
55
+ mustermann-grape (1.0.1)
56
+ mustermann (>= 1.0.0)
57
+ pagy (3.8.3)
58
+ parallel (1.19.2)
59
+ parser (2.7.2.0)
60
+ ast (~> 2.4.1)
61
+ rack (2.2.3)
62
+ rack-accept (0.4.5)
63
+ rack (>= 0.4)
64
+ rack-test (1.1.0)
65
+ rack (>= 1.0, < 3)
66
+ rainbow (3.0.0)
67
+ rake (13.0.1)
68
+ regexp_parser (1.8.1)
69
+ rexml (3.2.4)
70
+ rspec (3.9.0)
71
+ rspec-core (~> 3.9.0)
72
+ rspec-expectations (~> 3.9.0)
73
+ rspec-mocks (~> 3.9.0)
74
+ rspec-core (3.9.3)
75
+ rspec-support (~> 3.9.3)
76
+ rspec-expectations (3.9.2)
77
+ diff-lcs (>= 1.2.0, < 2.0)
78
+ rspec-support (~> 3.9.0)
79
+ rspec-mocks (3.9.1)
80
+ diff-lcs (>= 1.2.0, < 2.0)
81
+ rspec-support (~> 3.9.0)
82
+ rspec-support (3.9.3)
83
+ rubocop (0.92.0)
84
+ parallel (~> 1.10)
85
+ parser (>= 2.7.1.5)
86
+ rainbow (>= 2.2.2, < 4.0)
87
+ regexp_parser (>= 1.7)
88
+ rexml
89
+ rubocop-ast (>= 0.5.0)
90
+ ruby-progressbar (~> 1.7)
91
+ unicode-display_width (>= 1.4.0, < 2.0)
92
+ rubocop-ast (0.7.1)
93
+ parser (>= 2.7.1.5)
94
+ ruby-progressbar (1.10.1)
95
+ ruby2_keywords (0.0.2)
96
+ thread_safe (0.3.6)
97
+ tzinfo (1.2.7)
98
+ thread_safe (~> 0.1)
99
+ unicode-display_width (1.7.0)
100
+ zeitwerk (2.4.0)
101
+
102
+ PLATFORMS
103
+ ruby
104
+
105
+ DEPENDENCIES
106
+ bundler
107
+ grape-pagy!
108
+ rack-test
109
+ rake
110
+ rspec
111
+ rubocop
112
+
113
+ BUNDLED WITH
114
+ 2.1.4
data/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ Copyright 2020 Black Square Media Ltd
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
@@ -0,0 +1,83 @@
1
+ # Grape::Pagy
2
+
3
+ [![Build Status](https://travis-ci.org/bsm/grape-pagy.png?branch=master)](https://travis-ci.org/bsm/grape-pagy)
4
+ [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
5
+
6
+ [Pagy](https://github.com/ddnexus/pagy) pagination for [grape](https://github.com/ruby-grape/grape) API framework.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'grape-pagy'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ ```
19
+ $ bundle
20
+ ```
21
+
22
+ Or install it yourself as:
23
+
24
+ ```
25
+ $ gem install grape-pagy
26
+ ```
27
+
28
+ ## Usage
29
+
30
+ ```ruby
31
+ class MyApi < Grape::API
32
+ # Include helpers in your API.
33
+ helpers Grape::Pagy::Helpers
34
+
35
+ resource :posts do
36
+ desc 'Return a list of posts.'
37
+ params do
38
+ # This will add two optional params: :page and :items.
39
+ # The parameter names can be globally configured by adjusting
40
+ # global Pagy::VARS[:page_param] and Pagy::VARS[:item_param]
41
+ # settings.
42
+ use :pagination
43
+ end
44
+ get do
45
+ posts = Post.all.order(created_at: :desc)
46
+ paginate(posts)
47
+ end
48
+ end
49
+
50
+ resource :strings do
51
+ params do
52
+ # Override the number items returned, defaults to Pagy::VARS[:items].
53
+ use :pagination, items: 2
54
+ end
55
+ get do
56
+ words = %w[an array of words]
57
+ # This supports array as well as relations.
58
+ # Allows to override global Pagy::VARS settings.
59
+ paginate(words, max_items: )
60
+ end
61
+ end
62
+ end
63
+ ```
64
+
65
+ Example request:
66
+
67
+ ```shell
68
+ curl -si http://localhost:8080/api/posts?page=3&items=5
69
+ ```
70
+
71
+ The response will be paginated and also will include the following headers:
72
+
73
+ ```
74
+ Current-Page: 3
75
+ Page-Items: 5
76
+ Total-Count: 22
77
+ Total-Pages: 5
78
+ Link: <http://localhost:8080/api/posts?page=1&items=5>; rel="first", <http://localhost:8080/api/posts?page=4&items=5>; rel="next", <http://localhost:8080/api/posts?page=2&items=5>; rel="prev", <http://localhost:8080/api/posts?page=5&items=5>; rel="last"),
79
+ ```
80
+
81
+ ## Contributing
82
+
83
+ Bug reports and pull requests are welcome on GitHub at https://github.com/bsm/grape-pagy.
@@ -0,0 +1,8 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rubocop/rake_task'
3
+ require 'rspec/core/rake_task'
4
+
5
+ RuboCop::RakeTask.new
6
+ RSpec::Core::RakeTask.new
7
+
8
+ task default: %i[rubocop spec]
@@ -0,0 +1,24 @@
1
+ Gem::Specification.new do |spec|
2
+ spec.name = 'grape-pagy'
3
+ spec.version = '0.1.0'
4
+ spec.authors = ['Black Square Media']
5
+ spec.email = ['info@blacksquaremedia.com']
6
+ spec.description = 'Pagy paginator for grape API'
7
+ spec.summary = ''
8
+ spec.homepage = ''
9
+ spec.license = 'Apache-2.0'
10
+
11
+ spec.files = `git ls-files -z`.split("\x0").reject {|f| f.start_with?('spec/') }
12
+ spec.test_files = `git ls-files -z -- spec/*`.split("\x0")
13
+ spec.require_paths = ['lib']
14
+ spec.required_ruby_version = '>= 2.5'
15
+
16
+ spec.add_runtime_dependency 'grape', '>= 1.5'
17
+ spec.add_runtime_dependency 'pagy'
18
+
19
+ spec.add_development_dependency 'bundler'
20
+ spec.add_development_dependency 'rack-test'
21
+ spec.add_development_dependency 'rake'
22
+ spec.add_development_dependency 'rspec'
23
+ spec.add_development_dependency 'rubocop'
24
+ end
@@ -0,0 +1 @@
1
+ require 'grape/pagy'
@@ -0,0 +1,45 @@
1
+ require 'grape'
2
+ require 'pagy'
3
+ require 'pagy/extras/arel'
4
+ require 'pagy/extras/array'
5
+ require 'pagy/extras/headers'
6
+ require 'pagy/extras/items'
7
+ require 'pagy/extras/overflow'
8
+
9
+ module Grape
10
+ module Pagy
11
+ Pager = Struct.new :request, :params do
12
+ include ::Pagy::Backend
13
+
14
+ def paginate(collection, via: nil, **opts, &block)
15
+ pagy_with_items(opts)
16
+ via ||= if collection.respond_to?(:arel_table)
17
+ :arel
18
+ elsif collection.is_a?(Array)
19
+ :array
20
+ end
21
+
22
+ method = [:pagy, via].compact.join('_')
23
+ page, scope = send(method, collection, **opts)
24
+
25
+ pagy_headers(page).each(&block)
26
+ scope
27
+ end
28
+ end
29
+
30
+ module Helpers
31
+ extend Grape::API::Helpers
32
+
33
+ params :pagination do |items: ::Pagy::VARS[:items]|
34
+ optional ::Pagy::VARS[:page_param], type: Integer, default: 1, desc: 'Page offset to fetch.'
35
+ optional ::Pagy::VARS[:items_param], type: Integer, default: items, desc: 'Number of items to return per page.'
36
+ end
37
+
38
+ def paginate(collection, via: nil, **opts)
39
+ Pager.new(request, params).paginate(collection, via: via, **opts) do |key, value|
40
+ header key, value
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,73 @@
1
+ require 'spec_helper'
2
+
3
+ describe Grape::Pagy do
4
+ include Rack::Test::Methods
5
+
6
+ let(:app) { TestAPI }
7
+
8
+ it 'should paginate' do
9
+ get '/'
10
+ expect(last_response.status).to eq(200)
11
+ expect(last_response.headers).to include(
12
+ 'Current-Page' => '1',
13
+ 'Link' => %(<http://example.org/?page=1>; rel="first", <http://example.org/?page=2>; rel="next", <http://example.org/?page=3>; rel="last"),
14
+ 'Page-Items' => '5',
15
+ 'Total-Count' => '12',
16
+ 'Total-Pages' => '3',
17
+ )
18
+ expect(last_response.body).to eq(%([1, 2, 3, 4, 5]))
19
+ end
20
+
21
+ it 'should accept page and items parameters' do
22
+ get '/?page=2&items=3'
23
+ expect(last_response.status).to eq(200)
24
+ expect(last_response.headers).to include(
25
+ 'Current-Page' => '2',
26
+ 'Page-Items' => '3',
27
+ 'Total-Count' => '12',
28
+ 'Total-Pages' => '4',
29
+ )
30
+ expect(last_response.body).to eq(%([4, 5, 6]))
31
+ end
32
+
33
+ it 'should cap items' do
34
+ get '/?items=10'
35
+ expect(last_response.headers).to include('Page-Items' => '6')
36
+ expect(last_response.body).to eq(%([1, 2, 3, 4, 5, 6]))
37
+
38
+ get '/?items=3'
39
+ expect(last_response.headers).to include('Page-Items' => '3')
40
+ expect(last_response.body).to eq(%([1, 2, 3]))
41
+ end
42
+
43
+ it 'should ignore overflow' do
44
+ get '/?page=99'
45
+ expect(last_response.status).to eq(200)
46
+ expect(last_response.headers).to include(
47
+ 'Current-Page' => '99',
48
+ 'Total-Pages' => '3',
49
+ )
50
+ expect(last_response.body).to eq(%([]))
51
+ end
52
+
53
+ it 'should inherit helper' do
54
+ get '/sub'
55
+ expect(last_response.status).to eq(200)
56
+ expect(last_response.headers).to include(
57
+ 'Current-Page' => '1',
58
+ 'Page-Items' => '10',
59
+ 'Total-Count' => '12',
60
+ 'Total-Pages' => '2',
61
+ )
62
+ expect(last_response.body).to eq(%([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]))
63
+
64
+ get '/sub?items=20'
65
+ expect(last_response.status).to eq(200)
66
+ expect(last_response.headers).to include(
67
+ 'Current-Page' => '1',
68
+ 'Page-Items' => '20',
69
+ 'Total-Count' => '12',
70
+ 'Total-Pages' => '1',
71
+ )
72
+ end
73
+ end
@@ -0,0 +1,33 @@
1
+ ENV['RACK_ENV'] ||= 'test'
2
+
3
+ require 'rspec'
4
+ require 'grape/pagy'
5
+ require 'rack/test'
6
+
7
+ Pagy::VARS[:items] = 10
8
+ Pagy::VARS[:max_items] = 20
9
+
10
+ class TestAPI < Grape::API
11
+ helpers Grape::Pagy::Helpers
12
+
13
+ params do
14
+ use :pagination, items: 5
15
+ end
16
+ get '' do
17
+ paginate (1..12).to_a, max_items: 6
18
+ end
19
+
20
+ resource :sub do
21
+ params do
22
+ use :pagination
23
+ end
24
+ get '/' do
25
+ paginate (1..12).to_a
26
+ end
27
+ end
28
+ end
29
+
30
+ RSpec.configure do |config|
31
+ config.include Rack::Test::Methods
32
+ config.raise_errors_for_deprecations!
33
+ end
metadata ADDED
@@ -0,0 +1,157 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: grape-pagy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Black Square Media
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-10-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: grape
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pagy
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rack-test
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Pagy paginator for grape API
112
+ email:
113
+ - info@blacksquaremedia.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rubocop.yml"
120
+ - ".travis.yml"
121
+ - CHANGELOG.md
122
+ - Gemfile
123
+ - Gemfile.lock
124
+ - LICENSE
125
+ - README.md
126
+ - Rakefile
127
+ - grape-pagy.gemspec
128
+ - lib/grape-pagy.rb
129
+ - lib/grape/pagy.rb
130
+ - spec/grape/pagy_spec.rb
131
+ - spec/spec_helper.rb
132
+ homepage: ''
133
+ licenses:
134
+ - Apache-2.0
135
+ metadata: {}
136
+ post_install_message:
137
+ rdoc_options: []
138
+ require_paths:
139
+ - lib
140
+ required_ruby_version: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '2.5'
145
+ required_rubygems_version: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ requirements: []
151
+ rubygems_version: 3.1.2
152
+ signing_key:
153
+ specification_version: 4
154
+ summary: ''
155
+ test_files:
156
+ - spec/grape/pagy_spec.rb
157
+ - spec/spec_helper.rb