使用ActiveResource在Rails中使用非REST API

我正在编写一个使用非REST API的客户端(即GET site.com/gettreasurehunts),这要求我将请求的HTTP主体中的所有参数(甚至资源ID)指定为自定义XML文档。 我想使用Rails和ActiveResource,但我不得不重写几乎所有的ActiveResource方法。

是否有另一种更精致的方法来实现相同的结果,甚至使用另一个(Ruby)框架?

我不认为有一种方法可以使用ActiveResource,对于这些情况我只使用Net :: HTTP和Nokogiri

我会推荐HTTParty ,它非常灵活,我确信能够处理你需要的东西。

该项目的一些例子:

pp HTTParty.get('http://whoismyrepresentative.com/whoismyrep.php?zip=46544') pp HTTParty.get('http://whoismyrepresentative.com/whoismyrep.php', :query => {:zip => 46544}) @auth = {:username => u, :password => p} options = { :query => {:status => text}, :basic_auth => @auth } HTTParty.post('http://www.twitter.com/statuses/update.json', options) 

如果您需要在请求正文中发布一些内容,只需在选项哈希中添加:body =>“text”即可。

使用起来非常简单,我目前正在使用它代替ActiveResource来从Rails应用程序中使用一些REST服务。

简单的回答,不要。 我有一个与ActiveResource类似的问题,不喜欢HTTParty的api(太多的类方法),所以我推出了自己的。 尝试一下,它叫做Wrest 。 它通过REXML,LibXML,Nokogiri和JDom开箱即用,对Curl和反序列化提供部分支持。 您也可以轻松编写自己的反序列化器。

这是Delicious api的一个例子:

 class Delicious def initialize(options) @uri = "https://api.del.icio.us/v1/posts".to_uri(options) end def bookmarks(parameters = {}) @uri['/get'].get(parameters) end def recent(parameters = {}) @uri['/recent'].get(parameters) end def bookmark(parameters) @uri['/add'].post_form(parameters) end def delete(parameters) @uri['/delete'].delete(parameters) end end account = Delicious.new :username => 'kaiwren', :password => 'fupupp1es' account.bookmark( :url => 'http://blog.sidu.in/search/label/ruby', :description => 'The Ruby related posts on my blog!', :extended => "All posts tagged with 'ruby'", :tags => 'ruby hacking' )