Thursday, July 2, 2009

Don't Program RSS by Coincidence

‹prev | My Chain | next›

Up tonight is the RSS recipes feed. For the most part, the inside, RSpec driven work is the same as that for meals RSS feed.

I do need to create a new, recipes by-date CouchDB view:
  recipes_view = <<_JS
{
"views": {
"by_date": {
"map": "function (doc) {
if (typeof(doc['preparations']) != 'undefined') {
emit(doc['date'], [doc['_id'], doc['title']]);
}
}"
},
},
"language": "javascript"
}
_JS

RestClient.put "#{@@db}/_design/recipes",
recipes_view,
:content_type => 'application/json'
That gets consumed by the Sinatra application:
  url = "#{@@db}/_design/recipes/_view/by_date?limit=10&descending=true"
data = RestClient.get url
@recipe_view = JSON.parse(data)['rows']
As with the meals RSS feed, I use the results of the CouchDB view to build the recipes RSS feed with RSS::Maker.

With the inside work complete, I can move back out to the Cucumber scenario:
Feature: RSS

So that I tell my user when there are updates to this great cooking site
As an RSS bot
I want to be able to consume your RSS

Scenario: Recipe RSS

Given 20 delicious, easy to prepare recipes
When I access the recipe RSS feed
Then I should see the 10 most recent recipes
And I should see the summary of each recipe
The first step is a re-used, earlier defined step. I can define the next two steps with:
When /^I access the recipe RSS feed$/ do
visit('/recipes.rss')
response.should be_ok
end

Then /^I should see the 10 most recent recipes$/ do
response.
should have_selector("channel > item > title",
:count => 10)
response.
should have_selector("channel > item > title",
:content => "delicious, easy to prepare")
end
When I get to the last step, I realize that I will have to either choose a different first step or define a new one. The last step calls for recipes summaries, but the "Given 20 delicious, easy to prepare recipes" step does not define them.

I will determine what to do with that first step tomorrow. I may also want to refactor a little.

No comments:

Post a Comment