原创作者: hideto   阅读:1141次   评论:0条   更新时间:2011-05-26    
前面的一篇文章每天一剂Rails良药之Syndicate Your Site With RSS中我们看了怎样手动创建RSS
今天我们来看看Rails开发组提供的一个atom_feed_helper插件,它使得创建atom feeds更容易

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


我们来看看源码,首先是init.rb
# Include hook code here
ActionController::Base.helper(AtomFeedHelper)

init.rb会在Rails程序启动时自动加载,这段代码表示把AtomFeedHelper include到ActionController::Base

然后我们来看看atom_feed_helper.rb
# 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

我们会看到atom_feed()方法和我们创建RSS时代码大同小异,不过是生成的XML的格式稍显不同。

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

  respond_to do |format|
    format.html
    format.atom
  end
end

以及posts/index.atom.builder
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

看见了吧,通过atom_feed()方法简化了我们创建RSS XML的工作
这样生成的Atom为:
<?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>

代码具体我没有测试过,而大部分时候站点提供RSS就够了
不过通过这个例子的学习,我们可以自己写一个简化RSS创建的插件
评论 共 0 条 请登录后发表评论

发表评论

您还没有登录,请您登录后再发表评论

文章信息

Global site tag (gtag.js) - Google Analytics