Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

  • Loading branch information...
commit 038613a7b57013cab00833da59d974cd332cc6ed 1 parent 4dc99e0
authored September 29, 2011
93  lib/short.js
... ...
@@ -1,49 +1,54 @@
1 1
 
2  
-(function(global, undefined) {
3  
-	var mongoose = require("mongoose");
4  
-	var base = require("base-converter");
5  
-	var ShortURL = require("../models/ShortURL.js").ShortURL;
6  
-	function hesher(URL) {
7  
-		var id = Math.floor(Math.random() * (100000 - 9999999 + 1) + 9999999);
8  
-		var hash = base.decTo62(id);
9  
-		return hash;
10  
-	};
11  
-	var short = function(){};
12  
-	short.make = function(URL, callback) {
13  
-		var hashedURL = hesher(URL);
14  
-		ShortURL.checkExists(hashedURL, function(error, count) {
15  
-			if (error) {
16  
-				callback(error, null);
  2
+// core
  3
+var mongoose = require("mongoose");
  4
+var base = require("base-converter");
  5
+
  6
+var ShortURL = require("../models/ShortURL.js");
  7
+
  8
+function hesher(URL) {
  9
+	var id = Math.floor(Math.random() * (100000 - 9999999 + 1) + 9999999);
  10
+	var hash = base.decTo62(id);
  11
+	return hash;
  12
+};
  13
+
  14
+var short = function(){};
  15
+
  16
+// if it exists, use pre-existing
  17
+short.make = function(URL, callback) {
  18
+	var hashedURL = hesher(URL);
  19
+	ShortURL.checkExists(hashedURL, function(error, shortenedURLs) {
  20
+		if (error) {
  21
+			callback(error, null);
  22
+		} else {
  23
+			if (shortenedURLs.length === 0) {
  24
+				var shortURLItem = new ShortURL({
  25
+					URL : URL,
  26
+					hash : hashedURL
  27
+				});
  28
+				shortURLItem.save(function(error, item) {
  29
+					if (error) {
  30
+						callback(error, null);
  31
+					} else {
  32
+						callback(null, item);	
  33
+					};
  34
+				});
17 35
 			} else {
18  
-				if (count === 0) {
19  
-					var shortURLItem = new ShortURL({
20  
-						URL : URL,
21  
-						hash : hashedURL
22  
-					});
23  
-					shortURLItem.save(function(error, item) {
24  
-						if (error) {
25  
-							callback(error, null);
26  
-						} else {
27  
-							callback(null, item);	
28  
-						};
29  
-					});
30  
-				} else {
31  
-					console.log("taken ...");
32  
-					short.make(URL, callback);
33  
-				}
  36
+				callback(null, shortenedURLs[0]);
34 37
 			}
35  
-		});
36  
-	};
37  
-	short.get = function(hash, callback) {
38  
-		ShortURL.findByHash(hash, function(error, URL) {
39  
-			if (error) {
40  
-				callback(error, null);
41  
-			} else {
42  
-				callback(null, URL);
43  
-			};
44  
-		});
45  
-	};
46  
-	module.exports = short;
47  
-})(global);
  38
+		}
  39
+	});
  40
+};
  41
+
  42
+short.get = function(hash, callback) {
  43
+	ShortURL.findByHash(hash, function(error, URL) {
  44
+		if (error) {
  45
+			callback(error, null);
  46
+		} else {
  47
+			callback(null, URL);
  48
+		};
  49
+	});
  50
+};
  51
+
  52
+module.exports = short;
48 53
 
49 54
 /* EOF */
11  models/ShortURL.js
@@ -21,6 +21,7 @@ var shortURL_schema = {
21 21
 var ShortURLSchema = new Schema(shortURL_schema);
22 22
 var ShortURL = mongoose.model("ShortURL", ShortURLSchema);
23 23
 
  24
+// return count, if it exists, try another
24 25
 ShortURL.checkExists = function(hash, callback) {
25 26
 	var query = ShortURL.find({});
26 27
 	query.where("hash", hash);
@@ -28,11 +29,12 @@ ShortURL.checkExists = function(hash, callback) {
28 29
 		if (error) {
29 30
 			callback(error, null);
30 31
 		} else {
31  
-			callback(null, shortenedURLS.length);
  32
+			callback(null, shortenedURLS);
32 33
 		}
33 34
 	});
34 35
 };
35 36
 
  37
+// search for a URL by its hash
36 38
 ShortURL.findByHash = function(hash, callback) {
37 39
 	ShortURL.find({ hash: hash }, function(error, URL) {
38 40
 		if (error) {
@@ -54,6 +56,7 @@ ShortURL.findByHash = function(hash, callback) {
54 56
 	}); 
55 57
 };
56 58
 
  59
+// increment hits
57 60
 ShortURL.updateHitsById = function(id, callback) {
58 61
 	ShortURL.findById(id, function (error, URL) {
59 62
 		var hits = URL.hits + 1;
@@ -73,10 +76,6 @@ var nodeTiny = new ShortURL({
73 76
 
74 77
 nodeTiny.save();
75 78
 
76  
-module.exports = {
77  
-	shortURL_schema : shortURL_schema,
78  
-	ShortURLSchema : ShortURLSchema,
79  
-	ShortURL : ShortURL
80  
-};
  79
+module.exports = ShortURL;
81 80
 
82 81
 /* EOF */
2  package.json
@@ -2,7 +2,7 @@
2 2
     "author": "Edward Hotchkiss <e@ingk.com>",
3 3
     "name": "short",
4 4
     "description": "Generate, retieve short urls over mongoose/mongodb/express",
5  
-    "version": "0.0.7",
  5
+    "version": "0.0.8",
6 6
     "repository": {
7 7
       "type": "git",
8 8
       "url": "git://github.com/edwardhotchkiss/short.git"

0 notes on commit 038613a

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