Tuesday, September 19, 2006

FeedTools and FeedUpdater fun

Playing around with FeedTools and FeedUpdater.
Fun stuff.

Had a few lessons learned I wanted to share:

1) load the rails app feed config file


To get FeedUpdater to find my 'feed_updater.yml' file, I had to make a small change to some of the file loading logic. It kept loading the example/default comfig - so I changed the list of paths to look for my file first, by climbing out of the vendor directory, and getting the file in the regular rails app config directory.


config_file = FileStalker.hunt([
"./feed_updater.yml",
"../config/feed_updater.yml"
])


to:

config_file = FileStalker.hunt([
"../../../config/feed_updater.yml",
"./feed_updater.yml",
"../config/feed_updater.yml"])


2) Use the rails app model objects in the custom updater script.


This took some experimentation to figure out.

As the rails wiki says, you need to require the environment script to be able to use the rails app goodies in a standalone script. But where, pray-tell, should you put this require?

Use the on_begin method of the custom script.
So if you have in your 'feed_updater.yml':


load_script: lib/my_feed_updater.rb


Then in this file, you need something like the following:

class MyFeedUpdater < FeedTools::FeedUpdater

on_begin do
#load all thing needed to use rails app stuff
require File.dirname(__FILE__) + '/../config/environment'

#use your model like its going out of style
end

on_update do |feed, seconds|
self.logger.info("Loaded '#{feed.href}'. Updated (#{feed.title}) in #{seconds} seconds.")
end

on_error do |href, error|
self.logger.info("Error updating '#{href}':")
self.logger.info(error)
end

on_complete do |updated_feed_hrefs|
end
end


Hope someone else gets some fun out of this.