标签: grape-entity

葡萄错误处理策略?

我正在使用Grape和Rails来创建REST API.我有基本的架构,我正在寻找"清理"的地方.其中一个地方是错误处理/处理.

我目前正在整个API的root.rb(GRAPE :: API基类)文件中解决错误.我格式化它们然后通过rack_response发回错误.一切正常,但是root.rb文件变得有点臃肿,所有的错误都被拯救了,其中一些有特殊的解析需要完成.我想知道是否有人为错误处理制定了一个好的策略,以便它可以移动到它自己的模块中并使root.rb(GRAPE :: API基类)相当精简.

我真的想创建一个错误处理模块并为每种类型的错误定义方法,例如......

module API
 module ErrorHandler
   def record_not_found
     rack_response API::Utils::ApiErrors.new({type: e.class.name, message: 'Record not found'}).to_json, 404
   end
 end
end
Run Code Online (Sandbox Code Playgroud)

然后在root.rb文件中执行类似的操作

module API
  class Root < Grape::API
    prefix 'api'
    format :json

    helpers API::ErrorHandler

    rescue_from ActiveRecord::RecordNotFound, with: :record_not_found # Use the helper method as the handler for this error
  end
end
Run Code Online (Sandbox Code Playgroud)

有没有人做过这样的事情?我一直在尝试上述策略的各种风格,但我似乎无法做任何事情.

ruby rest ruby-on-rails ruby-grape grape-entity

8
推荐指数
1
解决办法
3692
查看次数

葡萄:葡萄实体需要的参数

我正在编写一个带有葡萄的API服务器,我选择使用葡萄实体,因为它能够自动生成swagger的文档.但是,当我根据需要设置一个参数时,我有一个问题.因为葡萄不能证实参数存在.看起来葡萄忽略required: true了实体的参数.

app.rb

module Smart
  module Version1
    class App < BaseApi

      resource :app do

        # POST /app
        desc 'Creates a new app' do
          detail 'It is used to re gister a new app on the server and get the app_id'
          params  Entities::OSEntity.documentation
          success Entities::AppEntity
          failure [[401, 'Unauthorized', Entities::ErrorEntity]]
          named 'My named route'
        end
        post do
          app = ::App.create params
          present app, with: Entities::AppEntity
        end
      end
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

os_entity.rb

module Smart
  module Entities
    class OSEntity < Grape::Entity

      expose …
Run Code Online (Sandbox Code Playgroud)

ruby api ruby-on-rails ruby-grape grape-entity

7
推荐指数
1
解决办法
4171
查看次数

Grape实体和单表继承

假设我有基类"Attachment <ActiveRecord :: Base"和2个子类"Attachment :: Video <Attachment","Attachment :: Image <Attachment".

present Attachment.all, with: API::Entities::AttachmentEntity
Run Code Online (Sandbox Code Playgroud)

但我想为不同的子类使用不同的实体.附件::视频应与API :: Entities :: AttachmentEntity :: Video等一起出现

葡萄有可能吗?

inheritance ruby-grape grape-entity

7
推荐指数
0
解决办法
111
查看次数

如何将 Rails Grape API 与 Ember 反身模型连接起来

我在将来自 Grape(用 Grape 实体序列化)的 Json 有效负载与 Ember 反射模型连接时遇到问题。模型看起来像这样:

Category = DS.Model.extend {
  name: DS.attr 'string',
  children: DS.hasMany 'category', inverse: 'parent',
  parent: DS.belongsTo 'category', inverse 'children'
}
Run Code Online (Sandbox Code Playgroud)

因此,正如您所看到的,我正在尝试对类别-子类别关系进行建模。来自端点的示例 json 响应是:

{
  "category": {
    "id": 1,
    "name": "Sport",
    "child_ids": [
      5,
      6,
      8,
      7
    ]
  },
  "children": [
    {
      "id": 5,
      "name": "Basketball",
      "parent_id": 1
    },
    {
      "id": 6,
      "name": "Football",
      "parent_id": 1
    },
    {
      "id": 8,
      "name": "Running",
      "parent_id": 1
    },
    {
      "id": 7,
      "name": "Volleyball",
      "parent_id": 1
    }
  ]
} …
Run Code Online (Sandbox Code Playgroud)

json ruby-grape ember.js ember-data grape-entity

5
推荐指数
1
解决办法
136
查看次数

为 Grape 实体添加常量属性

我正在尝试使用 Rails 和 Grape 构建一组 API。用户模型如下:

{
    "email": "foo@bar.com"
    "name": "Foo Bar"
}
Run Code Online (Sandbox Code Playgroud)

现在,在 API 表示级别,我希望用户对象是这样的:

{
    "object": "User"
    "email": "foo@bar.com"
    "name": "Foo Bar"
}
Run Code Online (Sandbox Code Playgroud)

由于我使用 Grape Entity gem 来公开我的模型,因此问题实际上是:如何向 Grape Entity 类添加额外的 CONSTANT-value 属性?感谢你的帮助!

rest ruby-on-rails grape-entity

3
推荐指数
1
解决办法
1222
查看次数