https://github.com/bsm/grape-kaminari
kaminari paginator integration for grape API framework
https://github.com/bsm/grape-kaminari
Last synced: about 1 year ago
JSON representation
kaminari paginator integration for grape API framework
- Host: GitHub
- URL: https://github.com/bsm/grape-kaminari
- Owner: bsm
- License: mit
- Created: 2013-12-06T19:12:00.000Z (over 12 years ago)
- Default Branch: main
- Last Pushed: 2024-10-28T18:28:11.000Z (over 1 year ago)
- Last Synced: 2025-04-03T22:07:42.811Z (about 1 year ago)
- Language: Ruby
- Homepage:
- Size: 86.9 KB
- Stars: 160
- Watchers: 55
- Forks: 83
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Grape::Kaminari
[](https://github.com/bsm/grape-kaminari/actions/workflows/test.yml)
[](https://opensource.org/licenses/MIT)
[kaminari](https://github.com/kaminari/kaminari) paginator integration for [grape](https://github.com/intridea/grape) API framework.
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'grape-kaminari'
```
And then execute:
```
$ bundle
```
Or install it yourself as:
```
$ gem install grape-kaminari
```
## Usage
```ruby
class MyApi < Grape::API
# Include Grape::Kaminari module in your api
include Grape::Kaminari
resource :posts do
desc 'Return a list of posts.'
# This will add three optional params: page, per_page, and offset
#
# You can optionally overwrite the default :per_page setting (10)
# and the :max_per_page(false/disabled) setting which will use a validator to
# check that per_page is below the given number.
#
# You can disable the offset parameter from appearing in the API
# documentation by setting it to false.
#
params do
use :pagination, per_page: 20, max_per_page: 30, offset: 5
end
get do
posts = Post.where(...)
# Use `paginate` helper to execute kaminari methods
# with arguments automatically passed from params
paginate(posts)
end
get do
things = ['a', 'standard', 'array', 'of', 'things', '...']
# Use `Kaminari.paginate_array` method to convert the array
# into an object that can be passed to `paginate` helper.
paginate(Kaminari.paginate_array(things))
end
end
end
```
Now you can make a HTTP request to your endpoint with the following parameters
- `page`: your current page (default: 1)
- `per_page`: how many to record in a page (default: 10)
- `offset`: the offset to start from (default: 0)
```
curl -v http://host.dev/api/posts?page=3&offset=10
```
and the response will be paginated and also will include pagination headers
```
X-Total: 42
X-Total-Pages: 5
X-Page: 3
X-Per-Page: 10
X-Next-Page: 4
X-Prev-Page: 2
X-Offset: 10
```
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/bsm/grape-kaminari.