sebastian.martinez

Parsing an OPML with Ruby

Posted by sebastian.martinez
on February 20, 2009

And Ruby just doesn't stop surprising us!! In the past we have to deal with XML files and parse them, incredibly easy task using Hpricot library. Now the turn was for OPML(OPML) (Outline Processor Markup Language) files. In case you are not familiar with this type of files, its most common use is to exchange lists of web feeds between web feed aggregators.

We found this function to parse the OPML document recursively preserving its structure in the desktop weblog(desktop weblog), that does the job of extracting the feeds, and modified it a bit. Now it returns a hash containing the title of the articles as keys, and its links as values.

Here's the function:

def self.parse_opml(opml_node, parent_names=[])
    feeds = {}
    opml_node.elements.each('outline') do |el|
      if (el.elements.size != 0)
        feeds.merge!(parse_opml(el, parent_names + [el.attributes['text']]))
      end
      if (el.attributes['xmlUrl'])
        feeds[el.attributes['title']] = el.attributes['xmlUrl']
      end
    end
    return feeds
  end

All you have to do is call it this way:

require 'rexml/Document'

opml = REXML::Document.new(File.read('my_feeds.opml'))
feeds = parse_opml(opml.elements['opml/body'])

Pretty easy, huh? Try it out and leave your comments...

| |
jose.costa

Google Analytics with AJAX

Posted by jose.costa
on February 11, 2009

It's been a while since us all have been working in fully AJAX enabled sites (as is the case of Indieoma(Indieoma), one of our projects). Time comes when you'd like to know a little bit about who is accessing the site, when, which page, why .. and a few more questions Google Analytics can answer for you (not particularly the last one) in a very simple manner most of you already know about.

You may encounter some issue when trying to check your stats for the AJAX requests people have made. As you can imagine, the code you place in your web pages won't execute itself on each AJAX request, but more like on each entire page load. But Google doesn't let us down and give us a tiny "explanation( How do I track AJAX applications?)":http://www.google.com/support/analytics/bin/answer.py?hl=en&answer=55519 on how to pull this off.

At the heart of Indieoma(Indieoma) we use Prototype(Prototype) So Google's tip may become more something like the code below:

Ajax.Responders.register({
 onComplete: function(request) {
  // Assuming the ga.js code was loaded first
  pageTracker._trackPageview(request.url) 
}
 });

Using the Ajax.Responders(Prototype Ajax.Responders), we register a function that will fire the tracker after each AJAX request has been completed.

The _trackPageview(request.url) allows us to name the tracked page exactly as the requested URL. Note that you can name this page in any way you like. We chose to do so with the requested URL in this case.

So, that's it and happy analytics!

| |