如何实例化 Ruby 的 RestClient?

How can I instantiate Ruby's RestClient?

我需要获取 2 个不同的 'clients' 用于测试(user/agent e-wallet 移动应用程序):

这两个客户端都应使用 ouath headers 进行签名。 RestClient 为此目的提供 #before_execution_proc,但这不适用于 2 个不同的会话。我已经尝试用这个 pull request 来解决它,但这对于多资源来说是丑陋的方式(因为我应该 运行 请求每个新资源的代码):

def resource(url)
  rest = RestClient::Resource.new(@base_url+ url)
  rest.add_before_execution_proc do |req, params|
    @access_token.sign! req, {"Cookie" => @cookies}
  end
  rest
end

我会准备哈希 url_regexp ⇒ access_token 和通用 add_before_execution_proc:

 @access_tokens = { /google/ => ..., /msoft/ => ... }
 RestClient.add_before_execution_proc do |req, params|
   token = @access_tokens.detect { |req_rex, token| req_rex =~ req }.last
   token.sign!(req) unless token.nil?
 end

当然要仔细检查,可能取决于请求和参数。