标签: ruby-grape

如果字段不是nil,则葡萄实体有条件公开

在一个葡萄实体中,我想只显示一个字段(不是零?)没有运气.

我正在尝试使用此代码,但根本无法正常工作,但始终隐藏该字段.

expose :winner, :using => PlayerEntity, :unless => { :winner => nil }
Run Code Online (Sandbox Code Playgroud)

我认为代码本身解释了我真正需要的东西,但正如我所说,我没有得到预期的结果.

任何线索?

ruby ruby-on-rails ruby-grape

6
推荐指数
1
解决办法
3100
查看次数

JSON到ActiveRecord(反序列化)

我正在使用Ruby,Grape和ActiveRecord构建Web API.来自ASP.NET Web API我习惯于从JSON到类对象的自动模型绑定,然后可以使用Entity Framework保存.我一直在寻找一下,看看在使用ActiveRecord时是否有类似的东西,但没有找到任何让我觉得我错过了一些非常明显的东西.将JSON反序列化为ActiveRecord模型的最佳方法是什么?

UPDATE

Matts答案适用于简单类型但是当我有关联时我得到以下错误:ActiveRecord :: AssociationTypeMismatch:连接器(#70297711668780)预期,得到哈希(#70297701477140)

这是我正在运行的代码

class Card < ActiveRecord::Base
   has_many    :connectors, autosave: true
end

class Connector < ActiveRecord::Base
   has_one  :card
   has_one  :connected_card, :class_name => "Card", :foreign_key => "connected_card_id"
end

json = "{\"id\":\"1\", \"connectors\": [{\"id\": 1, \"card_id\":1}]}"
card = Card.new(ActiveSupport::JSON.decode(json))
Run Code Online (Sandbox Code Playgroud)

ruby activerecord json ruby-on-rails ruby-grape

6
推荐指数
2
解决办法
4577
查看次数

如何将 ActiveSupport::Concern 应用于控制器中的特定操作?

我正在使用 Grape 构建 API。

我创建了一个ActiveSupport::Concern让我们说的名字Authentication,我在过滤器之前应用了一些,所以我的担忧看起来像:

module Authentication
  extend ActiveSupport::Concern

  included do
    before do
      error!('401 Unauthorized', 401) unless authenticated?
    end
    ....
  end
end
Run Code Online (Sandbox Code Playgroud)

现在让我们说在我的 UserController 中,我只想将这个问题应用于特定的操作。我怎样才能做到这一点?

  class SocialMessagesController < Grape::API
    include Authentication

    get '/action_one' do
    end

    get '/action_two' do
    end

  end
Run Code Online (Sandbox Code Playgroud)

任何简单的方法来指定特定方法的关注点,就像before_filter在带有only选项的rails 中一样?

ruby ruby-grape activesupport-concern

6
推荐指数
1
解决办法
310
查看次数

Groovy 1.8 @Grab失败,除非我手动调用葡萄决心

当运行使用葡萄系统的脚本来获取ibiblio repo中的依赖关系时,它会失败直到我grape resolve从命令行手动调用.之后,它在本地缓存中,脚本运行正常.

是否有一些其他注释需要用来让它第一次从脚本中运行?告诉用户首先"葡萄解决",然后@Grab工作,感觉很奇怪.

这是脚本,抓住了redis的jedis jar:

#!/usr/bin/env groovy
@Grab('redis.clients:jedis:2.0.0')

import redis.clients.jedis.*

Jedis redis = new Jedis("localhost")
Run Code Online (Sandbox Code Playgroud)

如果我有一个干净的〜/ .groovy/grape缓存,则会因此异常而失败:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
General error during conversion: Error grabbing Grapes -- [unresolved dependency: redis.clients#jedis;2.0.0: ibiblio: unable to get resource for redis/clients#jedis;2.0.0: res=/redis/clients/jedis/2.0.0/jedis-2.0.0.pom: java.net.MalformedURLException: no protocol:  /redis/clients/jedis/2.0.0/jedis-2.0.0.pom]
Run Code Online (Sandbox Code Playgroud)

它只在我grape resolve从命令行手动执行后运行:

grape -V resolve redis.clients jedis 2.0.0
Run Code Online (Sandbox Code Playgroud)

(部分输出显示它是从ibiblio下载的):

...
ibiblio: found md file for redis.clients#jedis;2.0.0
    => http://repo1.maven.org/maven2/redis/clients/jedis/2.0.0/jedis-2.0.0.pom (2.0.0)
downloading http://repo1.maven.org/maven2/redis/clients/jedis/2.0.0/jedis-2.0.0.pom ...
    ibiblio: downloading http://repo1.maven.org/maven2/redis/clients/jedis/2.0.0/jedis-2.0.0.pom
    ibiblio: downloading http://repo1.maven.org/maven2/redis/clients/jedis/2.0.0/jedis-2.0.0.pom.sha1
sha1 OK …
Run Code Online (Sandbox Code Playgroud)

groovy ruby-grape ivy maven

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

继承在葡萄中不起作用

我正在使用葡萄 redtful-api。我无法继承 Grape 中的 common_params 。我在类 API1 中定义了 common _params 并在 API2 中调用它会引发错误。我如何更改代码才能使其正常工作?

module Example
  class API1 < Grape::API
    version 'v1'
    format :json
    prefix :api

    resource :exc1 do
      common_params = proc do
        requires :param1
        requires :param2
      end

      params(&common_params)
      get :params_by_pair do
        p1 = params[:param1]
        p2 = params[:param2]
        response = "https://www.example1.com/#{p1}_#{p2}"
      end
    end
  end
end

module Example
  class API2 < API1
    version 'v1', using: :header, vendor: 'twitter'
    format :json
    prefix :api

    resource :exc2 do

      params(&common_params)
      get :params_by_pair do
        p1 = params[:param1] …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails ruby-grape grape-api

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

从返回的葡萄 api 字符串中删除引号

我想从我的葡萄/休息 api 返回原始数据/blob。

我关注了以下线程:https : //github.com/intridea/grape/issues/412

对于像这样的代码:

get 'foo' do
  content_type 'text/plain'
  "hello world"
end
Run Code Online (Sandbox Code Playgroud)

1)我使用:格式'txt' - 我得到了引用文本,如:“hello world”在浏览器上没有错误,curl给出了Content-Type:text/plain但引号没有被删除

2) env['api.format'] = :txt 在浏览器中给出错误

3) content_type :txt, 'text/plain' 在浏览器中给出错误数量的 args

还有其他方法可以解决这个问题吗?

谢谢。

html ruby thin ruby-grape

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

如何将 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
查看次数

如何将authenticate_or_request_with_http_token与Grape一起使用?

我如何在grape API中实现authenticate_or_request_with_http_token。下面是我的代码:

module Articles 
    class ArticleData < Grape::API
        include ActionController::HttpAuthentication::Token::ControllerMethods
     #   http_basic do |email, password|
        #   user = User.find_by_email(email)
        #   user && user.valid_password?(password)
        # end

        before do
            error!("401 Unauthorized", 401) unless authenticated
        end

        helpers do
            def authenticated
                 authenticate_or_request_with_http_token do |token, options|
                    apiKey = ApiKey.where(auth_token: token).first
                     #ApiKey.exists?(access_token: token)
                 end
            end
        end

        resource :article_data do

            desc "Return all article data"
            get do
                Article.all
            end

            desc "create a new article"
            ## This takes care of parameter validation
            params do
                requires :title, type: String …
Run Code Online (Sandbox Code Playgroud)

ruby curl ruby-on-rails ruby-grape grape-api

5
推荐指数
0
解决办法
637
查看次数

子域时不提供Swagger文档

使用grape-swagger宝石,我在Grape Swagger中设置了一个子域,例如routes.rb

constraints subdomain: /api/ do
  mount Api::V1::Base => '/'
end
Run Code Online (Sandbox Code Playgroud)

这是document.rb

require 'grape-swagger'

module API
  class Root < Grape::API
    mount API::Cats

    add_swagger_documentation
  end
end
Run Code Online (Sandbox Code Playgroud)

但是,当我转到时http://api.localhost:3000/swagger_doc,出现路由错误

没有路线匹配[GET]“ / swagger_doc”

如果删除约束,则可以访问http://localhost:3000/swagger_doc

我试图添加一个base_path参数,add_swagger_documentation但无济于事。

关于我可能做错了什么的任何提示?

ruby-on-rails ruby-grape swagger

5
推荐指数
0
解决办法
263
查看次数

如何处理Grape中特定操作的过滤器之前?

我正在我的Rails项目中安装Grape来构建RESTful API.

现在一些端点有需要身份验证的操作和其他不需要身份验证的端点.

例如,我有users一个看起来像这样的终点:

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

      before { authenticate! }

      resource :users do

        desc "Return a user"
        params do
          requires :id, type: Integer, desc: 'User id'
        end
        get ':id' do
          UsersService::Fetch.new(current_user,params).call
        end

        desc "Update a user"
        params do
          requires :id, type: Integer, desc: 'User id'
          requires :display_name, type: String, desc: 'Display name'
          requires :email, type: String, desc: 'Email'
        end
        post ':id' do
          UsersService::Save.new(current_user,params).call
        end

        desc "Reset user password"
        params do …
Run Code Online (Sandbox Code Playgroud)

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

5
推荐指数
2
解决办法
2042
查看次数