https://github.com/bsm/grape-pagy
https://github.com/bsm/grape-pagy
Last synced: 8 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/bsm/grape-pagy
- Owner: bsm
- License: apache-2.0
- Created: 2020-10-07T16:54:45.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2024-08-02T01:39:33.000Z (almost 2 years ago)
- Last Synced: 2025-03-22T19:02:31.930Z (about 1 year ago)
- Language: Ruby
- Size: 35.2 KB
- Stars: 10
- Watchers: 5
- Forks: 10
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Grape::Pagy
[](https://github.com/bsm/grape-pagy/actions/workflows/ruby.yml)
[](https://opensource.org/licenses/Apache-2.0)
[Pagy](https://github.com/ddnexus/pagy) pagination for [grape](https://github.com/ruby-grape/grape) API framework.
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'grape-pagy'
```
And then execute:
```
$ bundle
```
Or install it yourself as:
```
$ gem install grape-pagy
```
## Usage
```ruby
class MyApi < Grape::API
# Include helpers in your API.
helpers Grape::Pagy::Helpers
resource :posts do
desc 'Return a list of posts.'
params do
# This will add two optional params: :page and :items.
use :pagy
end
get do
posts = Post.all.order(created_at: :desc)
pagy(posts)
end
end
resource :strings do
desc 'Supports arrays as well as relations.'
params do
# Override defaults by setting Pagy::DEFAULT or by passing options.
use :pagy,
items_param: :per_page, # Accept per_page=N param to limit items.
items: 2, # If per_page param is blank, default to 2.
max_items: 10 # Restrict per_page to maximum 10.
end
get do
words = %w[this is a plain array of words]
pagy(words)
end
end
end
```
Example request:
```shell
curl -si http://localhost:8080/api/posts?page=3&items=5
```
The response will be paginated and also will include the following headers:
```
Current-Page: 3
Page-Items: 5
Total-Count: 22
Total-Pages: 5
Link: ; rel="first", ; rel="next", ; rel="prev", ; rel="last"),
```
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/bsm/grape-pagy.