December 07, 2008

HTTParty Divorces ActiveSupport

Right off the bat I would like to say that the divorce was completely amicable. I love ActiveSupport and use it with almost every project, even when haters complain. Originally, HTTParty’s parsing of JSON and XML just used ActiveSupport’s built in mechanisms and it worked great.

A few people brought up issues with ActiveSupport and I rolled my eyes. Then, once the roll was complete I thought about it and decided if I could use the JSON gem and find something comparable to ActiveSupport’s Hash#from_xml, I would be ok with removing it. I did some research and stumbled across extlib’s Hash#from_xml and liked it, so I ganked to power the XML de-serialization in HTTParty 0.2.0.

I removed ActiveSupport as a dependency and added the JSON gem. I then added a few core extensions, swapped out the de-serialization and HTTParty was free to date again.

But Wait, There Is More

So that update alone would probably be worth a blog post, but it is not the only thing that has changed since I first posted about HTTParty. Since it was suggested, I’m going to show a few of the updates here as well.

With the response content type detection, the new way is one less line of code and one less thing to remember.

class Twitter
include HTTParty
end
Twitter.get('http://twitter.com/statuses/public_timeline.json')

Much thanks to Jay Pignata for adding this initially.

Automatically Follow Redirects

Alex Vollmer snuck into HTTParty like a thief in the night and added automatic following of redirects. With his addition HTTParty automatically follows up to 5 redirects, after which it will raise an HTTParty::RedirectionTooDeep exception. Feel free to bump up or down the redirect limit like this:

HTML format

A few people mentioned that they would like a way to not automatically parse the response based on the content type. The simple fix for that was allowing an html format and making sure that format can optionally be passed in with an request.

Even though the request above returns the JSON content type, it will not be parsed as JSON, but instead will just return the JSON as plain text/html.

HTTParty.get/post/put/delete

One of my favorite additions was by Eli Miller, just a few days ago. We were chatting and he mentioned how he liked using HTTParty as a simple net/http wrapper. A few minutes later he sent me a pull request with a nifty addition that allows using HTTParty as a sane wrapper around net/http, without even making a new class and including HTTParty. You can now turn the Twitter public timeline into a ruby hash in one line thanks to Eli’s addition:

HTTParty.get('http://twitter.com/statuses/public_timeline.json')

Yep, that is it. It will fetch the response, detect that it is JSON and use JSON.parse to turn the response into a Ruby hash. It really doesn’t get any easier.

Conclusion

Ok, so now all you hold outs who haven’t used HTTParty yet have absolutely no reason. Use it with Rails, Merb, Sinatra or whatever the heck other framework you use, with no worries about including all of ActiveSupport.

11 Comments

HTTParty is The Truth. Thanks very much to everyone that has been working on it. I’m using it for a couple of projects that hook into different external APIs, and it’s lovely to have one library that works with so many services. Thank you!

I am glad you did this. ActiveSupports Hash.from_xml has proven to be buggy for me, especially when I was working with the Flickr API. It would wipe out attributes/values if they had the same name (Flickr’s API for tags as an example)