【问题标题】:use prepend to wrap grape api method使用 prepend 包装葡萄 api 方法
【发布时间】:2014-09-29 03:25:43
【问题描述】:

我使用grape来构建我的api网站,我想在我的网站中添加自动使用日志功能,所以我在我的Grape::API类中使用了prepend,我就是这样做的:

module GrapeExtension
 def get(paths = ['/'], options = {}, &block)
   #add log code here
   super
 end
end

并在我使用 Grape::API 的地方添加代码,例如

class API < Grape::API
    prepend GrapeExtension
    #other code
    get '/info' do
     #function code
    end
end

但是当我请求 /info api 时,似乎我在 GrapeExtension 中的代码没有被调用,为什么?

【问题讨论】:

    标签: ruby prepend grape


    【解决方案1】:

    尝试将paths=['/'] 替换为通用路径处理程序,如下所示:

    module GrapeExtension
     def get(paths = ['/*'], options = {}, &block)
       #add log code here
       super
     end
    end
    

    【讨论】:

    • 它似乎不起作用,我发现即使是 Grape::API 中的 get 方法在网站初始化时也只会被调用一次,所以我怀疑在这里添加自动使用日志的正确位置吗@Ivan Zarea
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-06
    • 1970-01-01
    • 2016-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多