denniss
denniss

Reputation: 17589

accessing remote xml in rails

I need to access xml that is located in another web server. Does rails/ruby have a function that allows me to access this xml and then may be store it in a variable so that i can process it with libxml-ruby?

Upvotes: 2

Views: 648

Answers (2)

Chubas
Chubas

Reputation: 18043

If you're not making DOM modifications (that is, you don't need the XML structure but rather the data in it) you can use HTTParty, which does all the parsing for you.

For example:

timeline = HTTParty.get("http://twitter.com/statuses/public_timeline.xml", :format => :xml)
timeline["statuses"].map{|status| status["user"]["screen_name"] } # => ["bobby", "johnny", "denisss"]

Upvotes: 3

Larry K
Larry K

Reputation: 49104

There are a number of Ruby-level libraries to access remote HTTP resources. The oldest is Net::HTTP

Once you've received the xml, use Hpricot or other options to parse it.

Added:

Remember that you probably don't want to make your clients wait while your Rails server queries another server. Cache your XML results if you can, or fetch the XML in the background.

Upvotes: 1

Related Questions