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.
Thanks to francxk, you can now use proxies.
class MyProxyParty include HTTParty http_proxy 'proxyaddress.com', 80 end
http_proxy method, you can set the address and the port of the proxy you would like to use. I’ve never needed a proxy personally but I think this was a worth addition.
Format Detection Based on Content Type
When HTTParty was released, you had to declare a format if you wanted the response to be parsed in some way. For example, the old way of parsing the Twitter public timeline was the following:
class Twitter include HTTParty </code><strong><code class="ruby">format :json</code></strong><code class="ruby"> end Twitter.get('http://twitter.com/statuses/public_timeline.json')
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:
class RedirectFollower include HTTParty base_uri 'http://someurlthatredirects.com' end RedirectFollower.get('/foo', :limit => 2)
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.
class Twitter include HTTParty end puts Twitter.get('http://twitter.com/statuses/public_timeline.json', :format => :html)
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.
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:
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.
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.