Permalink
Browse files

exitst() now returns the hash vs. trying a new hesher()

  • Loading branch information...
1 parent 4dc99e0 commit 038613a7b57013cab00833da59d974cd332cc6ed Edward Hotchkiss committed Sep 29, 2011
Showing with 55 additions and 51 deletions.
  1. +49 −44 lib/short.js
  2. +5 −6 models/ShortURL.js
  3. +1 −1 package.json
View
@@ -1,49 +1,54 @@
-(function(global, undefined) {
- var mongoose = require("mongoose");
- var base = require("base-converter");
- var ShortURL = require("../models/ShortURL.js").ShortURL;
- function hesher(URL) {
- var id = Math.floor(Math.random() * (100000 - 9999999 + 1) + 9999999);
- var hash = base.decTo62(id);
- return hash;
- };
- var short = function(){};
- short.make = function(URL, callback) {
- var hashedURL = hesher(URL);
- ShortURL.checkExists(hashedURL, function(error, count) {
- if (error) {
- callback(error, null);
+// core
+var mongoose = require("mongoose");
+var base = require("base-converter");
+
+var ShortURL = require("../models/ShortURL.js");
+
+function hesher(URL) {
+ 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.make = function(URL, callback) {
+ var hashedURL = hesher(URL);
+ ShortURL.checkExists(hashedURL, function(error, shortenedURLs) {
+ if (error) {
+ callback(error, null);
+ } else {
+ if (shortenedURLs.length === 0) {
+ var shortURLItem = new ShortURL({
+ URL : URL,
+ hash : hashedURL
+ });
+ shortURLItem.save(function(error, item) {
+ if (error) {
+ callback(error, null);
+ } else {
+ callback(null, item);
+ };
+ });
} else {
- if (count === 0) {
- var shortURLItem = new ShortURL({
- URL : URL,
- hash : hashedURL
- });
- shortURLItem.save(function(error, item) {
- if (error) {
- callback(error, null);
- } else {
- callback(null, item);
- };
- });
- } else {
- console.log("taken ...");
- short.make(URL, callback);
- }
+ callback(null, shortenedURLs[0]);
}
- });
- };
- short.get = function(hash, callback) {
- ShortURL.findByHash(hash, function(error, URL) {
- if (error) {
- callback(error, null);
- } else {
- callback(null, URL);
- };
- });
- };
- module.exports = short;
-})(global);
+ }
+ });
+};
+
+short.get = function(hash, callback) {
+ ShortURL.findByHash(hash, function(error, URL) {
+ if (error) {
+ callback(error, null);
+ } else {
+ callback(null, URL);
+ };
+ });
+};
+
+module.exports = short;
/* EOF */
View
@@ -21,18 +21,20 @@ var shortURL_schema = {
var ShortURLSchema = new Schema(shortURL_schema);
var ShortURL = mongoose.model("ShortURL", ShortURLSchema);
+// return count, if it exists, try another
ShortURL.checkExists = function(hash, callback) {
var query = ShortURL.find({});
query.where("hash", hash);
query.exec(function(error, shortenedURLS) {
if (error) {
callback(error, null);
} else {
- callback(null, shortenedURLS.length);
+ callback(null, shortenedURLS);
}
});
};
+// search for a URL by its hash
ShortURL.findByHash = function(hash, callback) {
ShortURL.find({ hash: hash }, function(error, URL) {
if (error) {
@@ -54,6 +56,7 @@ ShortURL.findByHash = function(hash, callback) {
});
};
+// increment hits
ShortURL.updateHitsById = function(id, callback) {
ShortURL.findById(id, function (error, URL) {
var hits = URL.hits + 1;
@@ -73,10 +76,6 @@ var nodeTiny = new ShortURL({
nodeTiny.save();
-module.exports = {
- shortURL_schema : shortURL_schema,
- ShortURLSchema : ShortURLSchema,
- ShortURL : ShortURL
-};
+module.exports = ShortURL;
/* EOF */
View
@@ -2,7 +2,7 @@
"author": "Edward Hotchkiss <e@ingk.com>",
"name": "short",
"description": "Generate, retieve short urls over mongoose/mongodb/express",
- "version": "0.0.7",
+ "version": "0.0.8",
"repository": {
"type": "git",
"url": "git://github.com/edwardhotchkiss/short.git"

0 comments on commit 038613a

Please sign in to comment.