Permalink
Browse files

added vows, fixed indenation

  • Loading branch information...
1 parent eef7008 commit f87ab4a17526172c9f774f3e2df019a13bc9345c Edward Hotchkiss committed Oct 6, 2011
Showing with 128 additions and 78 deletions.
  1. +3 −0 .gitignore
  2. +5 −0 Makefile
  3. +0 −1 README.md
  4. +15 −15 examples/api.js
  5. +24 −24 examples/server.js
  6. +33 −34 lib/short.js
  7. +1 −1 models/ShortURL.js
  8. +7 −3 package.json
  9. +40 −0 test/index.test.js
View
@@ -0,0 +1,3 @@
+.DS_Store
+node_modules/
+lib/.DS_Store
View
@@ -0,0 +1,5 @@
+
+test:
+ @vows test/*.test.js --spec
+
+.PHONY: test
View
@@ -30,7 +30,6 @@ short.gen(URL, function(error, shortURL) {
var URL = shortURLObject[0].URL
var hash = shortURLObject[0].hash;
console.log(URL, hash);
- process.exit(1);
};
});
}
View
@@ -1,26 +1,26 @@
var mongoose = require("mongoose");
-var short = require("../");
+var short = require("short");
mongoose.connect("mongodb://localhost/short");
var URL = "http://nodejs.org/";
short.gen(URL, function(error, shortURL) {
- if (error) {
- throw new Error(error);
- } else {
- short.get(shortURL.hash, function(error, shortURLObject) {
- if (error) {
- throw new Error(error);
- } else {
- var URL = shortURLObject[0].URL
- var hash = shortURLObject[0].hash;
- console.log(URL, hash);
- process.exit(1);
- };
- });
- }
+ if (error) {
+ throw new Error(error);
+ } else {
+ short.get(shortURL.hash, function(error, shortURLObject) {
+ if (error) {
+ throw new Error(error);
+ } else {
+ var URL = shortURLObject[0].URL
+ var hash = shortURLObject[0].hash;
+ console.log(URL, hash);
+ process.exit(1);
+ };
+ });
+ }
});
/* EOF */
View
@@ -6,30 +6,30 @@ var short = require("short");
mongoose.connect("mongodb://localhost/short");
var app = http.createServer(function(request, response) {
- var hash = request.url.slice(1);
- if (request.url === "/") {
- response.writeHead(200, { "Content-Type" : "text/html" });
- response.write("URL not found!");
- response.end();
- } else {
- short.get(hash, function(error, shortURLObject) {
- if (error) {
- console.error(error);
- } else {
- if (shortURLObject) {
- var URL = shortURLObject[0].URL;
- response.writeHead(302, {
- "Location" : URL
- });
- response.end();
- } else {
- response.writeHead(200, { "Content-Type" : "text/html" });
- response.write("URL not found!");
- response.end();
- }
- };
- });
- }
+ var hash = request.url.slice(1);
+ if (request.url === "/") {
+ response.writeHead(200, { "Content-Type" : "text/html" });
+ response.write("URL not found!");
+ response.end();
+ } else {
+ short.get(hash, function(error, shortURLObject) {
+ if (error) {
+ console.error(error);
+ } else {
+ if (shortURLObject) {
+ var URL = shortURLObject[0].URL;
+ response.writeHead(302, {
+ "Location" : URL
+ });
+ response.end();
+ } else {
+ response.writeHead(200, { "Content-Type" : "text/html" });
+ response.write("URL not found!");
+ response.end();
+ }
+ };
+ });
+ }
});
app.listen(8080);
View
@@ -5,50 +5,49 @@ var base = require("base-converter");
var ShortURL = require(__dirname + "/../models/ShortURL.js");
+// setup base62 hash
function hesher(URL) {
- var id = Math.floor(Math.random() * (100000 - 9999999 + 1) + 9999999);
- var hash = base.decTo62(id);
- return hash;
+ var id = Math.floor(Math.random() * (100000 - 9999999 + 1) + 9999999);
+ var hash = base.decTo62(id);
+ return hash;
};
var short = function(){};
// if it exists, use pre-existing
short.gen = function(URL, callback) {
- console.log("short.gen");
- var hashedURL = hesher(URL);
- ShortURL.checkExists(hashedURL, function(error, shortenedURLs) {
- console.log("shortenedURLS", shortenedURLs);
- if (error) {
- callback(error, null);
- } else {
- if (shortenedURLs.length === 0) {
- var item = new ShortURL({
- URL : URL,
- hash : hashedURL
- });
- item.save(function(error, item) {
- if (error) {
- callback(error, null);
- } else {
- callback(null, item);
- };
- });
- } else {
- short.gen(URL, callback);
- }
- }
- });
+ var hashedURL = hesher(URL);
+ ShortURL.checkExists(hashedURL, function(error, shortenedURLs) {
+ if (error) {
+ callback(error, null);
+ } else {
+ if (shortenedURLs.length === 0) {
+ var item = new ShortURL({
+ URL : URL,
+ hash : hashedURL
+ });
+ item.save(function(error, item) {
+ if (error) {
+ callback(error, null);
+ } else {
+ callback(null, item);
+ };
+ });
+ } else {
+ short.gen(URL, callback);
+ }
+ }
+ });
};
short.get = function(hash, callback) {
- ShortURL.findByHash(hash, function(error, URL) {
- if (error) {
- callback(error, null);
- } else {
- callback(null, URL);
- };
- });
+ ShortURL.findByHash(hash, function(error, URL) {
+ if (error) {
+ callback(error, null);
+ } else {
+ callback(null, URL);
+ };
+ });
};
module.exports = short;
View
@@ -30,7 +30,7 @@ ShortURL.checkExists = function(hash, callback) {
callback(error, null);
} else {
callback(null, shortenedURLS);
- }
+ };
});
};
View
@@ -2,7 +2,7 @@
"author": "Edward Hotchkiss <e@ingk.com>",
"name": "short",
"description": "Generate, retieve short urls over mongoose/mongodb/express",
- "version": "0.1.1",
+ "version": "0.1.2",
"repository": {
"type": "git",
"url": "git://github.com/edwardhotchkiss/short.git"
@@ -13,7 +13,11 @@
"node": "~v0.4.11"
},
"dependencies": {
- "mongoose":"2.2.3",
- "base-converter":"1.1.1"
+ "mongoose":"2.2.4",
+ "base-converter":"1.1.1"
+ },
+ "devDependencies" : {
+ "vows":"",
+ "should":""
}
}
View
@@ -0,0 +1,40 @@
+
+// testing
+var vows = require("vows");
+var assert = require("assert");
+var should = require("should");
+var mongoose = require("mongoose");
+
+// short core
+var short = require("../");
+
+// connect to mongodb
+mongoose.connect("mongodb://localhost/short");
+
+vows.describe("General Module Tests").addBatch({
+ "when instantiating short" : {
+ topic : function() {
+ return short;
+ },
+ "short should be a function" : function(topic) {
+ topic.should.be.a("function");
+ },
+ },
+ "when creating and retrieving a short url" : {
+ topic : function() {
+ var URL = "http://nodejs.org/";
+ short.gen(URL, this.callback);
+ },
+ "there should be no errors" : function(error, shortURL) {
+ assert.isNull(error);
+ },
+ "shortURL should be defined" : function(error, shortURL) {
+ assert.isNotNull(shortURL);
+ },
+ "and shortURL should be an object" : function(error, shortURL) {
+ shortURL.should.be.a("object");
+ }
+ }
+}).export(module);
+
+/* EOF */

0 comments on commit f87ab4a

Please sign in to comment.