简体   繁体   English

使用AMS和自定义JSON进行Rails Grape API分页

[英]Rails Grape API pagination with AMS and custom JSON

Doing in Rails what may require a more complex/creative solution, but basically using Grape, active_model_serializers (AMS), and grape-kaminari to return an index route (list) of User objects, and I'd like to be able to use the advantages of AMS with caching and easily serializing objects, but customize the JSON response. 在Rails中执行可能需要更复杂/创意的解决方案,但是基本上使用Grape,active_model_serializers(AMS)和grape-kaminari返回User对象的索引路由(列表),并且我希望能够使用AMS具有缓存和轻松序列化对象的优点,但可以自定义JSON响应。

Here's Grape: 这是葡萄:

module API
  module V1
    class Users < Grape::API
      include API::V1::Defaults
      include Grape::Kaminari

      resource :users do
         paginate per_page: 18
         get do
           users = User.all
           paginate users
         end
      end
    end
  end
end

And AMS: 和AMS:

class UserSerializer < ActiveModel::Serializer
  cache key: 'user', expires_in: 3.hours
  attributes :id, :firstname, :lastname, :email, :username
end

Ideal Response in JSON: JSON中的理想响应:

{
  users: [
    {
      id: 1,
      firstname: "Daniel",
      lastname: "Archer,
      email: "daniel@archer.co",
      username: "dja"
     },
    {
      id: 2,
      firstname: "Fakey",
      lastname: "McFakerson,
      email: "fake@fake.io",
      username: "fake"
     }
  ],
  extras: {
    user_count: 300,
    new_users_last_5_days: 10,
    new_users_last_30_days: 25
  }
}

What I'd like to happen (and having trouble with) is to paginate the amount of users in the JSON response, but include some extras that are related to the entire collection of Users. 我想发生的事情(并遇到麻烦)是在JSON响应中对用户数量进行分页,但要包括一些与整个用户集合有关的额外功能。 For example, I may have 300 Users, but I only want to return the first 18 users (or however many based on params per_page and page ), and extra data on the entire collection. 例如,我可能有300个用户,但我只想返回前18个用户(或许多基于参数per_pagepage ),以及整个集合的额外数据。

With Grape::Entity you can present multiple values. 使用Grape :: Entity,您可以显示多个值。

paginate users

just presents the selected page of users. 仅显示所选的用户页面。 You can add another present just after that: 您可以在此之后添加另一个礼物

present :extras, {user_count: User.count, ...}

To Answer your comment: 回答您的评论:
I'm using a similar setup and have a response that is paginated with kaminary and extended with extra data: 我使用的是类似的设置,并且响应中带有kaminary分页,并带有额外数据:

get do
  ...
  present :statistics, statistics, with: API::Entities::Statistics
  present :refresh_time, Setting.list_refresh_time
  present :users, @users.page(params[:page]).per(18), with: API::Entities::User
end

With vanilla Grape , a data structure is returned, that is converted dependent of the output format, ie with .to_json . 使用vanilla Grape ,将返回一个数据结构,该数据结构将根据输出格式进行转换,即使用.to_json So you can return a Hash: 因此,您可以返回哈希:

get do
  ...
  { statistics: some_statistics, 
    users: @users.page(params[:page]).per(18)
  }
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM