Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
Chris Lynch authored
28  README.md
Source Rendered
@@ -63,6 +63,34 @@ short.generate(URL, function(error, shortURL) {
63 63
   }
64 64
 });
65 65
 
  66
+/*!
  67
+  Generate a Shortened URL with custom data stored on the hashed URL
  68
+  Retrieve URL based on Generated Hash and retrieve custom data
  69
+  Make sure to use obj.data.toObject() for accessing your custom data
  70
+ */
  71
+
  72
+ var short = require('short')
  73
+   , URL = 'http://nodejs.org/',
  74
+   , options = {length: 6, data: {'my':'value','is':2}}
  75
+   ;
  76
+
  77
+short.generate(URL, options, function(error, shortURL) {
  78
+  if (error) {
  79
+    throw new Error(error);
  80
+  } else {
  81
+    short.retrieve(shortURL.hash, function(error, shortenedURLObject) {
  82
+      if (error) {
  83
+        throw new Error(error);
  84
+      } else {
  85
+        console.log('URL:', shortenedURLObject.URL);
  86
+        console.log('hash:', shortenedURLObject.hash);
  87
+        console.log('data:', JSON.stringify(shortenedURLObject.data.toObject()));
  88
+        process.exit(0);
  89
+      }
  90
+    });
  91
+  }
  92
+});
  93
+
66 94
 ```
67 95
 
68 96
 ## Complete Example with Express
7  lib/short.js
@@ -69,7 +69,8 @@ function createModel() {
69 69
  */
70 70
 
71 71
 var generate = exports.generate = function(URL, options, callback) {
72  
-  var hashedURL;
  72
+  var hashedURL,
  73
+      customData;
73 74
   // options takes an optional object literal
74 75
   // right now it only supports an options.length argument
75 76
   if (arguments.length === 2  && arguments[1] instanceof Function) {
@@ -78,6 +79,7 @@ var generate = exports.generate = function(URL, options, callback) {
78 79
   }
79 80
   else if (arguments.length === 3  && arguments[1] instanceof Object && arguments[2] instanceof Function) {
80 81
     hashedURL = (options.length) ? hasher(URL, options.length) : hasher(URL);
  82
+    customData = (options.data) ? options.data : null;
81 83
   }
82 84
   else {
83 85
     throw new Error("generate requires a URL and callback function!");
@@ -86,6 +88,7 @@ var generate = exports.generate = function(URL, options, callback) {
86 88
     URL : URL,
87 89
     hash : hashedURL
88 90
   });
  91
+  if(customData) item.data = customData; // If we have custom data for the URl add it
89 92
   item.save(function(error, item){
90 93
     // tries to save to mongodb, if it exists it retries
91 94
     if (error && error.code === 11000) {
@@ -99,7 +102,7 @@ var generate = exports.generate = function(URL, options, callback) {
99 102
       callback(null, item);
100 103
     }
101 104
   });
102  
-}
  105
+};
103 106
 
104 107
 /**
105 108
  * @method retrieve
17  models/ShortURL.js
@@ -11,14 +11,15 @@ var mongoose = require('mongoose')
11 11
   , ObjectId = Schema.ObjectId;
12 12
 
13 13
 var ShortURLSchema = new Schema({
14  
-  id         : { type : ObjectId },
15  
-  URL        : { type : String },
16  
-  qr         : { type : String },
17  
-  hash       : { type : String, unique: true },
18  
-  hits       : { type : Number, default: 0 },
19  
-  created_at : { type : Date, default: Date.now },
20  
-  uniques    : { type : Number, default: 0},
21  
-  visitors   : { type : [String]}
  14
+  id         : { 'type' : ObjectId },
  15
+  URL        : { 'type' : String },
  16
+  qr         : { 'type' : String },
  17
+  hash       : { 'type' : String, unique: true },
  18
+  hits       : { 'type' : Number, 'default': 0 },
  19
+  created_at : { 'type' : Date, 'default': Date.now },
  20
+  uniques    : { 'type' : Number, 'default': 0},
  21
+  visitors   : { 'type' : [String]},
  22
+  data       : { 'any'  : Schema.Types.Mixed }
22 23
 });
23 24
 
24 25
 var ShortURL = mongoose.model('ShortURL', ShortURLSchema);
34  test/index.test.js
@@ -49,7 +49,7 @@ vows.describe('general module tests').addBatch({
49 49
     }
50 50
   },
51 51
 
52  
-  // creating & retrieving
  52
+  // creating & retrieving standard item
53 53
   'when creating a short url and then retrieving it':{
54 54
     topic:function() {
55 55
       var self = this;
@@ -73,6 +73,38 @@ vows.describe('general module tests').addBatch({
73 73
     },
74 74
     'and shortURL.URL should be "http://nodejs.org/"':function(error, shortURL){
75 75
       assert.equal(shortURL.URL, 'http://nodejs.org/');
  76
+    },
  77
+    'and when there are custom data and options provided':{
  78
+      topic:function() {
  79
+        var self = this;
  80
+        var URL = 'http://nodejs.org/';
  81
+        var customData = {string_data:'test', num_data:2};
  82
+        short.generate(URL, {length:5, data:customData}, function(error, URLObject) {
  83
+          if (error) {
  84
+            self.callback(error, null);
  85
+          } else {
  86
+            short.retrieve(URLObject.hash, self.callback);
  87
+          };
  88
+        });
  89
+      },
  90
+      'there should be no errors':function(error, customDataURL){
  91
+        assert.isNull(error);
  92
+      },
  93
+      'the hashed URL should be 5 characters long':function(error, shortURL){
  94
+        assert.strictEqual(shortURL.hash.length, 5);
  95
+      },
  96
+      'the custom data should exist':function(error, shortURL){
  97
+        assert.isNotNull(shortURL.data.toObject());
  98
+      },
  99
+      'the custom data should be an object':function(error, shortURL){
  100
+        assert.isObject(shortURL.data.toObject());
  101
+      },
  102
+      'the custom data.string_data should have a value of "test"':function(error, shortURL){
  103
+        assert.equal(shortURL.data.toObject().string_data, 'test');
  104
+      },
  105
+      'the custom data.num_data should have a value of 2':function(error, shortURL){
  106
+        assert.equal(shortURL.data.toObject().num_data, 2);
  107
+      }
76 108
     }
77 109
   },
78 110
 

0 notes on commit f4c8e0e

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