Read my latest article: Announcing RailsDeveloper (posted Wed, 01 Sep 2010 17:01:00 GMT)

The HTTParty has just begun

Posted by Robby Russell Thu, 27 Nov 2008 01:54:00 GMT

67 comments Latest by vector conversion Tue, 31 Aug 2010 15:14:01 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
end

What 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
end

The 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'
end

Now 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
end

Our 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
end

When 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
end

Produces 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. :-)

Subscribe to my RSS feed Enjoying the content? Be sure to subscribe to my RSS feed.
Comments

Leave a response

  1. Avatar
    jason Thu, 27 Nov 2008 02:19:18 GMT

    wow, that looks really simple. perhaps i’ll build my own twitter client so that i can play with it

  2. Avatar
    Max Thu, 27 Nov 2008 10:21:02 GMT

    @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.

  3. Avatar
    Josh Nichols Thu, 27 Nov 2008 16:23:01 GMT

    For github-party (using JSON) I did something like:

      def self.user(user)
        get("/#{user}").to_struct.user
      end

    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.

  4. Avatar
    Trevor Turk Thu, 04 Dec 2008 06:21:25 GMT

    Perhaps you will enjoy my pet project, h8ter, which uses HTTParty to spread hatred all over the world.

  5. Avatar
    Free Web Hosting Tue, 04 May 2010 06:15:52 GMT

    Great stuff. I’m going to have to read it again to make sure I got it all.

  6. Avatar
    wholesale laptop adapter Thu, 13 May 2010 06:34:06 GMT

    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.

  7. Avatar
    okey oyna Thu, 20 May 2010 19:05:10 GMT

    Thanks man …............

  8. Avatar
    supra tk society black white Mon, 24 May 2010 05:38:46 GMT

    supra tk society black white

  9. Avatar
    air jordan 20 Mon, 24 May 2010 05:39:00 GMT

    rsgdrgdr

  10. Avatar
    testinside 1K0-002 Fri, 28 May 2010 05:48:44 GMT

    I absitively it ability be fun to acquaint added humans to the gem by assuming you all how to use it to allocution to the new RubyURL API.

  11. Avatar
    testinside SK0-002 Fri, 28 May 2010 05:49:20 GMT

    The RubyURL API currently supports both XML and JSON, which are anniversary accurate by HTTParty. The abundant affair about HTTParty is that all you charge to do is cover it in a chic and you’re able to bound allocution to limited services.

  12. Avatar
    testinside 1z0-007 Fri, 28 May 2010 05:49:56 GMT

    What we’ll wish to do now is cover the HTTParty library. (note: you’ll charge to crave both rubygems and httparty gems and I’ll skip those curve in afterward cipher samples)

  13. Avatar
    air jordan 23 premier Thu, 03 Jun 2010 05:49:34 GMT

    try

  14. Avatar
    air jordan 5 raging bull Thu, 03 Jun 2010 05:49:46 GMT

    try

  15. Avatar
    designer handbags reviews Fri, 11 Jun 2010 11:56:38 GMT

    Great stuff. I’m going to have to read it again to make sure I got it all.

  16. yuty

  17. Avatar
    supra chad muska skytop purple camo Thu, 17 Jun 2010 00:09:39 GMT

    supra chad muska skytop purple camo

  18. Avatar
    coach handbags Mon, 21 Jun 2010 04:33:48 GMT

    it’s the first time to be here.Very happy to come here.

  19. Avatar
    lv Sat, 26 Jun 2010 01:02:10 GMT

    Photographers got a attempt of a being allurement with one louis handbag , this looked rather antic as anybody knows these louis handbags are actual expensive. vuitton bag is your best choice.

  20. Avatar
    tiffany jewellery Mon, 28 Jun 2010 00:40:05 GMT

    There is a great deal of the delicate metalwork that is so indicative of the Tiffany designs. The Tiffany rings has a shade which is a gorgeous confection of stained glass that has been meticulously soldered together, and you will find that your silver rings can bring a great deal of life, you should consider what you can do with a Tiffany earrings

    Go to a renowned retail store of Bentley GT and shop to away for your new Breitling Aeromarine . Breitling Windrider has always been known for its ultimate quality and standard in manufacturing superb designs of watches. Bentley 6.75 is a type of watch which has been designed to suit the requirements of pilots.

    .

  21. Avatar
    nnnnnnnnnnnnnnn Mon, 28 Jun 2010 03:26:32 GMT
  22. Avatar
    wholesale dresses Tue, 29 Jun 2010 07:49:22 GMT
  23. Avatar
    air jordan 11 Wed, 30 Jun 2010 09:10:41 GMT

    Demonstrate a unique new conceptjordan shoesAwA10*

  24. Avatar
    New Style Christian Louboutin Thu, 01 Jul 2010 08:32:40 GMT
  25. Avatar
    air jordan shoes sale Thu, 01 Jul 2010 08:32:48 GMT
  26. Avatar
    Jimmy Choo Thu, 01 Jul 2010 08:32:57 GMT
  27. Avatar
    Christian Louboutin Boots Thu, 01 Jul 2010 08:33:08 GMT
  28. Avatar
    Wholesale Polo Shirts Thu, 01 Jul 2010 08:33:20 GMT
  29. Avatar
    St Louis Rams jerseys Thu, 01 Jul 2010 08:33:31 GMT
  30. Avatar
    UGG Classic Short Boots Thu, 01 Jul 2010 08:33:43 GMT
  31. Avatar
    adf Fri, 02 Jul 2010 07:40:29 GMT
  32. Avatar
    fashionfans Tue, 13 Jul 2010 08:51:37 GMT

    nice post, thank you for your sharing.

  33. Avatar
    designerhandbags Tue, 13 Jul 2010 08:54:47 GMT

    nice post,thank you for your sharing.

  34. Avatar
    Проститутки Fri, 16 Jul 2010 16:52:38 GMT

    Great stuff. I’m going to have to read it again to make sure I got it all.

  35. Avatar
    wedding dresses Sat, 17 Jul 2010 16:43:02 GMT

    Just want to buy something like wedding dress or prom dress

  36. Avatar
    prom dresses Sat, 17 Jul 2010 16:44:03 GMT

    it’s the first time to be here.Very happy to come here.

  37. Avatar
    Louis Vuitton Outlet Sun, 18 Jul 2010 12:41:31 GMT

    Louis Vuitton BOWLING MONTAIGNE PM M5932K Louis Vuitton BOWLING MONTAIGNE Louis Vuitton BOWLING MONTAIGNE GM M59312 Louis Vuitton BOWLING MONTAIGNE Louis Vuitton BOWLING MONTAIGNE GM M5931J Louis Vuitton BOWLING MONTAIGNE Louis Vuitton Epi Leather BOWLING MONTAIGNE GM M5931K Louis Vuitton Epi Leather BOWLING MONTAIGNE Louis Vuitton BOWLING MONTAIGNE GM M5931M Louis Vuitton BOWLING MONTAIGNE Louis Vuitton Epi Leather PASSY M5926M Louis Vuitton Epi Leather PASSY Louis Vuitton Epi Leather PASSY M59262 Louis Vuitton Epi Leather PASSY Louis Vuitton Epi Leather PASSY M5926K Louis Vuitton Epi Leather PASSY Louis Vuitton Epi Leather PASSY M5926J Louis Vuitton Epi Leather PASSY Louis Vuitton Epi Leather Lockit M4229K Louis Vuitton Epi Leather Lockit Louis Vuitton Epi Leather Lockit M4229J Louis Vuitton Epi Leather Lockit Louis Vuitton Epi Leather Lockit M42292 Louis Vuitton Epi Leather Lockit

  38. Avatar
    start sharing not selling Sun, 18 Jul 2010 13:27:55 GMT

    wow its just begun so can i join too hehe

  39. Avatar
    wholesale cell phones Sat, 24 Jul 2010 02:13:59 GMT

    A wholesale Cell phones allows its user to make and receive telephone calls to and from the public telephone network which includes other mobiles and fixed line phones across the world. It does this by connecting to a wholesale Cell phones network owned by a mobile network operator. A key feature of the cellular network is that it enables seamless wholesale Cell phones even when the user is moving around wide areas via a process known as handoff or handover.

  40. Avatar
    vibram fivefingers Tue, 27 Jul 2010 02:55:20 GMT
  41. Avatar
    replica jewelry Fri, 30 Jul 2010 08:12:58 GMT

    links of london earrings play a bigger national role, Cartier Tortue but he has kept a tiffany diamond rings low profile since leaving the replica rado Florida governor’s mansion in 2007. The tiffany engagement rings question that lingers on many panerai 8 days

  42. Avatar
    Mahina Leather Sun, 01 Aug 2010 11:17:08 GMT

    so good

  43. Avatar
    LV Handbags On Sale Sun, 01 Aug 2010 11:19:08 GMT

    thank you

  44. For me, I’m really interested in seeing what they’ve done to hopefully improve some of the short-comings of the user experience through their interaction design process. For example, tabs containing their own url/search fields sounds refreshing (I really dislike the hierarchy currently). Also, I’m really looking forward to their dashboard-like default pag

  45. Avatar
    Christian Louboutin Pumps Wed, 04 Aug 2010 05:45:59 GMT

    I don’t usually reply to posts but I will in this case.

  46. Avatar
    Adidas Online Shop Wed, 04 Aug 2010 05:46:28 GMT

    I don’t usually reply to posts but I will in this case.

  47. Avatar
    Mens Adidas Shoes Fri, 06 Aug 2010 03:02:10 GMT

    really good read. keep posting like this!

  48. Avatar
    briefcase manufacturer Fri, 06 Aug 2010 03:23:15 GMT

    oduce more people to the gem by showing you all how to use it to talk to the new RubyUR

  49. Avatar
    christian louboutin Sat, 07 Aug 2010 01:20:01 GMT

    Thanks a lot for sharing. You have done a brilliant job. wait for more info. christian louboutin

  50. Avatar
    vibram fivefingers Sat, 07 Aug 2010 02:34:19 GMT

    The more reading, the better .

  51. Avatar
    nike shox Sat, 07 Aug 2010 03:48:25 GMT

    How to make use of the new Ruby URL ?

  52. Avatar
    nike shox Sat, 07 Aug 2010 03:48:39 GMT

    How to make use of the new Ruby URL ?

  53. Avatar
    xubeibei Mon, 09 Aug 2010 06:23:19 GMT

    Thanks for your article. I think I have known how to do it. burberry wallet

  54. Avatar
    sciphone i9 Mon, 09 Aug 2010 07:47:23 GMT
  55. Avatar
    sciphone i9 Mon, 09 Aug 2010 07:47:29 GMT
  56. Avatar
    ugg boot sale Tue, 10 Aug 2010 02:33:35 GMT
  57. Avatar
    nike sb Thu, 12 Aug 2010 02:38:08 GMT
  58. Avatar
    SPEEDY 35 Mon, 16 Aug 2010 03:19:58 GMT

    so good!!!

  59. Avatar
    louis vuitton abbesses Mon, 16 Aug 2010 03:21:13 GMT

    have a good time

  60. Avatar
    sfds Mon, 23 Aug 2010 07:40:22 GMT

    wholesale nike shoesShop a great selection of authentic Nike shoes&Nike Air Max with reasonable price for the entire families at nike-shoes-max.com.nike shoes 100% quality guaranteed and smooth customer service.UGG Women’s Classic Cardy Boots 5819 are available with colorful knit uppers (composed of a wool blend) and a sheepskin sock liner for extra comfort.ugg boots It is detailed with three oversized wood buttons, allowing it to be styled buttoned up, australia uggslouched down, slightly unbuttoned, or completely cuffed down. They have a light and flexible EVA outsole along with a suede heel guard provides durable wear all season long. That is why it is one of several styles that have been all time favorites with women.

  61. Avatar
    christian louboutin Tue, 24 Aug 2010 02:44:36 GMT

    <a href=”http://www.christianlouboutintobuy.com/”>christian louboutin Shoes are currently becoming the top fashion accessory , which is made more obvious by the exotic shoe trends anticipated for every year.

  62. Avatar
    dfdfdf Tue, 24 Aug 2010 06:27:23 GMT

    “If you are cartier replica jewelry out shopping and you check discount Gucci shoes in somewhere and can immediately cartier wallets for men get a deal, that’s a burberry military belt win-win for you and the Mont Blanc Watch marketer,” Williamson added.

    On Foursquare, for swiss replica watch instance, Starbucks has offered a gucci clothing for men $1 discounts to the “mayors” Gucci outlet of its local coffee shops cartier belt  that is, people who Gucci 2009 check in the most often Breitling Watch  as a reward.

  63. Avatar
    louis vuitton Fri, 27 Aug 2010 07:29:44 GMT
  64. Avatar
    christian louboutin Mon, 30 Aug 2010 09:40:30 GMT

    christian louboutin Brad Wreidt of Bluetoad.com joins us and speaks about how tochristian louboutin shoes take your publication online, cost efficiently. christian louboutin saleTo make it even better

  65. Avatar
    Accident Insurance Mon, 30 Aug 2010 12:51:04 GMT

    we can use to communicate with the RubyURL API. We’ll call this shorten as we’re using RubyURL to shorten long URLs… right?

  66. Avatar
    hilda_dada Tue, 31 Aug 2010 08:53:02 GMT

    Great job here. I really enjoyed what you had to say. Keep going because you definitely bring a new voice to this subject. Not many people would say what you’ve said and still make it interesting. Well, at least I’m interested.

    custom packaging bags

  67. Avatar
    vector conversion Tue, 31 Aug 2010 15:14:01 GMT

    Now, I feel good, I started take up real income. vector conversion have never thought that there weren’t any need in big initial investment.Keep working, great job, I love it! digitizing software Thanks.Great post. I have been searching for this exact info for a while now. I will bookmark it in the public bookmarking sites to get you more traffic. stock designs Now lets see if I can do something productive with it.Great job here. I really enjoyed what you had to say. Keep going because you definitely bring a new voice to this subject.

Share your thoughts... (really...I want to hear them)

Comments