Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Implemented posts RSS feed (in XML).

  • Loading branch information...
commit f68e536aa4840c0c90b5047ba4ba79c5372680a7 1 parent 92956b8
Pedro Franceschi authored December 11, 2010
1  README.markdown
Source Rendered
@@ -18,6 +18,7 @@ TODO
18 18
 ----
19 19
 * Refactor with Tests
20 20
 * Add other databases
  21
+* Implement RSS feed generation
21 22
 * Finish implementing comments
22 23
 * Implement layout management (user view and admin view)
23 24
 * Implement plugins API
9  blogode.js
@@ -36,6 +36,15 @@ app.get("/", function(req, res){
36 36
     });
37 37
 });
38 38
 
  39
+app.get("/feed", function(req, res){
  40
+    // return posts in XML format
  41
+    
  42
+    posts.getPosts(10, function (postsResult){
  43
+        posts.generatePostsXML(postsResult, function(xmlString) {
  44
+            return res.send(xmlString); 
  45
+        });
  46
+    });
  47
+});
39 48
 
40 49
 function adminLoginFilter(req, res, next) {
41 50
     // verifies if user is an admin
33  lib/posts.js
@@ -89,4 +89,37 @@ exports.destroyPost = function(id, callback) {
89 89
             callback();
90 90
         });
91 91
     });
  92
+}
  93
+
  94
+exports.generatePostsXML = function(jsonObject, callback) {
  95
+    
  96
+    var unescapedXML = '' + 
  97
+    '<?xml version="1.0" ?>' + 
  98
+    '<rss version="2.0">' + 
  99
+    '  <channel>' + 
  100
+    '    <title>The Blog Name</title>' + 
  101
+    '    <link>http://0.0.0.0:3000/</link>' + 
  102
+    '    <description>The Blog Description</description>' + 
  103
+    '    <image>' + 
  104
+    '        <url>http://www.xul.fr/xul-icon.gif</url>' + 
  105
+    '        <link>http://www.xul.fr/en/index.php</link>' + 
  106
+    '    </image>';
  107
+    
  108
+    for (var i=0; i < jsonObject.length; i++) {
  109
+        var thisObject = jsonObject[i];
  110
+        unescapedXML += '' + 
  111
+        '<item>' +
  112
+        '   <title>' + unescape(thisObject.title) + '</title>' + 
  113
+        '   <link>/' + unescape(thisObject.id) + '</link>' + 
  114
+        '   <author>' + thisObject.user_name + '</author>' +
  115
+        '   <pubDate>' + thisObject.created_at + '</pubDate>' + 
  116
+        '   <description>' + unescape(thisObject.body) + '</description>' + 
  117
+        '</item>';
  118
+    }
  119
+    
  120
+    unescapedXML += '' +
  121
+    '</channel>' +
  122
+    '</rss>';
  123
+    
  124
+    callback(unescapedXML);
92 125
 }
2  views/posts/index.ejs
... ...
@@ -1,3 +1,5 @@
  1
+<link rel="alternate" type="application/rss+xml" title="My Blog Name's RSS" href="/feed" />
  2
+
1 3
 <% if(posts != undefined) { %>
2 4
 	<% for (var i=0; i < posts.length; i++) { %>
3 5
 		<h2><a href="/<%= posts[i].id %>"><%= unescape(posts[i].title) %></a></h2>

0 notes on commit f68e536

Please sign in to comment.
Something went wrong with that request. Please try again.