Skip to content

Commit

Permalink
basic ripping it out from my /lib
Browse files Browse the repository at this point in the history
  • Loading branch information
Edward Hotchkiss committed Sep 27, 2011
0 parents commit 36aa2b7
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 0 deletions.
27 changes: 27 additions & 0 deletions README.md
@@ -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);
}
});
```
4 changes: 4 additions & 0 deletions index.js
@@ -0,0 +1,4 @@

module.exports = require("./lib/short");

/* EOF */
Empty file added lib/GUID
Empty file.
18 changes: 18 additions & 0 deletions lib/short-url.js
@@ -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 added models/.DS_Store
Binary file not shown.
66 changes: 66 additions & 0 deletions models/short.js
@@ -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 */
17 changes: 17 additions & 0 deletions package.json
@@ -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"
}
}

0 comments on commit 36aa2b7

Please sign in to comment.