Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting started consuming web services in a Ruby on Rails 3 application

So I'm getting started learning Rails. Now that Rails 3 is out, I want to stick to learning the Rails 3 way of doing things. One of the things I want to learn how to do is how to consume web services / work with third party REST APIs / create "mashup" applications. I've only done minimal work like this with PHP and pre-built libraries.

Can someone please lead me to some resources, best practices, or give me a quick 101 lesson on how to start working with these types of APIs? What gems should I use? Some sample code to get me started would be much appreciated.

Update: I am specifically trying to use the Google Books API (non-authenticated). Since there is no client library for this API, I'm wondering how other Ruby/Rails developers are working with APIs that don't come with their own Ruby library. That's why I'm looking for a more generic solution to working with "fill in the blank" API.

Update: After some research, I noticed the Net::HTTP library. All the tutorials that talked about it were fairly old. Is this still the best library to use for consuming RESTful web services? Or is there a newer Gem that makes this easier? I just want to know which gem/library I should use, and how to work with the XML/JSON result that is returned.

Update: This presentation was really helpful. It mentions a bunch of different libraries and shows examples of some of the libraries mentioned below: http://www.slideshare.net/pengwynn/json-and-the-apinauts

like image 514
Andrew Avatar asked Sep 11 '10 00:09

Andrew


People also ask

Is Ruby on Rails a Web server?

Ruby on Rails (simplify as Rails) is a server-side web application framework written in Ruby under the MIT License. Rails is a model–view–controller (MVC) framework, providing default structures for a database, a web service, and web pages.


2 Answers

I'm a pretty big fan of HTTParty.

It's an abstraction layer on top of Net::HTTP with a nice little DSL for consuming web services. Here's a good example of how easy it is to use.

It's not without some warts (lots of dependencies) but it's really the way to go if you're on the consuming side.

like image 135
Mando Escamilla Avatar answered Sep 17 '22 18:09

Mando Escamilla


I'd recommend REST with Nokogiri: http://railscasts.com/episodes/190-screen-scraping-with-nokogiri Nokogiri works well with xml too, not just HTML.

like image 25
coder_tim Avatar answered Sep 19 '22 18:09

coder_tim