The best kittens, technology, and video games blog in the world.

Friday, August 04, 2006

magic/xml really works

Photo from Commons by Rasbak, GFDL.
In the last few days magic/xml progressed a lot and I already use it in some of my programs. Let's cut the bullshit, and go straight to the code. First one that parses ATOM feed for my blog and prints post titles and URLs:
require 'magic_xml'
doc = XML.from_url "http://t-a-w.blogspot.com/atom.xml"
doc.children(:entry).children(:link) {|c|
   print "#{c[:title]}\n#{c[:href]}\n\n" if c[:rel] == "alternate"
}
And one that interacts with del.icio.us to get list of all my posts on given subject and nicely format, for websites like RLisp's and magic/xml's (used from eruby page template):
require 'magic_xml'
deli_passwd = File.read("/home/taw/.delipasswd").chomp
url = "http://taw:#{deli_passwd}@del.icio.us/api/posts/recent?tag=taw+blog+magicxml"
XML.from_url(url).children(:post).reverse.each_with_index {|p,i|
   print XML.li("#{i+1}. ", XML.a({:href => p[:href]}, p[:description]))
}
I guess that's enough code to convince everyone that magic/xml is not completely useless. Or is there any library in which either code would be simpler than that (or as simple) ? Anyway in the process I discovered two useful Ruby utilities. One is rdoc. It works kinda like Perldoc (or like Javadoc if you're from the dark side). You simply write comments before a methods and they get automatically copied into documentation. This is highly cool, as it requires very little work. You can now read magic/xml's documentation online. The other cool thing is rcov, which shows which lines of code were executed and which weren't. If you run rcov on your unit tests, then you get information which methods aren't tested for free. And that's a great way to find potential bugs. For an example take a look at magic/xml's coverage.

No comments: