forked from sinatra/sinatra-recipes
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
merge builder and rss into one guide
- Loading branch information
Zachary Scott
committed
Mar 18, 2013
1 parent
7ed6c7c
commit 024a9da
Showing
3 changed files
with
43 additions
and
62 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
RSS Feed | ||
-------- | ||
|
||
The [builder](https://github.com/jimweirich/builder) gem used for creating XML | ||
markup is required in this recipe. | ||
|
||
Let's first write our basic app to begin with: | ||
|
||
```ruby | ||
require 'sinatra' | ||
require 'builder' # dont for get this! | ||
|
||
get '/rss' do | ||
@posts = # ... find posts | ||
builder :rss | ||
end | ||
``` | ||
|
||
Then, assuming that your site url is `http://liftoff.msfc.nasa.gov/`, we will | ||
set up our view at `views/rss.builder`: | ||
|
||
```ruby | ||
xml.instruct! :xml, :version => '1.0' | ||
xml.rss :version => "2.0" do | ||
xml.channel do | ||
xml.title "Liftoff News" | ||
xml.description "Liftoff to Space Exploration." | ||
xml.link "http://liftoff.msfc.nasa.gov/" | ||
|
||
@posts.each do |post| | ||
xml.item do | ||
xml.title post.title | ||
xml.link "http://liftoff.msfc.nasa.gov/posts/#{post.id}" | ||
xml.description post.body | ||
xml.pubDate Time.parse(post.created_at.to_s).rfc822() | ||
xml.guid "http://liftoff.msfc.nasa.gov/posts/#{post.id}" | ||
end | ||
end | ||
end | ||
end | ||
``` | ||
|
||
This will render the RSS inline, directly from the handler. |