【问题标题】:Count api calls with token使用令牌计算 api 调用
【发布时间】:2014-02-19 09:26:50
【问题描述】:

我正在尝试创建一个需要令牌才能使用的 api。每次调用页面时,我都会在控制器中使用此 before_filter 检查令牌是否存在:

            def restrict_access_token
                authenticate_or_request_with_http_token do |token, options|
                    ApiKey.exists?(access_token: token)
                end
            end

找到 apikey 后,我想增加 ApiKey 表中的计数列。

我正在寻找模型中的回调,该回调在找到 apikey 时触发,但在这种情况下似乎不起作用。

有什么想法吗?

【问题讨论】:

    标签: ruby-on-rails api authentication token


    【解决方案1】:

    在你的模型中,你需要重写exists?方法

    def self.exists?(conditions = :none)
      if super
        ...{YOUR CALLBACK}...
      end
    end
    

    更新: 我更喜欢自定义您的方法,例如find_and_increment(...)。所以在模型中:

    def self.find_and_increment(conditions = :none)
      if object = where(conditions).first
        object.increment!(...)
      end
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-18
      • 2018-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-13
      • 1970-01-01
      • 2021-12-26
      相关资源
      最近更新 更多