Adds easy defaults to writing Atom feeds with the Builder template engine (this does not work on ERB or any other template languages).
Full usage example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | config/routes.rb: Rails.application.routes.draw do resources :posts root to: "posts#index" end app/controllers/posts_controller.rb: class PostsController < ApplicationController::Base # GET /posts.html # GET /posts.atom def index @posts = Post.all respond_to do |format| format.html format.atom end end end app/views/posts/index.atom.builder: atom_feed do |feed| feed.title( "My great blog!" ) feed.updated( @posts [ 0 ].created_at) if @posts .length > 0 @posts . each do |post| feed.entry(post) do |entry| entry.title(post.title) entry.content(post.body, type: 'html' ) entry.author do |author| author.name( "DHH" ) end end end end |
The options for #atom_feed are:
-
:language
: Defaults to âen-USâ. -
:root_url
: The HTML alternative that this feed is doubling for. Defaults to / on the current host. -
:url
: The URL for this feed. Defaults to the current URL. -
:id
: The id for this feed. Defaults to âtag:#{request.host},#{options}:#{request.fullpath.split(â.â)}â -
:schema_date
: The date at which the tag scheme for the feed was first used. A good default is the year you created the feed. See feedvalidator.org/docs/error/InvalidTAG.html for more information. If not specified, 2005 is used (as an âI don't careâ value). -
:instruct
: Hash of XML processing instructions in the form {target => {attribute => value, }} or {target => [{attribute => value, }, ]}
Other namespaces can be added to the root element:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | app/views/posts/index.atom.builder: feed.title( "My great blog!" ) feed.updated(( @posts .first.created_at)) feed.tag!( 'openSearch:totalResults' , 10 ) @posts . each do |post| feed.entry(post) do |entry| entry.title(post.title) entry.content(post.body, type: 'html' ) entry.tag!( 'app:edited' , Time .now) entry.author do |author| author.name( "DHH" ) end end end end |
The Atom spec defines five elements (content rights title subtitle summary) which may directly contain xhtml content if type: 'xhtml' is specified as an attribute. If so, this helper will take care of the enclosing div and xhtml namespace declaration. Example usage:
1 2 3 4 5 | entry.summary type: 'xhtml' do |xhtml| xhtml.p pluralize(order.line_items.count, "line item" ) xhtml.p "Shipped to #{order.address}" xhtml.p "Paid by #{order.pay_type}" end |
atom_feed
yields an AtomFeedBuilder
instance.
Nested elements yield an AtomBuilder
instance.
Please login to continue.