views:

1008

answers:

3

An external web service is sending json or xml towards my app. What would be the best way to consume this data? Is there a built-in library in Rails or Ruby to do this or do I need to use a gem for this?

+2  A: 

For parsing json, you can use rails' method ActiveSupport::JSON.decode directly in your controller code. For what concerns XML parsing instead, you can choose between rexml (included in the ruby Standard Library), hapricot and libxml-ruby (available via ruby gem).

The choice is ultimately a matter of taste and performance. This benchmark claims that libxml-ruby is the fastest one, though it requires a few other libraries.

And
another nice lib for xml is nokogiri, also a rubygem
Peer Allan
+1  A: 

if it is a web service you query as a source of data, consider making an ActiveResource here is a railscast of the basics

http://railscasts.com/episodes/94-activeresource-basics

ErsatzRyan
+1  A: 

Try nokogiri.

There's a good explanation of how to use it here:

http://www.robertsosinski.com/2008/12/08/scraping-pages-made-easy-with-ruby-and-nokogiri/

flo