Skip to content

Commit

Permalink
RSS feeds for meals and recipes
Browse files Browse the repository at this point in the history
  • Loading branch information
eee-c committed Jul 4, 2009
1 parent 380bb22 commit 2d66d09
Show file tree
Hide file tree
Showing 6 changed files with 250 additions and 10 deletions.
58 changes: 57 additions & 1 deletion eee.rb
Expand Up @@ -3,8 +3,11 @@
require 'rest_client'
require 'json'
require 'pony'
require 'rss'
require 'helpers'

ROOT_URL = "http://www.eeecooks.com"

configure :test do
@@db = "http://localhost:5984/eee-test"
end
Expand Down Expand Up @@ -33,6 +36,60 @@
haml :index
end

get '/main.rss' do
content_type "application/rss+xml"

url = "#{@@db}/_design/meals/_view/by_date?limit=10&descending=true"
data = RestClient.get url
@meal_view = JSON.parse(data)['rows']

rss = RSS::Maker.make("2.0") do |maker|
maker.channel.title = "EEE Cooks: Meals"
maker.channel.link = ROOT_URL
maker.channel.description = "Meals from a Family Cookbook"
@meal_view.each do |couch_rec|
data = RestClient.get "#{@@db}/#{couch_rec['key']}"
meal = JSON.parse(data)
date = Date.parse(meal['date'])
maker.items.new_item do |item|
item.link = ROOT_URL + date.strftime("/meals/%Y/%m/%d")
item.title = meal['title']
item.pubDate = Time.parse(meal['date'])
item.description = meal['summary']
end
end
end

rss.to_s
end

get '/recipes.rss' do
content_type "application/rss+xml"

url = "#{@@db}/_design/recipes/_view/by_date?limit=10&descending=true"
data = RestClient.get url
@recipe_view = JSON.parse(data)['rows']

rss = RSS::Maker.make("2.0") do |maker|
maker.channel.title = "EEE Cooks: Recipes"
maker.channel.link = ROOT_URL
maker.channel.description = "Recipes from a Family Cookbook"

@recipe_view.each do |couch_rec|
data = RestClient.get "#{@@db}/#{couch_rec['value'][0]}"
recipe = JSON.parse(data)
maker.items.new_item do |item|
item.link = ROOT_URL + "/recipes/recipe['id']"
item.title = recipe['title']
item.pubDate = Time.parse(recipe['date'])
item.description = recipe['summary']
end
end
end

rss.to_s
end

get %r{/meals/(\d+)/(\d+)/(\d+)} do |year, month, day|
data = RestClient.get "#{@@db}/#{year}-#{month}-#{day}"
@meal = JSON.parse(data)
Expand All @@ -48,7 +105,6 @@
haml :meal
end


get %r{/meals/(\d+)/(\d+)} do |year, month|
url = "#{@@db}/_design/meals/_view/by_month?group=true&key=%22#{year}-#{month}%22"
data = RestClient.get url
Expand Down
2 changes: 2 additions & 0 deletions features/rss.feature
Expand Up @@ -10,6 +10,8 @@ Feature: RSS
When I access the meal RSS feed
Then I should see the 10 most recent meals
And I should see the summary of each meal
And I should see a meal link
And I should see a meal published date

Scenario: Recipe RSS

Expand Down
1 change: 1 addition & 0 deletions features/step_definitions/recipe_search.rb
Expand Up @@ -124,6 +124,7 @@

recipe = {
:title => "#{keyword} recipe #{i}",
:summary => "recipe summary",
:date => date,
:preparations => [
{ 'ingredient' => { 'name' => 'ingredient' } }
Expand Down
51 changes: 51 additions & 0 deletions features/step_definitions/rss.rb
@@ -0,0 +1,51 @@
When /^I access the meal RSS feed$/ do
visit('/main.rss')
response.should be_ok
end

When /^I access the recipe RSS feed$/ do
visit('/recipes.rss')
response.should be_ok
end

Then /^I should see the 10 most recent meals$/ do
response.
should have_selector("channel > item > title",
:count => 10)
response.
should have_selector("channel > item > title",
:content => "Meal 0")
end

Then /^I should see the summary of each meal$/ do
response.
should have_selector("channel > item > description",
:content => "meal summary")
end

Then /^I should see a meal link$/ do
response.
should contain('http://www.eeecooks.com/meals/2009/06/11')
# should have_selector('link',
# :content => 'http://www.eeecooks.com/meals/2009/06/11')
end

Then /^I should see a meal published date$/ do
response.
should contain("Thu, 11 Jun 2009")
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

Then /^I should see the summary of each recipe$/ do
response.
should have_selector("channel > item > description",
:content => "recipe summary")
end
32 changes: 32 additions & 0 deletions features/support/env.rb
Expand Up @@ -15,6 +15,14 @@
# RSpec mocks / stubs
require 'spec/mocks'


module WebRat
class Session
def xml_content_type?; raise "here"; true end
end
end


# Webrat
require 'webrat'
Webrat.configure do |config|
Expand All @@ -25,6 +33,11 @@
session = Webrat::SinatraSession.new
session.extend(Webrat::Matchers)
session.extend(Webrat::HaveTagMatcher)

# def session.xml_content_type?
# raise "here"
# end

session
end

Expand Down Expand Up @@ -151,6 +164,25 @@
RestClient.put "#{@@db}/_design/meals",
meals_view,
:content_type => 'application/json'

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'
end

After do
Expand Down
116 changes: 107 additions & 9 deletions spec/eee_spec.rb
Expand Up @@ -54,15 +54,6 @@
get "/"
end

it "should request the most recent 13 meals from CouchDB" do
RestClient.
should_receive(:get).
with(/by_date.+limit=13/).
and_return('{"rows": [] }')

get "/"
end

it "should pull back full details for the first 10 meals" do
RestClient.
stub!(:get).
Expand Down Expand Up @@ -96,6 +87,58 @@
end
end

describe "GET /main.rss" do
it "should respond OK" do
get "/main.rss"
last_response.should be_ok
end

it "should be the meals rss feed" do
get "/main.rss"
last_response.
should have_selector("channel title",
:content => "EEE Cooks: Meals")
end

it "should request the 10 most recent meals from CouchDB" do
RestClient.
should_receive(:get).
with(/by_date.+limit=10/).
and_return('{"rows": [] }')

get "/main.rss"
end

it "should pull back full details for the 10 meals" do
RestClient.
stub!(:get).
and_return('{"rows": [' +
'{"key":"2009-06-10","value":["2009-06-10","Foo"]},' +
'{"key":"2009-06-09","value":["2009-06-09","Foo"]},' +
'{"key":"2009-06-08","value":["2009-06-08","Foo"]},' +
'{"key":"2009-06-07","value":["2009-06-07","Foo"]},' +
'{"key":"2009-06-06","value":["2009-06-06","Foo"]},' +
'{"key":"2009-06-05","value":["2009-06-05","Foo"]},' +
'{"key":"2009-06-04","value":["2009-06-04","Foo"]},' +
'{"key":"2009-06-03","value":["2009-06-03","Foo"]},' +
'{"key":"2009-06-02","value":["2009-06-02","Foo"]},' +
'{"key":"2009-06-01","value":["2009-06-01","Foo"]}' +
']}')

RestClient.
should_receive(:get).
with(/2009-06-/).
exactly(10).times.
and_return('{"title":"foo",' +
'"date":"2009-06-17",' +
'"summary":"foo summary",' +
'"menu":[]}')

get "/main.rss"
end
end


describe "GET /meals/YYYY" do
it "should respond OK" do
get "/meals/2009"
Expand Down Expand Up @@ -408,3 +451,58 @@
end
end
end

describe "GET /recipe.rss" do
before(:each) do
RestClient.stub!(:get).and_return('{"rows": [] }')
end

it "should respond OK" do
get "/recipes.rss"
last_response.should be_ok
end

it "should be the meals rss feed" do
get "/recipes.rss"
last_response.
should have_selector("channel title",
:content => "EEE Cooks: Recipes")
end

it "should request the 10 most recent meals from CouchDB" do
RestClient.
should_receive(:get).
with(/by_date.+limit=10/).
and_return('{"rows": [] }')

get "/recipes.rss"
end

it "should pull back full details for the 10 meals" do
RestClient.
stub!(:get).
and_return('{"rows": [' +
'{"key":"2009-06-10","value":["2009-06-10","Foo"]},' +
'{"key":"2009-06-09","value":["2009-06-09","Foo"]},' +
'{"key":"2009-06-08","value":["2009-06-08","Foo"]},' +
'{"key":"2009-06-07","value":["2009-06-07","Foo"]},' +
'{"key":"2009-06-06","value":["2009-06-06","Foo"]},' +
'{"key":"2009-06-05","value":["2009-06-05","Foo"]},' +
'{"key":"2009-06-04","value":["2009-06-04","Foo"]},' +
'{"key":"2009-06-03","value":["2009-06-03","Foo"]},' +
'{"key":"2009-06-02","value":["2009-06-02","Foo"]},' +
'{"key":"2009-06-01","value":["2009-06-01","Foo"]}' +
']}')

RestClient.
should_receive(:get).
with(/2009-06-/).
exactly(10).times.
and_return('{"title":"foo",' +
'"date":"2009-06-17",' +
'"summary":"foo summary",' +
'"menu":[]}')

get "/recipes.rss"
end
end

0 comments on commit 2d66d09

Please sign in to comment.