From cf384cd0fdf6f208ab45c984d2d8a011d9653f66 Mon Sep 17 00:00:00 2001 From: John Crepezzi Date: Fri, 18 Nov 2011 16:45:48 -0500 Subject: [PATCH] Added random keys --- lib/document_handler.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/document_handler.js b/lib/document_handler.js index db05386..18debf0 100644 --- a/lib/document_handler.js +++ b/lib/document_handler.js @@ -9,6 +9,7 @@ var DocumentHandler = function() { // TODO implement with FS backend DocumentHandler.documents = {}; +// Handle retrieving a document DocumentHandler.prototype.handleGet = function(key, response) { if (DocumentHandler.documents[key]) { winston.verbose('retrieved document', { key: key }); @@ -22,8 +23,9 @@ DocumentHandler.prototype.handleGet = function(key, response) { } }; +// Handle adding a new Document DocumentHandler.prototype.handlePost = function(request, response) { - var key = '123'; + var key = this.randomKey(); request.on('data', function(data) { if (!DocumentHandler.documents[key]) { response.writeHead(200, { 'content-type': 'application/json' }); @@ -42,6 +44,15 @@ DocumentHandler.prototype.handlePost = function(request, response) { }); }; -// TODO block modifying +// Generate a random key +// TODO make length configurable +DocumentHandler.prototype.randomKey = function() { + var text = ''; + var keyspace = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; + for (var i = 0; i < 6; i++) { + text += keyspace.charAt(Math.floor(Math.random() * keyspace.length)); + } + return text; +}; module.exports = DocumentHandler;