grape-pagination 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/.rspec +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +42 -0
- data/Rakefile +10 -0
- data/grape-pagination.gemspec +26 -0
- data/lib/grape/pagination/configuration.rb +9 -0
- data/lib/grape/pagination/extensions.rb +17 -0
- data/lib/grape/pagination/helpers.rb +7 -0
- data/lib/grape/pagination/paginator.rb +56 -0
- data/lib/grape/pagination/version.rb +5 -0
- data/lib/grape/pagination.rb +22 -0
- data/lib/grape-pagination.rb +1 -0
- data/spec/grape/pagination/paginator_spec.rb +20 -0
- data/spec/spec_helper.rb +6 -0
- metadata +134 -0
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Eric J. Holmes
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# Grape Pagination
|
2
|
+
|
3
|
+
Provides helpers for paginating collections in [Grape](https://github.com/intridea/grape)
|
4
|
+
endpoints. Currently only works with with [will\_paginate](https://github.com/mislav/will_paginate),
|
5
|
+
or something that responds to the same `paginate` interface.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'grape-pagination'
|
13
|
+
```
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
|
17
|
+
Tag your endpoint as supporting pagination params, then use the `paginate`
|
18
|
+
helper.
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
class API < Grape::API
|
22
|
+
desc 'Gets everything!'
|
23
|
+
paginate
|
24
|
+
get do
|
25
|
+
paginate collection
|
26
|
+
end
|
27
|
+
end
|
28
|
+
```
|
29
|
+
|
30
|
+
Which would result in a paginated collection and the following headers set:
|
31
|
+
|
32
|
+
```
|
33
|
+
X-Total: 150
|
34
|
+
```
|
35
|
+
|
36
|
+
## Contributing
|
37
|
+
|
38
|
+
1. Fork it
|
39
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
40
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
41
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
42
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'grape/pagination/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'grape-pagination'
|
8
|
+
spec.version = Grape::Pagination::VERSION
|
9
|
+
spec.authors = ['Eric J. Holmes']
|
10
|
+
spec.email = ['eric@ejholmes.net']
|
11
|
+
spec.description = %q{Pagination helpers for Grape}
|
12
|
+
spec.summary = %q{Pagination helpers for Grape}
|
13
|
+
spec.homepage = 'https://github.com/remind101/grape-pagination'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_dependency 'grape', '~> 0.3'
|
22
|
+
|
23
|
+
spec.add_development_dependency 'bundler', '~> 1.3'
|
24
|
+
spec.add_development_dependency 'rspec'
|
25
|
+
spec.add_development_dependency 'rake'
|
26
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Grape::Pagination
|
2
|
+
module Extensions
|
3
|
+
def paginate(options = {})
|
4
|
+
options.reverse_merge!(per_page: 30)
|
5
|
+
params do
|
6
|
+
optional :page,
|
7
|
+
type: Integer,
|
8
|
+
desc: 'Page offset to fetch.',
|
9
|
+
default: 1
|
10
|
+
optional :per_page,
|
11
|
+
type: Integer,
|
12
|
+
desc: 'Number of results to return per page.',
|
13
|
+
default: options[:per_page]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module Grape::Pagination
|
2
|
+
class Paginator
|
3
|
+
extend Forwardable
|
4
|
+
|
5
|
+
TOTAL_HEADER = 'X-Total'.freeze
|
6
|
+
LINK_HEADER = 'Link'.freeze
|
7
|
+
|
8
|
+
attr_reader :endpoint
|
9
|
+
attr_reader :collection
|
10
|
+
attr_reader :options
|
11
|
+
|
12
|
+
def self.paginate(*args)
|
13
|
+
new(*args).paginate
|
14
|
+
end
|
15
|
+
|
16
|
+
def initialize(endpoint, collection, options = {})
|
17
|
+
@endpoint = endpoint
|
18
|
+
@collection = collection
|
19
|
+
@options = options
|
20
|
+
end
|
21
|
+
|
22
|
+
def paginate
|
23
|
+
header TOTAL_HEADER, total
|
24
|
+
collection.paginate(page_params)
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def_delegators :endpoint, :header, :params, :request
|
30
|
+
|
31
|
+
def query_hash
|
32
|
+
@query_hash ||= Rack::Utils.parse_nested_query(uri.query)
|
33
|
+
end
|
34
|
+
|
35
|
+
def uri
|
36
|
+
@uri ||= URI.parse(request.url)
|
37
|
+
end
|
38
|
+
|
39
|
+
def page_params
|
40
|
+
@page_params ||= params.slice(:page, :per_page).to_h.symbolize_keys
|
41
|
+
end
|
42
|
+
|
43
|
+
def total
|
44
|
+
options[:total] || total_proc.call(collection)
|
45
|
+
end
|
46
|
+
|
47
|
+
def total_proc
|
48
|
+
options[:total_proc] || configuration.total_proc
|
49
|
+
end
|
50
|
+
|
51
|
+
def configuration
|
52
|
+
Grape::Pagination.configuration
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'grape'
|
2
|
+
require 'grape/pagination/version'
|
3
|
+
|
4
|
+
module Grape
|
5
|
+
module Pagination
|
6
|
+
autoload :Configuration, 'grape/pagination/configuration'
|
7
|
+
autoload :Extensions, 'grape/pagination/extensions'
|
8
|
+
autoload :Helpers, 'grape/pagination/helpers'
|
9
|
+
autoload :Paginator, 'grape/pagination/paginator'
|
10
|
+
|
11
|
+
def self.configuration
|
12
|
+
@configuration ||= Configuration.new
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.configure
|
16
|
+
yield configuration
|
17
|
+
end
|
18
|
+
|
19
|
+
Grape::API.send :extend, Grape::Pagination::Extensions
|
20
|
+
Grape::Endpoint.send :include, Grape::Pagination::Helpers
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'grape/pagination'
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Grape::Pagination::Paginator do
|
4
|
+
let(:request ) { double(Rack::Request, url: 'https://localhost:5000/api/v1/tweets?page=1&per_page=30') }
|
5
|
+
let(:endpoint ) { double(Grape::Endpoint, request: request, header: nil, params: Hashie::Mash.new(page: 1, per_page: 30)) }
|
6
|
+
let(:collection ) { double('collection', count: 4, paginate: nil) }
|
7
|
+
subject(:paginator) { described_class.new endpoint, collection }
|
8
|
+
|
9
|
+
describe '.paginate' do
|
10
|
+
it 'sets the X-Total header' do
|
11
|
+
endpoint.should_receive(:header).with('X-Total', 4)
|
12
|
+
paginator.paginate
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'paginates the collection' do
|
16
|
+
collection.should_receive(:paginate).with(page: 1, per_page: 30)
|
17
|
+
paginator.paginate
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: grape-pagination
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Eric J. Holmes
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-08-17 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: grape
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0.3'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0.3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: bundler
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '1.3'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '1.3'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rake
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: Pagination helpers for Grape
|
79
|
+
email:
|
80
|
+
- eric@ejholmes.net
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .gitignore
|
86
|
+
- .rspec
|
87
|
+
- Gemfile
|
88
|
+
- LICENSE.txt
|
89
|
+
- README.md
|
90
|
+
- Rakefile
|
91
|
+
- grape-pagination.gemspec
|
92
|
+
- lib/grape-pagination.rb
|
93
|
+
- lib/grape/pagination.rb
|
94
|
+
- lib/grape/pagination/configuration.rb
|
95
|
+
- lib/grape/pagination/extensions.rb
|
96
|
+
- lib/grape/pagination/helpers.rb
|
97
|
+
- lib/grape/pagination/paginator.rb
|
98
|
+
- lib/grape/pagination/version.rb
|
99
|
+
- spec/grape/pagination/paginator_spec.rb
|
100
|
+
- spec/spec_helper.rb
|
101
|
+
homepage: https://github.com/remind101/grape-pagination
|
102
|
+
licenses:
|
103
|
+
- MIT
|
104
|
+
post_install_message:
|
105
|
+
rdoc_options: []
|
106
|
+
require_paths:
|
107
|
+
- lib
|
108
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
109
|
+
none: false
|
110
|
+
requirements:
|
111
|
+
- - ! '>='
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
segments:
|
115
|
+
- 0
|
116
|
+
hash: -2291269891516805911
|
117
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
118
|
+
none: false
|
119
|
+
requirements:
|
120
|
+
- - ! '>='
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '0'
|
123
|
+
segments:
|
124
|
+
- 0
|
125
|
+
hash: -2291269891516805911
|
126
|
+
requirements: []
|
127
|
+
rubyforge_project:
|
128
|
+
rubygems_version: 1.8.23
|
129
|
+
signing_key:
|
130
|
+
specification_version: 3
|
131
|
+
summary: Pagination helpers for Grape
|
132
|
+
test_files:
|
133
|
+
- spec/grape/pagination/paginator_spec.rb
|
134
|
+
- spec/spec_helper.rb
|