The HTTParty has just begun
4 comments Latest by Trevor Turk Thu, 04 Dec 2008 06:21:25 GMT
After releasing the new RubyURL API, I decided that it was time to look around at libraries to interact with it. I came across a new Ruby gem from John Nunemaker named, HTTParty, which aims to make it easy to talk to XML and JSON-based web services. Be sure to read John’s announcement of HTTParty.
So, I decided it might be fun to introduce more people to the gem by showing you all how to use it to talk to the new RubyURL API.
Install HTTParty
Before we get started, you’ll need to install the HTTParty gem with the following command:
~ : sudo gem install httparty
Password:
When you HTTParty, you must party hard!
Successfully installed httparty-0.1.6
1 gem installed
Installing ri documentation for httparty-0.1.6...
Installing RDoc documentation for httparty-0.1.6...
Great! Now that we’re ready to party hard, let’s build something.
Talking to the RubyURL API
The RubyURL API currently supports both XML and JSON, which are each supported by HTTParty. The great thing about HTTParty is that all you need to do is include it in a class and you’re able to quickly talk to remote services.
In this following example, we’re going to create a new class called Rubyurl.
class Rubyurl
endWhat we’ll want to do now is include the HTTParty library. (note: you’ll need to require both rubygems and httparty gems and I’ll skip those lines in following code samples)
class Rubyurl
include HTTParty
endThe HTTParty provides a few class methods, which we can use to configure our library. We’ll go ahead and specify the base_uri, which we’ll set to rubyurl.com.
class Rubyurl
include HTTParty
base_uri 'rubyurl.com'
endNow that our class is setup to talk to the Rubyurl.com site, we’ll want to add a new method which we can use to communicate with the RubyURL API. We’ll call this shorten as we’re using RubyURL to shorten long URLs… right?
class Rubyurl
include HTTParty
base_uri 'localhost:3000'
def self.shorten( website_url )
end
endOur new shorten method will expect us to provide it with a website url, which we’ll want RubyURL to return a shortened URL for. The PATH for the API that we’ll want to talk to is: /api/links, which we’re expected to pass XML or JSON to.
Here are two examples of using the RubyURL API with HTTParty.
RubyURL via JSON w/HTTParty
We’re going to use the post method that is provided with HTTParty to send a request to /api/links.json. As you can see, we’re providing the original website url to the web service.
class Rubyurl
include HTTParty
base_uri 'rubyurl.com'
def self.shorten( website_url )
post( '/api/links.json', :query => { :link => { :website_url => website_url } } )
end
endWhen ran, it’ll produce the following:
>> Rubyurl.shorten( 'http://github.com/jnunemaker/httparty/tree/master/lib/httparty.rb' ).inspect
=> {"link"=>{"permalink"=>"http://rubyurl.com/uJVu", "website_url"=>"http://github.com/jnunemaker/httparty/tree/master/lib/httparty.rb"}}Pretty simple, eh?
RubyURL via XML w/HTTParty
The great thing about HTTParty is that you can use XML without changing much.
class Rubyurl
include HTTParty
base_uri 'rubyurl.com'
def self.shorten( website_url )
post( '/api/links.xml', :query => { :link => { :website_url => website_url } } )
end
endProduces the following
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<link>
<website_url>http://github.com/jnunemaker/httparty/tree/master/lib/httparty.rb</website_url>
<permalink>http://rubyurl.com/uJVu</permalink>
</link>Closing thoughts
So… there you have it. HTTParty makes it extremely easy to interact with various web services that work over HTTP. I’d encourage you all to take a few minutes to experiment with it and see what crazy ideas that come to mind during the process. :-)
Enjoying the content? Be sure to subscribe to my RSS feed.




wow, that looks really simple. perhaps i’ll build my own twitter client so that i can play with it
@jason That’s what I’m doing. I’m building a twitter client that searches my girlfriend’s university library catalogue. You might find jnunemaker’s HappyMapper gem useful (it contains an example for mapping twitter responses straight to objects).
http://github.com/jnunemaker/happymapper/tree/master
The hardest part is parsing the search results. The template hasn’t been touched since about 1996.
For github-party (using JSON) I did something like:
Basically, it’s making an OpenStruct from the JSON hash, and then I’m pulling off the toplevel object. In you’re example, I figure you probably just want to pull off permalink.
Perhaps you will enjoy my pet project, h8ter, which uses HTTParty to spread hatred all over the world.