每天一剂Rails良药之atom_feed_helper

前面的一篇文章[url=http://hideto.iteye.com/blog/81579]每天一剂Rails良药之Syndicate Your Site With RSS[/url]中我们看了怎样手动创建RSS
今天我们来看看Rails开发组提供的一个atom_feed_helper插件,它使得创建atom feeds更容易

首先安装atom_feed_helper插件
[code]
ruby script/plugin install atom_feed_helper
[/code]

我们来看看源码,首先是init.rb
[code]
# Include hook code here
ActionController::Base.helper(AtomFeedHelper)
[/code]
init.rb会在Rails程序启动时自动加载,这段代码表示把AtomFeedHelper include到ActionController::Base

然后我们来看看atom_feed_helper.rb
[code]
# Adds easy defaults to writing Atom feeds with the Builder template engine (this does not work on ERb or any other
# template languages).
module AtomFeedHelper
def atom_feed(options = {}, &block)
xml = options[:xml] || eval("xml", block.binding)
xml.instruct!

xml.feed "xml:lang" => "en-US", "xmlns" => 'http://www.w3.org/2005/Atom' do
xml.id("tag:#{request.host}:#{request.request_uri.split(".")[0].gsub("/", "")}")

if options[:root_url] || respond_to?(:root_url)
xml.link(:rel => 'alternate', :type => 'text/html', :href => options[:root_url] || root_url)
end

if options[:url]
xml.link(:rel => 'self', :type => 'application/atom+xml', :href => options[:url])
end

yield AtomFeedBuilder.new(xml, self)
end
end


protected
class AtomFeedBuilder
def initialize(xml, view)
@xml, @view = xml, view
end

def entry(record)
@xml.entry do
@xml.id("tag:#{@view.request.host_with_port},#{record.created_at.xmlschema}:#{record.class}#{record.id}")
@xml.published(record.created_at.xmlschema)
@xml.updated(record.updated_at.xmlschema) if record.respond_to?(:updated_at)

yield @xml

@xml.link(:rel => 'alternate', :type => 'text/html', :href => @view.polymorphic_url(record))
end
end

private
def method_missing(method, *arguments, &block)
@xml.__send__(method, *arguments, &block)
end
end
end
[/code]
我们会看到atom_feed()方法和我们创建RSS时代码大同小异,不过是生成的XML的格式稍显不同。

然后我们就可以这样使用了,比如PostsController:
[code]
def index
@posts = Post.find(:all, :limit => 25)

respond_to do |format|
format.html
format.atom
end
end
[/code]
以及posts/index.atom.builder
[code]
atom_feed(:url => formatted_people_url(:atom)) do |feed|
feed.title("Address book")
feed.updated(@people.first ? @people.first.created_at : Time.now.utc)

for post in @posts
feed.entry(post) do |entry|
entry.title(post.title)
entry.content(post.body, :type => 'html')

entry.author do |author|
author.name(post.creator.name)
author.email(post.creator.email_address)
end
end
end
end
[/code]
看见了吧,通过atom_feed()方法简化了我们创建RSS XML的工作
这样生成的Atom为:
[code]
<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
<id>tag:localhost:people</id>
<link type="application/atom+xml" rel="self" href="http://example.com/people.atom"/>
<title>Address book</title>
<updated></updated>
<entry>
<id>tag:localhost:3000,2007-05-18T16:35:00-07:00:Person1</id>
<published>2007-05-18T16:35:00-07:00</published>
<link type="text/html" rel="alternate" href="http://example.com/people/1" />
<title>The future is now</title>
<content type="html">Once upon a time</content>
<author>
<name>DHH</name>
<email>david@loudthinking.com</email>
</author>
</entry>
<entry>
<id>tag:localhost:3000,2007-05-18T09:36:00-07:00:Person2</id>
<published>2007-05-18T09:36:00-07:00</published>
<link type="text/html" rel="alternate" href="http://example.com/people/1" />
<title>Matz</title>
<content type="html">This is Matz</content>
<author>
<name>Matz</name>
<email>Matz</email>
</author>
</entry>
</feed>
[/code]
代码具体我没有测试过,而大部分时候站点提供RSS就够了
不过通过这个例子的学习,我们可以自己写一个简化RSS创建的插件
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值