Search code examples
rubyrssatom-feed

Ruby RSS/Atom creation - including content


I am creating an Atom feed using ruby's stdlib rss library. This library is essentially undocumented , but I have it working using the example provided on this page:

require 'rss'

rss = RSS::Maker.make("atom") do |m|

    m.channel.author  = "Steve Wattam"
    m.channel.updated = Time.now
    m.channel.about   = "http://stephenwattam.com/blog/"
    m.channel.title   = "Steve W's Blog"

    storage.posts.each do |p|
        m.items.new_item do |item|
            item.link    = p.link
            item.title   = p.title
            item.updated = p.edited
            item.pubDate = p.date
            item.summary = p.summary
        end
    end
 end

This works fine. I am unable, however, to add a content element. There is no such thing as item.content=, and I can't seem to find any example code online---a browse of the source indicates that content is stored in the item (docs here), but I lack the knowledge to tease it out.

Does anyone know how I might go about adding a content element?

Incidentally, I'm aware other libraries exist to do this, but would ideally like to get this working without requiring any gems.


Solution

  • By digging through the source of the library, I've discovered that item.content yields an object of type RSS::Maker::Atom::Feed::Items::Item::Content. It's possible to set the content on that object:

    item.content.content = 'text to set as content'
    

    This object also responds to #xml_content.

    Hope this helps someone!