2015-05-12 29 views
0

我需要得到2個不同的「客戶端」用於測試目的(用戶/代理電子錢包移動應用):如何實例化Ruby的RestClient?

  • 1用於用戶客戶
  • 1爲代理客戶端

兩者的這些客戶端應該用ouath頭文件簽名。 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 

回答

1

我會去準備的哈希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 

當然,檢查應該仔細做,可能取決於請求和參數。

+0

令牌是一個數組,因爲塊中使用了'| req_rex,token |',所以你的代碼不會出現問題。如果兩個應用程序共享相同的URL,則這不會起作用,即兩個客戶端的./login相同 – brbrr