Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Edward Hotchkiss
committed
Sep 27, 2011
0 parents
commit 36aa2b7
Showing
7 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
|
||
short | ||
===== | ||
|
||
***Node.js Tiny URL generator with analytics*** | ||
|
||
**Setup** | ||
|
||
```bash | ||
$ npm install short | ||
``` | ||
***Integrated*** | ||
|
||
```javascript | ||
var short = require("short"); | ||
var guid = require("short").guid; | ||
|
||
var URL = "http://twitter.com/kisshotch/"; | ||
var GUID = guid(URL); | ||
|
||
// w/ callback, just for show | ||
short.save(URL, GUID, function(error) { | ||
if (error) { | ||
console.error(error); | ||
} | ||
}); | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
module.exports = require("./lib/short"); | ||
|
||
/* EOF */ |
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
|
||
var mongoose = require("mongoose"); | ||
|
||
var short = function(){}; | ||
|
||
short.find = function(hash, callback) { | ||
ShortURL.findByHash(hash, function(error, URL) { | ||
if (error) { | ||
callback(error, null); | ||
} else { | ||
callback(null, URL); | ||
}; | ||
}); | ||
}; | ||
|
||
module.exports = short; | ||
|
||
/* EOF */ |
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
|
||
/* | ||
* | ||
* Model::short | ||
* | ||
*/ | ||
|
||
var mongoose = require("mongoose"); | ||
var Schema = mongoose.Schema; | ||
var ObjectId = Schema.ObjectId; | ||
|
||
function GUID(URL) { | ||
var CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; | ||
var GUID = ""; | ||
for (var i = 0; i < 6; i++) { | ||
GUID += CHARS.charAt(Math.floor(Math.random() * CHARS.length)); | ||
}; | ||
return GUID; | ||
}; | ||
|
||
var short_schema = { | ||
id : ObjectId, | ||
URL : String, | ||
hash : { type : String }, | ||
hits : { type : Number, default: 0 }, | ||
created_at : { type : Date, default: Date.now } | ||
}; | ||
|
||
var ShortSchema = new Schema(short_schema); | ||
var Short = mongoose.model("Short", ShortSchema); | ||
|
||
Short.updateHitsById = function(id, callback) { | ||
Short.findById(id, function (error, URL) { | ||
var hits = URL.hits + 1; | ||
if (!error) { | ||
URL.hits = hits; | ||
URL.save(function(error) { | ||
callback(error); | ||
}); | ||
} | ||
}); | ||
}; | ||
|
||
Short.findByHash = function(hash, callback) { | ||
Short.find({ hash: hash }, function(error, URL) { | ||
if (error) { | ||
callback(error, null); | ||
} else { | ||
var id = URL[0]._id; | ||
Short.updateHitsById(id, function(error) { | ||
if (error) { | ||
console.error(error); | ||
} | ||
}); | ||
callback(null, URL); | ||
}; | ||
}); | ||
}; | ||
|
||
module.exports = { | ||
short_schema : short_schema, | ||
ShortSchema : ShortSchema, | ||
Short : Short | ||
}; | ||
|
||
/* EOF */ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"author": "Edward Hotchkiss <e@ingk.com>", | ||
"name": "short", | ||
"description": "generate, serve etc short urls over mongoose/mongodb/express", | ||
"version": "0.0.1", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/edwardhotchkiss/short.git" | ||
}, | ||
"main": "./index", | ||
"engines": { | ||
"node": "~v0.4.11" | ||
}, | ||
"dependencies": { | ||
"mongoose":"2.2.0" | ||
} | ||
} |