Monday, June 29, 2009

Quick Start on RSS Feeds

‹prev | My Chain | next›

Moving on to the next Cucumber feature, I need to implement RSS feeds. First up is the RSS feed for the meals:



It is nice that, once a project has reached a certain point, one or two steps are already defined when starting on a new feature or scenario. The "Given" step of 20 yummy meals is already working, so I am able to get right down to business of accessing the RSS feed. Since I have yet to define that action in the Sinatra application, I am done with my outside, Cucumber work and am ready to dive into the inside, driven by RSpec work.

In the legacy Rails app (and the legacy baked-from-XML version before it), the RSS feed was named main.rss. To keep disruption to a minimum, I will keep that name, which means that I first drive RSS implementation with this example:
  describe "GET /main.rss" do
it "should respond OK" do
get "/main.rss"
last_response.should be_ok
end
end
Implementation of this simple example is predictably simple:
get '/main.rss' do
""
end
Before switching to a RSS builder library, I implement a simple string bare bones RSS feed. This is a simpler step than going straight for the RSS builder. Simpler, smaller steps make for fewer things that can go wrong moving to subsequent steps, making for a smoother overall development process.

The example that I use to drive the bare bones RSS feed is:
    it "should be the meals rss feed" do
get "/main.rss"
last_response.
should have_selector("channel title",
:content => "EEE Cooks: Meals")
end
And the bare bones RSS feed that implements this example is:
get '/main.rss' do
"<channel><title>EEE Cooks: Title</title></channel>"
end
With that example passing, I will give the built-in ruby library RSS:Maker a try for building the real RSS feed. I convert the bare bones string version to:
get '/main.rss' do
content_type "application/rss+xml"

rss = RSS::Maker.make("2.0") do |maker|
maker.channel.title = "EEE Cooks: Title"
end

rss.to_s
end
When I run the spec, however, I get this failure:
2)
RSS::NotSetError in 'eee GET /main.rss should be the meals rss feed'
required variables of maker.channel are not set: link, description
./eee.rb:40:in `GET /main.rss'
OK, I guess I can understand that those two attributes are needed, so I add them:
get '/main.rss' do
content_type "application/rss+xml"

rss = RSS::Maker.make("2.0") do |maker|
maker.channel.title = "EEE Cooks: Title"
maker.channel.link = "http://www.eeecooks.com/"
maker.channel.description = "Meals from a Family Cookbook"
end

rss.to_s
end
Which makes the spec pass again.

That is a good stopping point for today. Tomorrow I will build up the items in the RSS feed by pulling them from CouchDB.

No comments:

Post a Comment