在Grape中如果使用错误!它将抛出一个错误,并且永远不会在"回调之后"调用Grape :: Endpoint.
我希望应用程序在出错时调用挂钩!被称为.
我添加了这块中间件来实现这一目标.
class AfterFailure < Grape::Middleware::Base
def call!(env)
@env = env
before
error = catch (:error) do
@app_response = @app.call(@env)
return after || @app_response
end
after_failure(error) || throw(:error, error)
end
def after_failure(error)
puts "After Failure"
nil
end
end
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来实现这一目标?
谢谢
我需要一些帮助才能让Grape :: API启动并运行Rails 4.我得到一个Unable to autoload constant Base即使a puts告诉我该类已加载.我究竟做错了什么?
应用程序/ API/api.rb
class API < Grape::API
prefix 'api'
format :json
default_format :json
mount V1::Base # Everything loads perfectly until I add this line.
end
Run Code Online (Sandbox Code Playgroud)
应用程序/ API/V1/base.rb
module V1
class Base < API
puts "=== DEBUG - in Base"
version 'v1', using: :path, vendor: 'orwapp', cascade: false
mount Users
end
end
Run Code Online (Sandbox Code Playgroud)
$ rspec spec/api
12:58:29 - INFO - Run all
12:58:29 - INFO - Running all specs
=== DEBUG …Run Code Online (Sandbox Code Playgroud) 目标:使用帮助器模块中的grape Shared Params,而不必helpers Helper::Module在每个已安装的API上添加语法。
有效的示例代码:
# /app/api/v1/helpers.rb
module V1
module Helpers
extend Grape::API::Helpers
params :requires_authentication_params do
requires :user_email, type: String
requires :authentication_token, type: String
end
end
end
# /app/api/api.rb
class API < Grape::API
format :json
version 'v1', using: :path
mount V1::A
mount V1::B
end
# /app/api/v1/a.rb
module V1
class A < Grape::API
helpers V1::Helpers
desc 'SampleA'
params do
use :requires_authentication_params
end
get 'sample_a/url' do
#...
end
end
end
# /app/api/v1/B.rb
module V1
class B < Grape::API
helpers V1::Helpers …Run Code Online (Sandbox Code Playgroud) 我使用Ember作为我的前端和Grape API来提供我的API.
前端发送类似的东西:
{
"service"=>{
"name"=>"Name",
"duration"=>"30",
"user"=>nil,
"organization"=>"org",
"category"=>nil,
"description"=>"description",
"disabled"=>true,
"color"=>nil,
"availabilities"=>[
{
"day"=>"Saturday",
"enabled"=>false,
"timeSlots"=>[
{
"startAt"=>"09:00 AM",
"endAt"=>"05:00 PM"
}
]
},
{
"day"=>"Sunday",
"enabled"=>false,
"timeSlots"=>[
{
"startAt"=>"09:00 AM",
"endAt"=>"05:00 PM"
}
]
},
{
"day"=>"Monday",
"enabled"=>true,
"timeSlots"=>[
{
"startAt"=>"09:00 AM",
"endAt"=>"05:00 PM"
},
{
"startAt"=>"05:00 AM",
"endAt"=>"09:00 PM"
},
{
"startAt"=>"05:00 AM",
"endAt"=>"09:00 PM"
}
]
},
{
"day"=>"Tuesday",
"enabled"=>true,
"timeSlots"=>[
{
"startAt"=>"09:00 AM",
"endAt"=>"05:00 PM"
}
]
},
{
"day"=>"Wednesday",
"enabled"=>true,
"timeSlots"=>[
{
"startAt"=>"09:00 …Run Code Online (Sandbox Code Playgroud) 我正在我的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) 我正在尝试使用GrapeAPI和构建用于登录的API Authlogic。
我的代码如下所示:
class Api
class V1
class UserSessionsController < Grape::API
post :login do
@user_session = UserSession.new(params[:user_session])
if @user_session.save
fetch_api_key(@user_session.user)
else
error!('Unauthorized.', 401)
end
end
helpers do
def current_user_session
return @current_user_session if defined?(@current_user_session)
@current_user_session = UserSession.find
end
def current_user
return @current_user if defined?(@current_user)
@current_user = current_user_session && current_user_session.user
end
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
问题是当我跑步时@user_session = UserSession.new(params[:user_session])我会得到You must activate the Authlogic::Session::Base.controller with a controller object before creating objects。
我尝试添加
Authlogic::Session::Base.controller = Authlogic::ControllerAdapters::RailsAdapter.new(self)
在尝试创建之前UserSession …
我正在使用rails 5和grape for api,我正在尝试为表格应用程序创建葡萄终点.
module PROJ::API::V2
class Applications < ::Grape::API
get '/' do
authorize! :read, Application
status 200
present Application.list(filter_params, current_user), with: Entities::ApplicationList, only_show: params[:only], person_base: true, current_ability: current_ability, user: current_user, nps_details: params[:nps_details]
end
end
end
Run Code Online (Sandbox Code Playgroud)
我正在获得未初始化的常量PROJ :: API :: V2 :: Applications :: Application.我尝试更改类名和文件名仍然发生相同的错误.
嗨,我正在使用intridea的葡萄开发一个简单的红宝石api.假设我们有这个:
class API_v1 < Grape::API
resource :foo do
end
resource :bar do
end
end
Run Code Online (Sandbox Code Playgroud)
我怎么能这样做,以便声明:foo和:bar单独的文件?基本上,我想知道是否有可能有类似于rails控制器的东西,其中有多个文件来组织代码.
我希望有人能给我一个如何实现这一目标的见解.
如何避免重复代码?
resource 'api/publication/:publicationName' do
params do
requires :type, type: String, regexp: /^(static|dynamic)$/i
requires :name, type: String, regexp: /^[a-z0-9_\s]+$/i
requires :liveStartDate, type: String, regexp: dateRegexp
optional :liveEndDate, type: String, regexp: dateRegexp
requires :query, type: String
end
post '/dynamic' do
authenticate!
save_or_update(params)
end
params do
requires :type, type: String, regexp: /^(static|dynamic)$/i
requires :name, type: String, regexp: /^[a-z0-9_\s]+$/i
requires :liveStartDate, type: String, regexp: dateRegexp
optional :liveEndDate, type: String, regexp: dateRegexp
requires :query, type: String
end
put '/dynamic/:id' do
authenticate!
save_or_update(params)
end
end
Run Code Online (Sandbox Code Playgroud) 有人可以解释之间的差异ActiveModel::Serializers和Grape.我应该使用其中一种还是可以/应该一起使用它们.有人还可以解释使用上述一个(或两个)与仅仅使用rails它自己构建一个的好处restful JSON API吗?
先感谢您