RSS Parser in 10 lines of Ruby or in 153 lines of C++
I know there’s like 10 libraries to do this already, but they all seemed a bit heavy to me. I just wanted to grab the items from an RSS feed. Plain and simple. What’s amazing to me is that it could be done in 10 lines of Ruby. This parser reads an RSS feed, and stores the descriptions, items, and links into a hash of arrays.
#!/usr/bin/env ruby
require 'http-access2'
h = HTTPAccess2::Client.new()
element = Hash.new {|h,k| h[k] = []}
while urlstr = ARGV.shift
response = h.get(urlstr) { |data|
data.gsub(/<(description|title|link)>(.*?)<\/(description|title|link)>/m) {
element[$1].push($2.gsub(/\s\s+/m,' '))
}
}
end
site_description = element['description'].shift
site_title = element['title'].shift
Stupidly, I wrote the same code in C++ first because I thought it would run faster. After starting down that path, I found myself wanting to use ActiveRecord. So, I ported over to Ruby.
The C++ version is here if you really want to see it. It’s 121 lines of C++ with a 32 line h-file. Truth be told, it actually took me less time to code the C++ because I made a couple of mistakes in my Ruby version at first. All said and done, the Ruby version is a helluva lot easier to maintain than the C++ which I think is why I spent the time to do it in the first place.
Trackbacks
Use this link to trackback from your own site.






You could make the c++ function “int feed_parser::get_tag(string& sOutput, istream* input)” into a void and remove the “return 0;” line. You don’t use the return value from this function anyhow.
Indeed. I think perhaps maybe I was going to do some kind of error check in that function *eventually* before I rewrote it in ruby and activerecord.
Is that $gt; intended to be a “>”? I haven’t tried the code yet but “>” makes more sense to me.
Right on, brother. Thanks for catching that one.
Google sent me here when I asked it for help with a problem in erlang. The person of small stature driving the chess automaton known as Google is truly wise, for this post helped solve the problem.