module ActionView::Helpers::AtomFeedHelper
def atom_feed(options = {}, &block)
atom_feed yields an +AtomFeedBuilder+ instance. Nested elements yield
end
xhtml.p "Paid by #{order.pay_type}"
xhtml.p "Shipped to #{order.address}"
xhtml.p pluralize(order.line_items.count, "line item")
entry.summary type: 'xhtml' do |xhtml|
the enclosing div and xhtml namespace declaration. Example usage:
is specified as an attribute. If so, this helper will take care of
summary) which may directly contain xhtml content if type: 'xhtml'
The Atom spec defines five elements (content rights title subtitle
end
end
end
end
author.name("DHH")
entry.author do |author|
entry.tag!('app:edited', Time.now)
entry.content(post.body, type: 'html')
entry.title(post.title)
feed.entry(post) do |entry|
@posts.each do |post|
feed.tag!('openSearch:totalResults', 10)
feed.updated((@posts.first.created_at))
feed.title("My great blog!")
'xmlns:openSearch' => 'http://a9.com/-/spec/opensearch/1.1/'}) do |feed|
atom_feed({'xmlns:app' => 'http://www.w3.org/2007/app',
app/views/posts/index.atom.builder:
Other namespaces can be added to the root element:
* :instruct: Hash of XML processing instructions in the form {target => {attribute => value, }} or {target => [{attribute => value, }, ]}
2005 is used (as an "I don't care" value).
created the feed. See http://feedvalidator.org/docs/error/InvalidTAG.html for more information. If not specified,
* :schema_date: The date at which the tag scheme for the feed was first used. A good default is the year you
* :id: The id for this feed. Defaults to "tag:localhost,2005:/posts", in this case.
* :url: The URL for this feed. Defaults to the current URL.
* :root_url: The HTML alternative that this feed is doubling for. Defaults to / on the current host.
* :language: Defaults to "en-US".
The options for atom_feed are:
end
end
end
end
author.name("DHH")
entry.author do |author|
entry.content(post.body, type: 'html')
entry.title(post.title)
feed.entry(post) do |entry|
@posts.each do |post|
feed.updated(@posts[0].created_at) if @posts.length > 0
feed.title("My great blog!")
atom_feed do |feed|
app/views/posts/index.atom.builder:
end
end
end
format.atom
format.html
respond_to do |format|
@posts = Post.all
def index
# GET /posts.atom
# GET /posts.html
class PostsController < ApplicationController
app/controllers/posts_controller.rb:
end
root to: "posts#index"
resources :posts
Rails.application.routes.draw do
config/routes.rb:
Full usage example:
template languages).
Adds easy defaults to writing Atom feeds with the Builder template engine (this does not work on ERB or any other
def atom_feed(options = {}, &block) if options[:schema_date] options[:schema_date] = options[:schema_date].strftime("%Y-%m-%d") if options[:schema_date].respond_to?(:strftime) else options[:schema_date] = "2005" # The Atom spec copyright date end xml = options.delete(:xml) || eval("xml", block.binding) xml.instruct! if options[:instruct] options[:instruct].each do |target, attrs| if attrs.respond_to?(:keys) xml.instruct!(target, attrs) elsif attrs.respond_to?(:each) attrs.each { |attr_group| xml.instruct!(target, attr_group) } end end end feed_opts = { "xml:lang" => options[:language] || "en-US", "xmlns" => "http://www.w3.org/2005/Atom" } feed_opts.merge!(options).select! { |k, _| k.start_with?("xml") } xml.feed(feed_opts) do xml.id(options[:id] || "tag:#{request.host},#{options[:schema_date]}:#{request.fullpath.split(".")[0]}") xml.link(rel: "alternate", type: "text/html", href: options[:root_url] || (request.protocol + request.host_with_port)) xml.link(rel: "self", type: "application/atom+xml", href: options[:url] || request.url) yield AtomFeedBuilder.new(xml, self, options) end end