在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:davidcelis/api-pagination开源软件地址:https://github.com/davidcelis/api-pagination开源编程语言:Ruby 100.0%开源软件介绍:api-paginationPaginate in your headers, not in your response body. This follows the proposed RFC-8288 standard for Web linking. InstallationIn your # Requires Rails (Rails-API is also supported), or Grape
# v0.10.0 or later. If you're on an earlier version of
# Grape, use api-pagination v3.0.2.
gem 'rails', '>= 3.0.0'
gem 'rails-api'
gem 'grape', '>= 0.10.0'
# Then choose your preferred paginator from the following:
gem 'pagy'
gem 'kaminari'
gem 'will_paginate'
# Finally...
gem 'api-pagination' Configuration (optional)By default, api-pagination will detect whether you're using Pagy, Kaminari, or WillPaginate, and it will name headers appropriately. If you want to change any of the configurable settings, you may do so: ApiPagination.configure do |config|
# If you have more than one gem included, you can choose a paginator.
config.paginator = :kaminari # or :will_paginate
# By default, this is set to 'Total'
config.total_header = 'X-Total'
# By default, this is set to 'Per-Page'
config.per_page_header = 'X-Per-Page'
# Optional: set this to add a header with the current page number.
config.page_header = 'X-Page'
# Optional: set this to add other response format. Useful with tools that define :jsonapi format
config.response_formats = [:json, :xml, :jsonapi]
# Optional: what parameter should be used to set the page option
config.page_param = :page
# or
config.page_param do |params|
params[:page][:number] if params[:page].is_a?(ActionController::Parameters)
end
# Optional: what parameter should be used to set the per page option
config.per_page_param = :per_page
# or
config.per_page_param do |params|
params[:page][:size] if params[:page].is_a?(ActionController::Parameters)
end
# Optional: Include the total and last_page link header
# By default, this is set to true
# Note: When using kaminari, this prevents the count call to the database
config.include_total = false
end Pagy-specific configurationPagy does not have a built-in way to specify a maximum number of items per page, but Pagy::DEFAULT[:max_per_page] = 100 If left unconfigured, clients can request as many items per page as they wish, so it's highly recommended that you configure this. RailsIn your controller, provide a pageable collection to the class MoviesController < ApplicationController
# GET /movies
def index
movies = Movie.all # Movie.scoped if using ActiveRecord 3.x
paginate json: movies
end
# GET /movies/:id/cast
def cast
actors = Movie.find(params[:id]).actors
# Override how many Actors get returned. If unspecified,
# params[:per_page] (which defaults to 25) will be used.
paginate json: actors, per_page: 10
end
end This will pull your collection from the class MoviesController < ApplicationController
# GET /movies
def index
movies = paginate Movie.all
render json: MoviesSerializer.new(movies)
end
# GET /movies/:id/cast
def cast
actors = paginate Movie.find(params[:id]).actors, per_page: 10
render json: ActorsSerializer.new(actors)
end
end Note that the collection sent to NOTE: In versions 4.4.0 and below, the class API::ApplicationController < ActionController::Base
include Rails::Pagination
end GrapeWith Grape, class MoviesAPI < Grape::API
format :json
desc 'Return a paginated set of movies'
paginate
get do
# This method must take an ActiveRecord::Relation
# or some equivalent pageable set.
paginate Movie.all
end
route_param :id do
desc "Return one movie's cast, paginated"
# Override how many Actors get returned. If unspecified,
# params[:per_page] (which defaults to 25) will be used.
# There is no default for `max_per_page`.
paginate per_page: 10, max_per_page: 200
get :cast do
paginate Movie.find(params[:id]).actors
end
desc "Return one movie's awards, paginated"
# Enforce max_per_page value will add the alowed values
# to the swagger docs, and cause grape to return an error
# if outside that range
paginate per_page: 10, max_per_page: 200, enforce_max_per_page: true
get :awards do
paginate Movie.find(params[:id]).awards
end
end
end HeadersThen $ curl --include 'https://localhost:3000/movies?page=5'
HTTP/1.1 200 OK
Link: <http://localhost:3000/movies?page=1>; rel="first",
<http://localhost:3000/movies?page=173>; rel="last",
<http://localhost:3000/movies?page=6>; rel="next",
<http://localhost:3000/movies?page=4>; rel="prev"
Total: 4321
Per-Page: 10
# ... A Note on Kaminari and WillPaginateapi-pagination requires either Kaminari or WillPaginate in order to function, but some users may find themselves in situations where their application includes both. For example, you may have included ActiveAdmin (which uses Kaminari for pagination) and WillPaginate to do your own pagination. While it's suggested that you remove one paginator gem or the other, if you're unable to do so, you must configure api-pagination explicitly: ApiPagination.configure do |config|
config.paginator = :will_paginate
end If you don't do this, an annoying warning will print once your app starts seeing traffic. You should also configure Kaminari to use a different name for its Kaminari.configure do |config|
config.page_method_name = :per_page_kaminari
end |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论