Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added support for passing in custom data in the options when creating…

… a new URL. This will store any meta-information on that URL that you might want to track. A possible use for this would be an ecommerce store with a shortened URL, but you might want to track that this URL was generated for a specific micro-site or even a specific customer.
  • Loading branch information...
commit f4c8e0e36132176736bfb63c62c1e37f26e2d3d5 1 parent f446f8a
@lynchseattle lynchseattle authored
View
28 README.md
@@ -63,6 +63,34 @@ short.generate(URL, function(error, shortURL) {
}
});
+/*!
+ Generate a Shortened URL with custom data stored on the hashed URL
+ Retrieve URL based on Generated Hash and retrieve custom data
+ Make sure to use obj.data.toObject() for accessing your custom data
+ */
+
+ var short = require('short')
+ , URL = 'http://nodejs.org/',
+ , options = {length: 6, data: {'my':'value','is':2}}
+ ;
+
+short.generate(URL, options, function(error, shortURL) {
+ if (error) {
+ throw new Error(error);
+ } else {
+ short.retrieve(shortURL.hash, function(error, shortenedURLObject) {
+ if (error) {
+ throw new Error(error);
+ } else {
+ console.log('URL:', shortenedURLObject.URL);
+ console.log('hash:', shortenedURLObject.hash);
+ console.log('data:', JSON.stringify(shortenedURLObject.data.toObject()));
+ process.exit(0);
+ }
+ });
+ }
+});
+
```
## Complete Example with Express
View
7 lib/short.js
@@ -69,7 +69,8 @@ function createModel() {
*/
var generate = exports.generate = function(URL, options, callback) {
- var hashedURL;
+ var hashedURL,
+ customData;
// options takes an optional object literal
// right now it only supports an options.length argument
if (arguments.length === 2 && arguments[1] instanceof Function) {
@@ -78,6 +79,7 @@ var generate = exports.generate = function(URL, options, callback) {
}
else if (arguments.length === 3 && arguments[1] instanceof Object && arguments[2] instanceof Function) {
hashedURL = (options.length) ? hasher(URL, options.length) : hasher(URL);
+ customData = (options.data) ? options.data : null;
}
else {
throw new Error("generate requires a URL and callback function!");
@@ -86,6 +88,7 @@ var generate = exports.generate = function(URL, options, callback) {
URL : URL,
hash : hashedURL
});
+ if(customData) item.data = customData; // If we have custom data for the URl add it
item.save(function(error, item){
// tries to save to mongodb, if it exists it retries
if (error && error.code === 11000) {
@@ -99,7 +102,7 @@ var generate = exports.generate = function(URL, options, callback) {
callback(null, item);
}
});
-}
+};
/**
* @method retrieve
View
17 models/ShortURL.js
@@ -11,14 +11,15 @@ var mongoose = require('mongoose')
, ObjectId = Schema.ObjectId;
var ShortURLSchema = new Schema({
- id : { type : ObjectId },
- URL : { type : String },
- qr : { type : String },
- hash : { type : String, unique: true },
- hits : { type : Number, default: 0 },
- created_at : { type : Date, default: Date.now },
- uniques : { type : Number, default: 0},
- visitors : { type : [String]}
+ id : { 'type' : ObjectId },
+ URL : { 'type' : String },
+ qr : { 'type' : String },
+ hash : { 'type' : String, unique: true },
+ hits : { 'type' : Number, 'default': 0 },
+ created_at : { 'type' : Date, 'default': Date.now },
+ uniques : { 'type' : Number, 'default': 0},
+ visitors : { 'type' : [String]},
+ data : { 'any' : Schema.Types.Mixed }
});
var ShortURL = mongoose.model('ShortURL', ShortURLSchema);
View
34 test/index.test.js
@@ -49,7 +49,7 @@ vows.describe('general module tests').addBatch({
}
},
- // creating & retrieving
+ // creating & retrieving standard item
'when creating a short url and then retrieving it':{
topic:function() {
var self = this;
@@ -73,6 +73,38 @@ vows.describe('general module tests').addBatch({
},
'and shortURL.URL should be "http://nodejs.org/"':function(error, shortURL){
assert.equal(shortURL.URL, 'http://nodejs.org/');
+ },
+ 'and when there are custom data and options provided':{
+ topic:function() {
+ var self = this;
+ var URL = 'http://nodejs.org/';
+ var customData = {string_data:'test', num_data:2};
+ short.generate(URL, {length:5, data:customData}, function(error, URLObject) {
+ if (error) {
+ self.callback(error, null);
+ } else {
+ short.retrieve(URLObject.hash, self.callback);
+ };
+ });
+ },
+ 'there should be no errors':function(error, customDataURL){
+ assert.isNull(error);
+ },
+ 'the hashed URL should be 5 characters long':function(error, shortURL){
+ assert.strictEqual(shortURL.hash.length, 5);
+ },
+ 'the custom data should exist':function(error, shortURL){
+ assert.isNotNull(shortURL.data.toObject());
+ },
+ 'the custom data should be an object':function(error, shortURL){
+ assert.isObject(shortURL.data.toObject());
+ },
+ 'the custom data.string_data should have a value of "test"':function(error, shortURL){
+ assert.equal(shortURL.data.toObject().string_data, 'test');
+ },
+ 'the custom data.num_data should have a value of 2':function(error, shortURL){
+ assert.equal(shortURL.data.toObject().num_data, 2);
+ }
}
},
Please sign in to comment.
Something went wrong with that request. Please try again.