rdodev
rdodev

Reputation: 3192

Rails: What's the suggested approach to retrieve xml from an outside source

Rails newbie (though long time programmer) here. I'm writing an test app that retrieves data from several outside sources (think Twitter, RSS feeds, etc.) and under certain circumstances, it stores that data in a db (or presents it to the user). The data model and the views are trivial. What I'm having difficulty with is making the actual xml HTTP call to the outside source and deserialize the xml response so I can query/use it in my controller/helper. What library/gem should I use to accomplish this? I tried looking this up around the net, but only came up with some article from 2006 which, knowing how fast Rails has developed, might well be completely deprecated. Your help is much appreciated.

Upvotes: 0

Views: 105

Answers (2)

Chris Heald
Chris Heald

Reputation: 62638

Check out HTTParty. It basically lets you define objects that behave as models, but backed with external resources. The Twitter example is good - it shows how HTTParty lets you pull a timeline, which it automatically deserializes into a hash, and lets you do things with it.

Upvotes: 2

pjammer
pjammer

Reputation: 9577

If it's twitter, there are gems that allow you to take a twitter api call and essentially use the response like you would any other method. (i.e., all the parsing and 'stuff' is done for you, so you can work with 'it' in normal ruby).

One such gem is twitter gem

As for all XML feeds, you may be able to use something like REXML to hpricot. These are xml parsing libraries. I personally use REXML, but you'll see the hip kids try to use something a little 'faster'. I've never found REXML to be slow, but I'm just giving you the facts.

Now you say

deserialize the xml response so I can query/use it in my controller/helper.

Are you actually wanting to query a regular expression from within an XML response without saving it to the DB? Almost like 'on the fly'?

Upvotes: 0

Related Questions