ruby-on-rails - 葡萄 : required params with grape-entity

标签 ruby-on-rails ruby api ruby-grape grape-entity

我正在用 grape 编写一个 API 服务器,我选择使用 grape-entity 因为它能够自动生成 swagger 的文档。 但是现在我在按要求设置参数时遇到了问题。因为葡萄不验证参数是否存在。看起来 grape 忽略了实体参数的 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

os_entity.rb

module Smart
  module Entities
    class OSEntity < Grape::Entity

      expose :os, documentation: { type: String, desc: 'Operative system name', values: App::OS_LIST, required: true }

    end
  end
end

app_entity.rb

module Smart
  module Entities
    class AppEntity < OSEntity

      expose :id, documentation: { type: 'integer', desc: 'Id of the created app', required: true }
      expose :customer_id, documentation: { type: 'integer', desc: 'Id of the customer', required: true }

    end
  end
end

现在其他一切都很好,但我不知道如何以 DRY 方式使用实体,并让 grape 验证参数的要求。

最佳答案

经过一些工作后,我能够让 grape 工作,因为我认为它应该工作。因为我不想重复验证和文档的代码。您只需要将其添加到初始化程序中(当然,如果您在 rails 中)。我还能够支持嵌套关联。如您所见,API 代码看起来很简单,swagger 看起来很完美。 以下是 API 和所有需要的实体:

app/api/smart/entities/characteristics_params_entity.rb

module Smart
  module Entities
    class CharacteristicsParamsEntity < Grape::Entity

      root :characteristics, :characteristic
      expose :id, documentation: { type: Integer, desc: 'Id of the characteristic' }

    end
  end
end

app/api/smart/entities/characterisitcs_entity.rb

module Smart
  module Entities
    class CharacteristicsEntity < CharacteristicsParamsEntity

      expose :id, documentation: { type: Integer, desc: 'Id of the characteristic' }
      expose :name, documentation: { type: String, desc: 'Name of the characteristic' }
      expose :description, documentation: { type: String, desc: 'Description of the characteristic' }
      expose :characteristic_type, documentation: { type: String, desc: 'Type of the characteristic' }
      expose :updated_at, documentation: { type: Date, desc: 'Last updated time of the characteristic' }

    end
  end
end

app/api/smart/entities/apps_params_entity.rb

module Smart
  module Entities
    class AppsParamsEntity < Grape::Entity

      expose :os, documentation: { type: String, desc: 'Operative system name', values: App::OS_LIST, required: true }
      expose :characteristic_ids, using: CharacteristicsParamsEntity, documentation: { type: CharacteristicsParamsEntity, desc: 'List of characteristic_id that the customer has', is_array: true }


    end
  end
end

app/api/smart/entities/apps_entity.rb

module Smart
  module Entities
    class AppsEntity < AppsParamsEntity

      unexpose :characteristic_ids
      expose :id, documentation: { type: 'integer', desc: 'Id of the created app', required: true }
      expose :customer_id, documentation: { type: 'integer', desc: 'Id of the customer', required: true }
      expose :characteristics, using: CharacteristicsEntity, documentation: { is_array: true, desc: 'List of characteristics that the customer has' }

    end
  end
end

app/api/smart/version1/apps.rb

module Smart
  module Version1
    class Apps < Version1::BaseAPI

    resource :apps do

        # POST /apps
        desc 'Creates a new app' do
          detail 'It is used to register a new app on the server and get the app_id'
          params Entities::AppsParamsEntity.documentation
          success Entities::AppsEntity
          failure [[400, 'Bad Request', Entities::ErrorEntity]]
          named 'create app'
        end
        post do
          app = ::App.create! params
          present app, with: Entities::AppsEntity
        end

      end

    end
  end
end

这是神奇地让它工作的代码:

config/initializers/grape_extensions.rb

class Evaluator
  def initialize(instance)
    @instance = instance
  end

  def params parameters
    evaluator = self
    @instance.normal_params do
      evaluator.list_parameters(parameters, self)
    end
  end

  def method_missing(name, *args, &block)
  end

  def list_parameters(parameters, grape)
    evaluator = self
    parameters.each do |name, description|
      description_filtered = description.reject { |k| [:required, :is_array].include?(k) }
      if description.present? && description[:required]
        if description[:type] < Grape::Entity
          grape.requires name, description_filtered.merge(type: Array) do
            evaluator.list_parameters description[:type].documentation, self
          end
        else
          grape.requires name, description_filtered
        end
      else
        if description[:type] < Grape::Entity
          grape.optional name, description_filtered.merge(type: Array) do
            evaluator.list_parameters description[:type].documentation, self
          end
        else
          grape.optional name, description_filtered
        end
      end
    end
  end
end

module GrapeExtension
  def desc name, options = {}, &block
    Evaluator.new(self).instance_eval &block if block
    super name, options do
      def params *args
      end

      instance_eval &block if block
    end
  end
end

class Grape::API
  class << self
    prepend GrapeExtension
  end
end

这是示例的结果:

Swagger result

关于ruby-on-rails - 葡萄 : required params with grape-entity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29517362/

相关文章:

ruby-on-rails - 获取用户正在关注 w/acts_as_follower 的帐户帖子

ruby-on-rails - 从类方法调用实例方法

ruby - 在 Vim (Ruby) 中用 do/end 替换匹配的 { braces }

ruby-on-rails - 使用 Rails API 和 JSON 创建用户?

ruby-on-rails - ruby on Rails MVC 框架项目中的 API 调用在哪里?

ruby-on-rails - 如何在 Ruby on rails 中上传大文件?

ruby-on-rails - "No resource or method named ` windows_package ' "使用 chef 安装简单的 windows 包时

ruby-on-rails - :disabled option for text_field in Rails 2. 3 的正确使用?

ruby - 哈希 has_key?句法

api - 如何使用 API 向 SalesForce 用户发送临时密码?