The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
What's New in Edge Rails: Get Your RSS & Atom Feeds for Free

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
rwdaigle

Posts: 312
Nickname: rwdaigle
Registered: Feb, 2003

Ryan is a passionate ruby developer with a strong Java background.
What's New in Edge Rails: Get Your RSS & Atom Feeds for Free Posted: Sep 14, 2006 7:42 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by rwdaigle.
Original Post: What's New in Edge Rails: Get Your RSS & Atom Feeds for Free
Feed Title: Ryan's Scraps
Feed URL: http://feeds.feedburner.com/RyansScraps
Feed Description: Ryan Daigle's various technically inclined rants along w/ the "What's new in Edge Rails" series.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by rwdaigle
Latest Posts From Ryan's Scraps

Advertisement

Rails is all about some open-ness and interopability and it has become incrementally easier to achieve this with the new Resource Feeder plugin. This plugin gives you an easy way to create RSS and Atom feeds in your controllers from a collection of model objects with the use of the rss_feed_for and atom_feed_for methods:


class PostsController < ActionController

  # Build an rss feed
  def rss
    rss_feed_for Post.find(:all, :order => 'created_at',
                               :limit => 10)
  end

  # Build an atom feed
  def atom
    atom_feed_for Post.find(:all, :order => 'created_at',
                                :limit => 10)
  end

end

So how do these feed methods know how to pull information from each individual resource of the collection? It uses a combination of options passed into the feed methods and specific properties of the resource. If you don’t pass any options into these feed methods your model objects should have title, description and created_at reader methods (and updated_at for atom).

Options

There are several options you can pass into these nice little feed methods to customize the title of the feed and most of the other feed elements. Here’s my attempt at enumerating all of them for RSS:

Feed options (w/ defaults):
  • options[:feed][:title] ; Pluralization of the model class, i.e. “Posts”
  • options[:feed][:link] ; Url to this record
  • options[:feed][:description] ; omitted if not specified
  • options[:feed][:language] ; “en-us”
  • options[:feed][:ttl] ; 40
Individual item options (if specified will be used for all items)
  • options[:item][:title]
  • options[:item][:description]
  • options[:item][:pub_date]
General options:
  • options[:url_writer] ; an instance of UrlWriter for your app (I think? Help me out here!)

Just pass in these options to your feed methods as needed:


class PostsController < ActionController

  # Build an rss feed
  def rss
    rss_feed_for Post.find(:all, :order => 'created_at',
                               :limit => 10),
                    { :feed => {:title => "All posts"},
                      :item => {:pub_date => Time.now} }
  end

end

Feelin’ sassy? Try taking advantage of this guy :


class PostsController < ActionController

  # View a collection of posts.  If "format" is specified
  # will return view of that type (of either rss or atom format)
  # /posts/list?format=rss => RSS
  # /posts/list?format=atom => Atom

  def list
    @posts = Post.find(:all, :order => 'created_at',
                            :limit => 10)
    options = { :feed => {:title => "All posts"},
                    :item => {:pub_date => Time.now} }

    respond_to do |wants|
      wants.html
      wants.rss { rss_feed_for @posts, options }
      wants.atom { atom_feed_for @posts, options }
    end
  end
end

It just feels so right sometimes!

oh yeah, and to install this pup just do:

script/plugin install resource_feeder

tags: rubyonrails, rails, rss, atom

Read: What's New in Edge Rails: Get Your RSS & Atom Feeds for Free

Topic: Controlling Rails Process Size Previous Topic   Next Topic Topic: XML Webcast with IBM's team plus free book

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use