Using REST APIs without REST in Rails with ActiveResource

I am writing a client that uses the REST API (i.e. GET site.com/gettreasurehunts), which requires me to specify all parameters (even the resource identifier) ​​in the HTTP request tag as a custom XML document. I would like to use Rails and ActiveResource, but I would have to rewrite almost all the ActiveResource methods.

Is there another, more polished way to achieve the same result, even using a different (Ruby) framework?

+4
source share
3 answers

I don’t think there is a way to do this with ActiveResource, for these cases I just use Net :: HTTP and Nokogiri

+3
source

I would recommend HTTParty , it is quite flexible, and I'm sure I am able to handle what you need.

Some examples from the project:

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) 

And if you need POST something in the request body, just add: body => "text" to the parameter hash.

This is very easy to work with, and now I use it instead of ActiveResource to use some of the REST services from the Rails application.

+3
source

The simple answer is, no. I had a similar problem with ActiveResource, I didn't like the HTTParty api (too many class methods), so I made my own. Try it, it's called Wrest . It has partial support for Curl and deserialization through REXML, LibXML, Nokogiri, and JDom out of the box. You can trivially write your own deserializer.

Here is an example for the 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' ) 
+1
source

All Articles