打印

[Rails 常用插件简介]atom_feed_helper

本帖已经被作者加入个人空间

[Rails 常用插件简介]atom_feed_helper

前两天(好像?)看到大家在讨论feeds生成,工作累了,来写点文字,稍作休息一下:)

DHH写的,很小巧实用的代码

0: 前因
cclong 的php?name=%CC%FB%D7%D3" onclick="tagshow(event)" class="t_tag">帖子:如何制作feed?
http://www.ruby-lang.org.cn/foru ... &extra=page%3D1

1:安装

ruby script/plugin install atom_feed_helper


2:使用
这个插件太简单的,没什么好说的,大家如果有什么问题请跟帖,一起讨论。(下面代码取之插件中的README). 大家一起来阅读一下代码
Controller file:posts_controller.rb

	  def index
 	    @posts = Post.find(:all, :limit => 25)
 	   
 	    respond_to do |format|
 	      format.html
 	      format.atom
 	    end
	  end


View file:posts/index.atom

 	  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


生成,这个也就是你想要的:

 <?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>


三:源码
/plugins/atom_feed_helper/lib/atom_feed_helper.rb
http://dev.rubyonrails.org/brows ... atom_feed_helper.rb

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},2007:#{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},2007:#{record.class}#{record.id}")
          @xml.published(record.created_at.xmlschema) if record.respond_to?(:created_at)
          @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


Rails的“契约”中有一条,DRY:Don't repeat yourself,还记得么?

PS:
 还有个叫Simple Rss,写的和大家写的没什么区别了,我就不推荐也不写了。

Any question? 有的请举手。为了提高大家的积极性,有奖励,o(∩_∩)o...哈哈

[ 本帖最后由 martin 于 2007-9-3 16:53 编辑 ]

TOP

DHH就是比偶写的好。呃。
本帖最近评分记录
  • martin R币 +2 论坛一直参与讨论的人不多:) 2007-9-4 06:58
谢谢大家加入Ruby中文社区!
[寻找您身边的Rubyist.]

TOP

本帖最近评分记录
  • martin R币 +2 fetcher is not generate,is it?:) 2007-9-4 11:06

I.forget('you'){|something| something.remember.deepen}

TOP

这个插件我看了一下,还不值得推荐:) 没有价值。但是他里面用到的那个Hpricot http://code.whytheluckystiff.net/hpricot/ 还是值得看一下的。以前用过,还不错:)

[ 本帖最后由 martin 于 2007-9-4 11:15 编辑 ]

TOP

Ruby Forum有一篇帖子,可以看看,http://www.ruby-forum.com/topic/78027

TOP

上午看到那个插件,就发过来,没仔细研究,那个网站值得推荐的还有个插件,RedBox。。。

I.forget('you'){|something| something.remember.deepen}

TOP

2008-11-22 17:48 Crawled by CCBot/1.0 (+http://www.commoncrawl.org/bot.html) @38.103.63.61