我的Rails 3应用程序正在使用Grape API.我知道如何在选定的路由上安装该API,mount API => '/api'但我需要在子域上访问该API api.mydomain.com.
我搜索了Grape&Sinatra文档,有关堆栈溢出的问题,并试图谷歌它,但我找不到任何解决方案.
如何配置Grape以在默认值以外的位置查找自定义配置文件~/.groovy/grapeConfig.xml?不幸的是,http: //groovy.codehaus.org/Grape上的官方文档似乎不完整,并未涵盖此特定情况.
例如,我想指定一个路径,$PROJECT_DIR/src/main/resources/groovyConfig.xml以便我的团队中的其他成员不必groovyConfig.xml在其user/home目录中创建和维护自己的文件.
我正在Grape中编写一个API,但它是独立的,没有Rails或Sinatra或任何东西.我想将app.rb文件拆分为单独的文件.我看过如何在葡萄api app中拆分东西?,但这与Rails有关.
我不知道如何使用模块或类来完成这项工作 - 我确实尝试将不同的文件子类化为我的大文件GrapeApp,但这很难看,我甚至不确定它是否正常工作.最好的方法是什么?
我现在有通过文件夹(拆分版本v1,v2等),但仅此而已.
如您所知,您可以指定路由中需要参数,如下所示:
requires :province, :type => String
但是,我希望能够更改引发的错误,并在未给出参数时提供我自己的错误JSON.
我怎么能这么容易做到?猴子补丁我很好.
编辑:我在第191行看到rescue_from,看起来它可能会有所帮助,但我不知道如何使用它.
https://codeclimate.com/github/intridea/grape/Grape::API
我使用curl手动测试我的rails应用程序葡萄API.我想发送一个[1,2,3]数组作为请求查询参数,以便可访问,如:
p params[:tickets_ids]
=> [1,2,3]
Run Code Online (Sandbox Code Playgroud)
我只发现了如何发送被解释为哈希的东西的线索.
我将非常感谢如何使用curl发布数组.
我正在使用葡萄创建的API获得JSON格式的用户输入.在一个特定的参数中,我给出了一个JSON数组,格式如下.
"personal" : {
"details" : {
"firstname" :"nagalakshmi",
"lastname" : "n"
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试打印"个人"属性时,它显示如下
#<Hashie::Mash details=#<Hashie::Mash firstname="nagalakshmi" lastname="n">>
Run Code Online (Sandbox Code Playgroud)
有没有办法将属性解析为json格式?
你如何在葡萄中获得葡萄多路线参数?
我可以让这条路线起作用:
.../api/company/:cid
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试这个:
.../api/company/:cid/members
.../api/company/:cid/members/:mid
Run Code Online (Sandbox Code Playgroud)
我收到错误.
这是有效的代码.
resource 'company' do
params do
optional :start_date, type: Date, desc: "Start date of range."
optional :end_date, type: Date, desc: "End date of range."
end
route_param :cid do
get do
{company_id: params[:cid]}
end
end
Run Code Online (Sandbox Code Playgroud) 如何使用葡萄实体将参数传递给模型方法?
我想在显示项目时检查current_user是否喜欢某个项目,所以我构建了一个模型user_likes?方法:
class Item
include Mongoid::Document
#some attributes here...
has_and_belongs_to_many :likers
def user_likes?(user)
likers.include?(user)
end
end
Run Code Online (Sandbox Code Playgroud)
但我不知道如何将current_user发送到葡萄实体模型:
module FancyApp
module Entities
class Item < Grape::Entity
expose :name #easy
expose :user_likes # <= How can I send an argument to this guy ?
end
end
end
Run Code Online (Sandbox Code Playgroud)
在葡萄api:
get :id do
item = Item.find(.....)
present item, with: FancyApp::Entities::Item # I should probably send current_user here, but how ?
end
Run Code Online (Sandbox Code Playgroud)
我觉得current_user可能应该从最后一段代码发送,但我无法想象如何做到:(
有什么想法吗 ?谢谢 !
这是我的API的样子:
module ServiceRequests
class API < Grape::API
version 'v1', using: :path
format :json
namespace :companies do
params do
requires :company_id, type: Integer, desc: "A Company Id"
end
resource :service_requests do
get :all do
Company.find(params[:company_id]).service_requests
end
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
我试图像这样打电话:http://localhost/v1/companies/:company_id/service_requests/all但是当我打这个电话时,我收到了错误No route matches [GET] "/v1/companies/1/service_requests/all"
工作守则:
module ServiceRequests
class API < Grape::API
version 'v1', using: :path
format :json
resource :companies do
params do
requires :company_id, type: Integer, desc: "A Company Id"
end
route_param :company_id do
resource :service_requests do …Run Code Online (Sandbox Code Playgroud) 我一直试图让一个简单的Ember.js应用程序现在发布到Grape API后端几个小时,但我似乎无法让它工作.我知道API有效,因为我可以通过Swagger文档向它发布新记录,并且它们是持久的.我知道API和Ember说得很好,因为我可以从服务器获取所有记录并在页面上与它们进行交互,我知道Ember在真空中工作正常,因为我的记录持久存储到本地存储.
但是,我似乎无法获得POST请求.它总是回来400.我已经正确配置了Rack-Cors,我在前端设置了ActiveModelAdapter,后端设置了ActiveModelSerializer.
这是模型
Contact = DS.Model.extend {
firstName: DS.attr('string'),
lastName: DS.attr('string'),
email: DS.attr('string'),
title: DS.attr('string'),
createdAt: DS.attr('date'),
updatedAt: DS.attr('date')
}
Run Code Online (Sandbox Code Playgroud)
和控制器
ContactsNewController = Ember.ObjectController.extend(
actions:
save: ->
@get('model').save()
cancel: ->
true
)
Run Code Online (Sandbox Code Playgroud)
API的相关部分如下所示
desc 'Post a contact'
params do
requires :first_name, type: String, desc: 'First name of contact'
requires :last_name , type: String, desc: 'Last name of the contact'
requires :email , type: String, desc: 'Email of the contact'
requires :title , type: String, desc: 'Title of the contact' …Run Code Online (Sandbox Code Playgroud)