FABE: Feeding the readers

This part is really easy, as we have done it before, all we have to do is get the last 10 entries and inject them in a template based on an atom feed.

So, whithout further ado, here we have the feeder:

/* feeder.jss */

// Call this method whenever you update the blog to generate feed.xml

function updateFeed(){
  var model, feed, data={};
  model = require('./models.jss');
  data.entries = model.getLastEntries(10);
  data.updated = new Date(data.entries[0].date).format('c');
  feed = new Template({suffix:'xml'}).process('feeder',data);
  saveFeed('feed.xml',feed);
}

function saveFeed(name,text){
  var file = new File(name);
  file.open('w');
  file.write(text);
  file.close();
}

exports.updateFeed=updateFeed;

As we cann see, only the 'updateFeed' method will be exported, which will be called everytime we add a new entry to our blog (detailed in the previous post).

Now here we have the feeder.xml template, based on the Atom protocol:

<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>FABE</title>
  <subtitle>Fricking Awesome Blog Engine</subtitle>
  <link href="http://localhost/fabe" />
  <link href="http://localhost/fabe/feed.xml" rel="self" />
  <id>http://www.example.com/fabe</id>
  <updated>$(data.updated)</updated>
  <author><name>Anonymous</name></author>

$code( for(i in data.entries){ )
  <entry>
    <title>$(data.entries[i].title)</title>
    <id>http://localhost/fabe/view/$(data.entries[i].url)</id>
    <link href="http://localhost/fabe/view/$(data.entries[i].url)" />
    <updated>$(data.entries[i].date)</updated>
    <summary>$(data.entries[i].summary)</summary>
  </entry>
$code( } )

</feed>

Which in turn will produce feed.xml, the one to be consumed by our loyal readers.

Nothing else...