Created
May 17, 2010 15:38
-
-
Save zh/403890 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// start: node atomizer.js | |
var sys = require('sys'), fs = require('fs'), | |
url = require("url"), | |
path = require("path"), | |
http = require("http"), | |
port = process.env.PORT || 8001, | |
counter = 0 | |
var BASE = "http://atomizer.heroku.com" | |
var HUB = "https://pubsubhubbub.appspot.com/" | |
http.createServer(function (request, response) { | |
switch (url.parse(request.url).pathname) { | |
case '/atom': | |
show_atom(request, response); | |
break; | |
default: | |
show_index(request, response); | |
break; | |
} | |
}).listen(parseInt(port)); | |
Date.prototype.toISO8601String = function (format, offset) { | |
/* accepted values for the format [1-6]: | |
1 Year: | |
YYYY (eg 1997) | |
2 Year and month: | |
YYYY-MM (eg 1997-07) | |
3 Complete date: | |
YYYY-MM-DD (eg 1997-07-16) | |
4 Complete date plus hours and minutes: | |
YYYY-MM-DDThh:mmTZD (eg 1997-07-16T19:20+01:00) | |
5 Complete date plus hours, minutes and seconds: | |
YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00) | |
6 Complete date plus hours, minutes, seconds and a decimal | |
fraction of a second | |
YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00) | |
*/ | |
if (!format) { var format = 6; } | |
if (!offset) { | |
var offset = 'Z'; | |
var date = this; | |
} else { | |
var d = offset.match(/([-+])([0-9]{2}):([0-9]{2})/); | |
var offsetnum = (Number(d[2]) * 60) + Number(d[3]); | |
offsetnum *= ((d[1] == '-') ? -1 : 1); | |
var date = new Date(Number(Number(this) + (offsetnum * 60000))); | |
} | |
var zeropad = function (num) { return ((num < 10) ? '0' : '') + num; } | |
var str = ""; | |
str += date.getUTCFullYear(); | |
if (format > 1) { str += "-" + zeropad(date.getUTCMonth() + 1); } | |
if (format > 2) { str += "-" + zeropad(date.getUTCDate()); } | |
if (format > 3) { | |
str += "T" + zeropad(date.getUTCHours()) + | |
":" + zeropad(date.getUTCMinutes()); | |
} | |
if (format > 5) { | |
var secs = Number(date.getUTCSeconds() + "." + | |
((date.getUTCMilliseconds() < 100) ? '0' : '') + | |
zeropad(date.getUTCMilliseconds())); | |
str += ":" + zeropad(secs); | |
} else if (format > 4) { str += ":" + zeropad(date.getUTCSeconds()); } | |
if (format > 3) { str += offset; } | |
return str; | |
} | |
function show_index(request, response) { | |
response.writeHead(200, {'Content-Type': 'text/html'}); | |
var output = '<html><head><title>Always Changing Feed</title></head>' + "\n" + | |
'<body>' + "\n" + | |
'<h2>Always Changing Feed</h2>' + "\n" + | |
'<p>Dummy Atom feed that always has "updates" ready (for testing purposes).</p>' + "\n" + | |
'<p><a href="http://validator.w3.org/feed/check.cgi?url=http%3A//atomizer.heroku.com/atom"><img src="http://validator.w3.org/feed/images/valid-atom.png" alt="[Valid Atom 1.0]" title="Validate my Atom 1.0 feed" /></a>' + "\n" + | |
' <a href="' + BASE + '/atom">' + BASE + '/atom</a></p>' + "\n" + | |
'<small>This application was build by <a href="http://zhware.net/">Stoyan Zhekov</a>.' + | |
'The code can be found on <a href="http://gist.github.com/403890">GitHub</a>.</small>' + "\n" + | |
'</body></html>'; | |
response.write(output); | |
response.end(); | |
} | |
function show_atom(request, response) { | |
counter++; | |
var nowStr = new Date().toISO8601String(); | |
var output = '<?xml version="1.0" encoding="UTF-8"?>' + "\n" + | |
'<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">' + | |
'<title>Always Changing Feed</title>' + | |
'<link href="' + BASE + '/atom" rel="self"></link>' + | |
'<link href="' + BASE + '/" rel="alternate"></link>' + | |
'<link href="' + HUB + '" rel="hub"></link>' + | |
'<id>' + BASE + '/</id>' + | |
'<updated>' + nowStr + '</updated>' + | |
'<author><name>Atom Generator</name></author>' + | |
'<entry>' + | |
' <title>A simple atom entry # ' + counter + '</title>' + | |
' <link href="' + BASE + '/entries/' + counter + '" rel="alternate"></link>' + | |
' <id>' + BASE + '/entries/' + counter + '</id>' + | |
' <updated>' + nowStr + '</updated>' + | |
' <summary type="html">This is a simple Atom entry at ' + nowStr + '</summary>' + | |
'</entry>' + | |
'</feed>'; | |
response.writeHead(200, {"Content-type": "application/atom+xml + \ charset=utf-8"}); | |
response.write(output); | |
response.end(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment