der kl@mmeraffe | ruby. java. development.

a developers guide to the world of code

Friday, September 01, 2006

Simple Atom Feed With Ruby

Its really simple, took me bout an hour :-) But it suits my needs, and i don't have to implement other libs then build in (not even xml, allright, i'm crazy). Its not fast, reliable or something, but i wanted to create an atom feed that shows the files from a directory.
Heres the code:

class Atomfeed
attr_accessor :title, :subtitle, :link, :updated, :authorname, :authoremail, :id, :entries

def initialize
@title = ""
@subtitle = ""
@link = "http://"
@updated = ""
@authorname = ""
@authoremail = ""
@id = ""
@entries = []

def create_feed
@xml = ""
@xml = @xml + "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<feed xmlns=\"\">\n"
@xml = @xml + "<title>" + @title + "</title>\n"
@xml = @xml + "<subtitle>"+@subtitle+"</subtitle>\n"
@xml = @xml + "<link href=\""+@link+"\"/>\n"
@xml = @xml + "<updated>"+@updated+"</updated>\n"
@xml = @xml + "<author>\n <name>"+@authorname+"</name>\n <email>"+@authoremail+"</email>\n </author>\n"
@xml = @xml + "<id>"+@id+"</id>\n"
@entries.each do |entry|
@xml = @xml + " <entry>\n"
@xml = @xml + " <title>"+entry.title+"</title>\n"
@xml = @xml + " <link href=\"""\"/>\n"
@xml = @xml + " <id>""</id>\n"
@xml = @xml + " <updated>"+entry.updated+"</updated>\n"
@xml = @xml + " <summary>"+entry.summary+"</summary>\n"
@xml = @xml + " </entry>\n"
@xml = @xml + "</feed>"

class Entry
attr_accessor :title, :link, :id, :updated, :summary

def initialize
@title = ""
@link = "http://"
@id = ""
@updated = ""
@summary = ""

Thats it (Lots of stuff to refactor here, but - hey, it works). To use it, i just start a webrick httpserver and map an action to the create_feed method. Before that, i fill the Atomfeed / Entry objects:

require 'webrick'
include WEBrick

s = :Port => 2000 )

class AtomServlet < HTTPServlet::AbstractServlet
def do_GET(req, res)
@directory = "c:\\dev"
@files = Dir["*"]
feed =
feed.title = "New files"
feed.subtitle = "Yeah, new Files"
feed.updated =
feed.authorname = "dan"
feed.authoremail = "" = "123456789"

@files.each do |file|
entry =
entry.title = file.to_s = "http://"+file.to_s = file.to_s
entry.updated = "134"
entry.summary = "huuhu"

res.body = feed.create_feed
res['Content-Type'] = "text/html"

s.mount("/atom.xml", AtomServlet)
trap("INT"){ s.shutdown }

Most Information here doesn't make sense, but it shows all files in the working directory :-) ruby is so much fun!

add to | submit to digg | submit to reddit


  • At 11:16 AM, Anonymous Anonymous said…

    This probably won't give you a valid feed.

    * Make your dates correct to RFC 3339

    * Create correct ids

    * to insert XHTML you have to write <summary type="xhtml"> <div xmlns=""> yadayada </div></summary>

    * use the feed validator for more

  • At 1:53 PM, Blogger dan said…

    yeah you are right, its just a hack. but it works for what i wanted it to. maybe i just think its great you can implement feed creation and the web server in one script. :-)

  • At 11:09 PM, Blogger sink sink socks said…

    He rode out to the farm, not often, but first arsch sometimes, as a matter ofduty, and was forced to acknowledge that Sam was managing better thancould have been expected in the absence of his own continuousguidance.Also numbers, which frequentlyoccur in the dream, are assigned by Stekel a fixed symbolical meaning,but these interpretations seem neither sufficiently verified nor ofgeneral validity, sex pornostar us although the interpretation in individual cases cangenerally be recognized as probable.As far as our knowledge goes there is nopsychic apparatus possessing only the primary process, and in so far itis a theoretic fiction; but so much is based on fact that the primaryprocesses are present in the apparatus from the beginning, while thesecondary processes develop gradually in the course of life, inhibitingand covering the primary ones, naturist female and gaining complete mastery over themperhaps only at the height of life.His heart was beating thickly, fkk gallerie hismuscles twitched, and the awful terrors of night pulsed and shudderedover him.

  • At 7:49 AM, Blogger Seb said…

    *** SPAM SPAM SPAM ***

    hehe... ach ja... dir hats die formatierung zerschossen =)

  • At 1:25 AM, Blogger suresh said…

    My cousin recommended this blog and she was totally right keep up the fantastic work!

    Ruby Development

  • At 1:25 AM, Blogger suresh said…

    My cousin recommended this blog and she was totally right keep up the fantastic work!

    Ruby Development

  • At 3:15 AM, Anonymous Anonymous said…

    You should really be in a position to bounce right back to most of your tasks following 4 to 6 weeks., vpn port forwarding netgear wpn824. It is a recent technology over the past few decades used for growing indoor plants. The key is to eat different foods to get an assortment of vitamins. netgear wpn824v2 vpn. Some skills or spells have dual natures, and using them at the right moment could help you win a battle or two, or maybe even get you out of a tight spot so that you live to see another day! to unlock mobile phone

  • At 9:39 PM, Anonymous Anonymous said…

    You will know slowly that many things in the world are not yours. vpn vision network

  • At 10:28 AM, Anonymous Anonymous said…

    This entire system is the result of computer experts specializing in food service. connecting to vpn server. The online distance learning programs on psychology allow students to arrange the course modules according to one’s own schedule. battlefield linux server howto

  • At 12:16 AM, Anonymous Anonymous said…

    For instance, “this company prioritizes the needs of the poor”. In addition to these, there are various collaboration software that allows one to keep track of all the users and their related tasks in real time so that everybody could respond swiftly, and as you might know that document collaboration is typically online, it is becomes very easy for anyone from the team who might be physically absent in the establishment in order for him to access the information., checkpoint vpn edge. Many healthcare officials and college administrators are coming up with a number of innovative ideas about organizing student healthcare programs. microsoft access rounding


Post a Comment

Links to this post:

Create a Link

<< Home