2010-08-12 145 views
0

嘿,我有一個客戶端將Rails 1.2.6站點與另一個以REST風格公開服務的站點集成在一起。升級到Rails 2.x目前不是一種選擇。有沒有人建議使用直接的Net :: HTTP調用與REST服務進行通信以外的方法?技術或Gem的建議是值得歡迎的,但我所看到的大多數寶石似乎都依賴於ActiveSupport 2.x,我知道它與Rails 1.x不兼容。Rails 1.x客戶端與REST風格的服務器聊天

預先感謝您提供的任何輸入。

回答

0

感謝Chris Heald的回覆。我最終使用了Net :: HTTP,因爲它比我認爲的最終更直接。 HTTParty看起來好像可以讓這件事變得更容易,但爲了未來這個問題的人的利益,這就是我所做的。

# Assume @user_name and @password were previously declared to be the 
# appropriate basic auth values and that the connection is open as @connection 
    def put(path, body, header={}) 
    request = Net::HTTP::Put.new(path, header.merge({'Accept' => 'application/xml,application/json', 'Content-type'=>'application/json'})) 
    request.basic_auth(@user_name, @password) 
    @connection.request(request, body).body 
    end 

    def post(path, body, header={}) 
    request = Net::HTTP::Post.new(path, header.merge({'Accept' => 'application/xml,application/json', 'Content-type'=>'application/json'})) 
    request.basic_auth(@user_name, @password) 
    @connection.request(request, body).body 
    end 

    def get(path, header={}) 
    request = Net::HTTP::Get.new(path) 
    request.basic_auth(@user_name, @password) 
    @connection.request(request).body 
    end 

我再呼籲這些方法的輸出JSON解析::(),並得到了一個哈希代表,我認爲合適的,我可以使用JSON。

+0

感謝您發佈您的解決方案。我遇到了同樣的問題,很高興能找到如此有用的提綱。 – cnk 2011-06-08 20:39:42

1

嘗試HTTParty。它對依賴關係非常敏感,並且使其非常輕鬆 - 可以輕鬆地將JSON或XML資源消耗添加到應用程序中。