Resurrecting feed_tools, part 2

(Follow-up on part 1)

One of the great and powerful things about Ruby is its ability to expressively extract common patterns through metaprogramming. Even the standard library in Ruby provides some good examples. One of my favorites is the generation of getter/setter methods for instance variables, the attr methods:

class Person
  attr_reader :name # adds 'name' method (getter)
  attr_writer :phone # adds 'phone=' method (setter)
  attr_accessor :address # adds both 'address' and 'address='
  attr :gender # same as attr_accessor
end

Using the metaprogramming techniques like this, we’re going to clean up a few things in feed_tools.

Resurrecting feed_tools

Last week, a couple of unrelated sites I’ve worked on converged on the same problem: slow loading of data from RSS feeds. In one case, the site at times refused to load a feed at all, then at other times added a whole second to the page load time! The culprit, of course was feed_tools. While a very versatile and flexible library at parsing even ill-formed feeds, feed_tools is notorious for poor performance and huge memory usage. I was inspired by Charlie Savage’s resurrection of libxml-ruby to do the same for feed_tools, partly because it’s an old library (started in 2005) and because it would be an excuse to use the new hotness that is libxml-ruby.

To get a sense of my progression on this, follow my Twitter page. Here’s a few:

If FeedTools were to have a mental disorder, it would definitely be paranoid schizophrenia. Let’s hope I can be an anti-psychotic.

FeedTools’ largest problem is a lack of DRY (and poor Ruby style). The real intention of the code is lost in the repetition.

FeedTools uses ObjectSpace. EPIC FAIL

Modifying attributes with Hpricot

If you’re processing HTML with Hpricot and want to change the attributes on a tag in a search block, beware that the attributes method on an element returns a copy of the actual attributes. In order to change the real attribute on the element, you need to set raw_attributes on it. Here’s what I had to do, boiled down to the scum on the pan:

doc = Hpricot(html)
doc.search("img").each do |element|
  # do some processing here, calculating the new attribute value
  # ...
  element.raw_attributes = element.attributes.merge("src" => new_src)
end
# now save the tweaked document
file.write(doc.to_html)

Here's what I wanted to say...

...but didn’t have the planning or eloquence to deliver during the ill-executed “panel discussion” at BarCamp KC.

“In addition, because of the magic of many of the early screencasts and hype surrounding Rails, some people came to the framework thinking of it as some sort of magical solution that would automate the entire business of creating web applications. In reality, while developers of all levels can take advantage of the power Rails and Ruby provide, the best results will still come from developers who invest the time to gain a solid foundation.” — Eldon Alameda

(emphasis added)

BarCamp KC a Success!

BarCamp was a blast yesterday. I met some really great and smart people, and my presentation went over very well. I think we’ll have some more people using Radiant in the near future. For those who didn’t get to see my presentation, or want to review what they did see, click below for a PDF of the slides (with notes).

Thanks again to Pete Thomas for doing the legwork, Ad Astra for the space, and all of the sponsors for helping make it happen!

Download the slides.