【发布时间】:2015-05-12 07:28:46
【问题描述】:
这里使用 Grape 编写 API 函数。我想要限制 API(API 速率限制)。
lib/grape/extensions/grape_extension.rb
module Grape
module Extension
module ThrottleExtension
def throttle(options={})
route_setting :throttle, options
options
end
Grape::API.extend self
end
end
end
lib/grape/middleware/throttle_middleware.rb
module Grape
module Middleware
class ThrottleMiddleware < Grape::Middleware::Base
def before
binding.pry
end
end
end
end
lib/grape_throttle.rb
require 'grape'
require 'grape/extensions/throttle_extension'
module Grape
module Middleware
autoload :ThrottleMiddleware, 'grape/middleware/grape_middleware'
end
end
最后,在config/application.rb
require File.expand_path('../../lib/grape_throttle', __FILE__)
config.middleware.use Grape::Middleware::ThrottleMiddleware
并且,当我使用rails s 运行并调用 api 时,binding.pry 已调用。
[1] pry(#<Grape::Middleware::ThrottleMiddleware>)> env['api.endpoint']
=> nil
我想知道如何在 Grape 中间件中访问 env['api.endpoint']?
【问题讨论】: