usergrid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From snoopd...@apache.org
Subject [03/51] [abbrv] git commit: Adding helper methods and implementing Folders and Assets
Date Thu, 13 Feb 2014 14:20:47 GMT
Adding helper methods and implementing Folders and Assets


Project: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/commit/bcbbf9a7
Tree: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/tree/bcbbf9a7
Diff: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/diff/bcbbf9a7

Branch: refs/heads/two-dot-o
Commit: bcbbf9a740aa9bfe40d56740ddb3c248ba22f1cb
Parents: 07f7e45
Author: ryan bridges <rbridges@apigee.com>
Authored: Sat Feb 1 00:44:00 2014 -0500
Committer: ryan bridges <rbridges@apigee.com>
Committed: Sat Feb 1 00:44:00 2014 -0500

----------------------------------------------------------------------
 sdks/html5-javascript/Gruntfile.js              |   2 +-
 sdks/html5-javascript/lib/Asset.js              | 126 ++++-
 sdks/html5-javascript/lib/Collection.js         |  13 +
 sdks/html5-javascript/lib/Entity.js             |  36 +-
 sdks/html5-javascript/lib/Folder.js             | 152 ++++-
 sdks/html5-javascript/lib/Usergrid.js           |  45 +-
 sdks/html5-javascript/tests/mocha/test.js       | 548 ++++++++++++++-----
 .../tests/resources/js/blanket_mocha.min.js     |   2 +-
 sdks/html5-javascript/usergrid.js               | 407 +++++++++++++-
 sdks/html5-javascript/usergrid.min.js           |   5 +-
 10 files changed, 1125 insertions(+), 211 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/bcbbf9a7/sdks/html5-javascript/Gruntfile.js
----------------------------------------------------------------------
diff --git a/sdks/html5-javascript/Gruntfile.js b/sdks/html5-javascript/Gruntfile.js
index 703c6af..0401e24 100644
--- a/sdks/html5-javascript/Gruntfile.js
+++ b/sdks/html5-javascript/Gruntfile.js
@@ -1,5 +1,5 @@
 module.exports = function(grunt) {
-  var files = ['lib/Usergrid.js', 'lib/Client.js', 'lib/Entity.js', 'lib/Collection.js',
'lib/Group.js', 'lib/Counter.js'];
+  var files = ['lib/Usergrid.js', 'lib/Client.js', 'lib/Entity.js', 'lib/Collection.js',
'lib/Group.js', 'lib/Counter.js', 'lib/Folder.js', 'lib/Asset.js', 'lib/Error.js'];
   var tests = [ 'tests/mocha/index.html','tests/mocha/test_*.html' ];
    // Project configuration.
   grunt.initConfig({

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/bcbbf9a7/sdks/html5-javascript/lib/Asset.js
----------------------------------------------------------------------
diff --git a/sdks/html5-javascript/lib/Asset.js b/sdks/html5-javascript/lib/Asset.js
index 4bcff61..eaaf25a 100644
--- a/sdks/html5-javascript/lib/Asset.js
+++ b/sdks/html5-javascript/lib/Asset.js
@@ -1,26 +1,41 @@
+if (!XMLHttpRequest.prototype.sendAsBinary) {
+	XMLHttpRequest.prototype.sendAsBinary = function(sData) {
+		var nBytes = sData.length,
+			ui8Data = new Uint8Array(nBytes);
+		for (var nIdx = 0; nIdx < nBytes; nIdx++) {
+			ui8Data[nIdx] = sData.charCodeAt(nIdx) & 0xff;
+		}
+		/* send as ArrayBufferView...: */
+		this.send(ui8Data);
+		/* ...or as ArrayBuffer (legacy)...: this.send(ui8Data.buffer); */
+	};
+}
+
+
 /*
  *  A class to model a Usergrid asset.
  *
  *  @constructor
- *  @param {object} options {name:"photo.jpg", path:"/user/uploads", "content-type":"image/jpeg",
owner:"F01DE600-0000-0000-0000-000000000000" } 
+ *  @param {object} options {name:"photo.jpg", path:"/user/uploads", "content-type":"image/jpeg",
owner:"F01DE600-0000-0000-0000-000000000000" }
  *  @returns {callback} callback(err, asset)
  */
 Usergrid.Asset = function(options, callback) {
-	var self=this, okToSave=true, messages=[];
+	var self = this,
+		okToSave = true,
+		messages = [];
 	self._client = options.client;
 	self._data = options.data || {};
-	["name","owner","path"].forEach(function(required){
-		if(!(required in this._data)){
-			messages.push(required+" is a required data element.");
-			okToSave=false;
+	self._data.type = "assets";
+	["name", "owner", "path"].forEach(function(required) {
+		if (!(required in self._data)) {
+			messages.push(required + " is a required data element.");
+			okToSave = false;
 		}
 	});
-	if(okToSave){
+	if (okToSave) {
 		self.save(callback);
-	}else{
-		if(callback && typeof(callback) === 'function') {
-			callback.call(self, !okToSave, {error_description:messages.join("\n")});
-		}
+	} else {
+		doCallback(callback, [!okToSave, new Usergrid.Error(messages.join("\n"))], self);
 	}
 };
 /*
@@ -31,26 +46,89 @@ Usergrid.Asset.prototype = new Usergrid.Entity();
 /*
  *  Add an asset to a folder.
  *
- *  @constructor
- *  @param {object} options {folder:"F01DE600-0000-0000-0000-000000000000"} 
+ *  @method connect
+ *  @public
+ *  @param {object} options {folder:"F01DE600-0000-0000-0000-000000000000"}
  *  @returns {callback} callback(err, asset)
  */
-Usergrid.Asset.prototype.addToFolder=function(options, callback){
-	var self=this, error=null;
-	if(('folder' in options) && isUUID (options.folder)){
+
+Usergrid.Asset.prototype.addToFolder = function(options, callback) {
+	var self = this,
+		error = null;
+	if (('folder' in options) && isUUID(options.folder)) {
 		//we got a valid UUID
-		var folder = Usergrid.Folder({uuid:options.folder}, function(err, folder){
-			if(err){
+		var folder = Usergrid.Folder({
+			uuid: options.folder
+		}, function(err, folder) {
+			if (err) {
 				return callback.call(self, err, folder);
 			}
-			var endpoint=["folders", folder.get("uuid"), "assets", self.get("uuid")].join('/');
+			var endpoint = ["folders", folder.get("uuid"), "assets", self.get("uuid")].join('/');
+			var options = {
+				method: 'POST',
+				endpoint: endpoint
+			};
+			this._client.request(options, function(err, data) {
+				if (typeof(callback) === 'function') {
+					doCallback(callback, [!okToSave, new Usergrid.Error(data)], self);
+					callback(err, data);
+				}
+			});
 		});
-
-	}
-	if(callback && typeof(callback) === 'function') {
-		callback.call(self, false, self);
+	} else {
+		if (callback && typeof(callback) === 'function') {
+			callback.call(self, true, {
+				error_description: "folder not specified"
+			});
+		}
 	}
 };
 
-Usergrid.Asset.prototype.addToFolder=function(options, callback){};
+Usergrid.Asset.prototype.upload = function(data, callback) {
+	if (!(window.File && window.FileReader && window.FileList && window.Blob))
{
+		return doCallback(callback, [true, new Usergrid.Error('The File APIs are not fully supported
by your browser.')], self);
+	}
+	var self = this;
+	var endpoint = [this._client.URI, this._client.orgName, this._client.appName, "assets",
self.get("uuid"), 'data'].join('/'); //self._client.buildAssetURL(self.get("uuid"));
+
+	var xhr = new XMLHttpRequest();
+	xhr.open("POST", endpoint, true);
+	xhr.onerror = function(err) {
+		//callback(true, err);
+		doCallback(callback, [true, new Usergrid.Error('The File APIs are not fully supported by
your browser.')], self)
+	};
+	xhr.onload = function(ev) {
+		if (xhr.status >= 300) {
+			doCallback(callback, [null, JSON.parse(xhr.responseText)], self)
+		} else {
+			doCallback(callback, [null, self], self)
+		}
+	};
+	var fr = new FileReader();
+	fr.onload = function() {
+		var binary = fr.result;
+		xhr.overrideMimeType('application/octet-stream');
+		setTimeout(function() {
+			xhr.sendAsBinary(binary);
+		}, 1000);
+	};
+	fr.readAsBinaryString(data);
+}
+Usergrid.Asset.prototype.download = function(callback) {
+	var self = this;
+	var endpoint = [this._client.URI, this._client.orgName, this._client.appName, "assets",
self.get("uuid"), 'data'].join('/');
+	var xhr = new XMLHttpRequest();
+	xhr.open("GET", endpoint, true);
+	xhr.responseType = "blob";
+	xhr.onload = function(ev) {
+		var blob = xhr.response;
+		//callback(null, blob);
+		doCallback(callback, [false, blob], self)
+	};
+	xhr.onerror = function(err) {
+		callback(true, err);
+		doCallback(callback, [true, new Usergrid.Error(err)], self)
+	};
 
+	xhr.send();
+}

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/bcbbf9a7/sdks/html5-javascript/lib/Collection.js
----------------------------------------------------------------------
diff --git a/sdks/html5-javascript/lib/Collection.js b/sdks/html5-javascript/lib/Collection.js
index 57fdf06..5173fc9 100644
--- a/sdks/html5-javascript/lib/Collection.js
+++ b/sdks/html5-javascript/lib/Collection.js
@@ -44,6 +44,19 @@ Usergrid.Collection = function(options, callback) {
 
 
 /*
+ *  method to determine whether or not the passed variable is a Usergrid Collection
+ *
+ *  @method isCollection
+ *  @public
+ *  @params {any} obj - any variable
+ *  @return {boolean} Returns true or false
+ */
+Usergrid.isCollection = function(obj){
+  return (obj && obj instanceof Usergrid.Collection);
+}
+
+
+/*
  *  gets the data from the collection object for serialization
  *
  *  @method serialize

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/bcbbf9a7/sdks/html5-javascript/lib/Entity.js
----------------------------------------------------------------------
diff --git a/sdks/html5-javascript/lib/Entity.js b/sdks/html5-javascript/lib/Entity.js
index e070d8a..c4fe016 100644
--- a/sdks/html5-javascript/lib/Entity.js
+++ b/sdks/html5-javascript/lib/Entity.js
@@ -13,6 +13,31 @@ Usergrid.Entity = function(options) {
 };
 
 /*
+ *  method to determine whether or not the passed variable is a Usergrid Entity
+ *
+ *  @method isEntity
+ *  @public
+ *  @params {any} obj - any variable
+ *  @return {boolean} Returns true or false
+ */
+Usergrid.isEntity = function(obj){
+  return (obj && obj instanceof Usergrid.Entity);
+}
+
+/*
+ *  method to determine whether or not the passed variable is a Usergrid Entity
+ *  That has been saved.
+ *
+ *  @method isPersistedEntity
+ *  @public
+ *  @params {any} obj - any variable
+ *  @return {boolean} Returns true or false
+ */
+Usergrid.isPersistedEntity = function(obj){
+  return (isEntity(obj) && isUUID(obj.get('uuid')));
+}
+
+/*
  *  returns a serialized version of the entity object
  *
  *  Note: use the client.restoreEntity() function to restore
@@ -86,14 +111,13 @@ Usergrid.Entity.prototype.save = function (callback) {
   var self = this;
   var data = {};
   var entityData = this.get();
-    var password = this.get('password');
-    var oldpassword = this.get('oldpassword');
-    var newpassword = this.get('newpassword');
+  var password = this.get('password');
+  var oldpassword = this.get('oldpassword');
+  var newpassword = this.get('newpassword');
+  var SYSTEM_PROPERTIES=['metadata','created','modified','oldpassword','newpassword','type','activated','uuid'];
   //remove system specific properties
   for (var item in entityData) {
-    if (item === 'metadata' || item === 'created' || item === 'modified' ||
-          item === 'oldpassword' || item === 'newpassword' || //old and new pw not added
to data
-      item === 'type' || item === 'activated' || item === 'uuid') {
+    if (SYSTEM_PROPERTIES.indexOf(item) !== -1) {
       continue;
     }
     data[item] = entityData[item];

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/bcbbf9a7/sdks/html5-javascript/lib/Folder.js
----------------------------------------------------------------------
diff --git a/sdks/html5-javascript/lib/Folder.js b/sdks/html5-javascript/lib/Folder.js
index 9c7626c..5ecf69e 100644
--- a/sdks/html5-javascript/lib/Folder.js
+++ b/sdks/html5-javascript/lib/Folder.js
@@ -2,28 +2,156 @@
  *  A class to model a Usergrid folder.
  *
  *  @constructor
- *  @param {object} options {name:"MyPhotos", path:"/user/uploads", owner:"00000000-0000-0000-0000-000000000000"
} 
+ *  @param {object} options {name:"MyPhotos", path:"/user/uploads", owner:"00000000-0000-0000-0000-000000000000"
}
  *  @returns {callback} callback(err, folder)
  */
 Usergrid.Folder = function(options, callback) {
-	var self=this, okToSave=true, messages=[];
+	var self = this,
+		messages = [];
+	console.log("FOLDER OPTIONS", options);
 	self._client = options.client;
 	self._data = options.data || {};
-	["name","owner","path"].forEach(function(required){
-		if(!(required in this._data)){
-			messages.push(required+" is a required data element.");
-			okToSave=false;
+	self._data.type = "folders";
+	["name", "owner", "path"].forEach(function(required) {
+		if (!(required in self._data)) {
+			messages.push(required + " is a required data element.");
+			throw required + " is a required data element.";
 		}
 	});
-	if(okToSave){
-		self.save(callback);
-	}else{
-		if(callback && typeof(callback) === 'function') {
-			callback.call(self, !okToSave, {error_description:messages.join("\n")});
-		}
+	if (messages.length) {
+		return doCallback(callback, [true, messages.join("\n")], self)
 	}
+	var errors = ['service_resource_not_found', 'no_name_specified', 'null_pointer'];
+	self.save(function(err, data) {
+		if (err) {
+			doCallback(callback, [true, data], self);
+		} else {
+			console.log(data.entities[0]);
+			self.set(data.entities[0]);
+			doCallback(callback, [false, self], self);
+		}
+	});
 };
 /*
  *  Inherit from Usergrid.Entity.
  */
 Usergrid.Folder.prototype = new Usergrid.Entity();
+
+
+/*
+ *  fetch the folder.
+ *
+ *  @method fetch
+ *  @public
+ *  @param {function} callback(err, self)
+ *  @returns {callback} callback(err, self)
+ */
+Usergrid.Folder.prototype.fetch = function(callback) {
+	var self = this;
+	Usergrid.Entity.prototype.fetch.call(self, function(err, data) {
+		console.log("self", self.get());
+		console.log("data", data);
+		if (!err) {
+			self.getAssets(function(err, data) {
+				if (err) {
+					doCallback(callback, [true, new Usergrid.Error(data)], self);
+				} else {
+					doCallback(callback, [null, self], self);
+				}
+			});
+		} else {
+			doCallback(callback, [true, new Usergrid.Error(data)], self)
+		}
+	})
+};
+/*
+ *  Add an asset to the folder.
+ *
+ *  @method addAsset
+ *  @public
+ *  @param {object} options {asset:(uuid || Usergrid.Asset || {name:"photo.jpg", path:"/user/uploads",
"content-type":"image/jpeg", owner:"F01DE600-0000-0000-0000-000000000000" }) }
+ *  @returns {callback} callback(err, folder)
+ */
+Usergrid.Folder.prototype.addAsset = function(options, callback) {
+	var self = this;
+	if (('asset' in options)) {
+		var asset = null;
+		switch (typeof options.asset) {
+			case 'object':
+				asset = options.asset;
+				if (!(asset instanceof Usergrid.Entity)) {
+					asset = new Usergrid.Asset(asset);
+				}
+				break;
+			case 'string':
+				if (isUUID(options.asset)) {
+					asset = new Usergrid.Asset({
+						client: self._client,
+						data: {
+							uuid: options.asset,
+							type: "assets"
+						}
+					});
+				}
+				break;
+		}
+		if (asset && asset instanceof Usergrid.Entity) {
+			asset.fetch(function(err, data) {
+				if (err) {
+					doCallback(callback, [err, new Usergrid.Error(data)], self)
+				} else {
+					var endpoint = ["folders", self.get("uuid"), "assets", asset.get("uuid")].join('/');
+					var options = {
+						method: 'POST',
+						endpoint: endpoint
+					};
+					self._client.request(options, callback);
+				}
+			})
+		}
+	} else {
+		//nothing to add
+		doCallback(callback, [true, {
+			error_description: "No asset specified"
+		}], self)
+	}
+};
+Usergrid.Folder.prototype.removeAsset = function(options, callback) {
+	var self = this;
+	if (('asset' in options)) {
+		var asset = null;
+		switch (typeof options.asset) {
+			case 'object':
+				asset = options.asset;
+				break;
+			case 'string':
+				if (isUUID(options.asset)) {
+					asset = new Usergrid.Asset({
+						client: self._client,
+						data: {
+							uuid: options.asset,
+							type: "assets"
+						}
+					});
+				}
+				break;
+		}
+		if (asset && asset !== null) {
+			var endpoint = ["folders", self.get("uuid"), "assets", asset.get("uuid")].join('/');
+			self._client.request({
+				method: 'DELETE',
+				endpoint: endpoint
+			}, callback);
+		}
+	} else {
+		//nothing to add
+		doCallback(callback, [true, {
+			error_description: "No asset specified"
+		}], self)
+	}
+};
+Usergrid.Folder.prototype.getAssets = function(callback) {
+	return this.getConnections("assets", callback);
+};
+
+//Usergrid.Folder.prototype.getAssets = function(options, callback){};

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/bcbbf9a7/sdks/html5-javascript/lib/Usergrid.js
----------------------------------------------------------------------
diff --git a/sdks/html5-javascript/lib/Usergrid.js b/sdks/html5-javascript/lib/Usergrid.js
index 89507cf..1da4241 100644
--- a/sdks/html5-javascript/lib/Usergrid.js
+++ b/sdks/html5-javascript/lib/Usergrid.js
@@ -41,8 +41,8 @@ Usergrid.USERGRID_SDK_VERSION = '0.10.07';
  * @param {string} uuid The string to test
  * @returns {Boolean} true if string is uuid
  */
-function isUUID (uuid) {
-  var uuidValueRegex = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/;
+var uuidValueRegex = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/;
+function isUUID(uuid) {
   if (!uuid) {
     return false;
   }
@@ -57,7 +57,7 @@ function isUUID (uuid) {
  *  @params {object} params - an object of name value pairs that will be urlencoded
  *  @return {string} Returns the encoded string
  */
-function encodeParams (params) {
+function encodeParams(params) {
   var tail = [];
   var item = [];
   var i;
@@ -85,3 +85,42 @@ function encodeParams (params) {
   }
   return tail.join("&");
 }
+
+/*
+ *  method to determine whether or not the passed variable is a function
+ *
+ *  @method isFunction
+ *  @public
+ *  @params {any} f - any variable
+ *  @return {boolean} Returns true or false
+ */
+function isFunction(f) {
+  return (f && f !== null && typeof(f) === 'function');
+}
+
+/*
+ *  a safe wrapper for executing a callback
+ *
+ *  @method doCallback
+ *  @public
+ *  @params {Function} callback - the passed-in callback method
+ *  @params {Array} params - an array of arguments to pass to the callback
+ *  @params {Object} context - an optional calling context for the callback
+ *  @return Returns whatever would be returned by the callback. or false.
+ */
+function doCallback(callback, params, context) {
+  var returnValue;
+  if (isFunction(callback)) {
+    if (!params) params = [];
+    if (!context) context = this;
+    params.push(context);
+    try {
+      returnValue = callback.apply(context, params);
+    } catch (ex) {
+      if (console && console.error) {
+        console.error("Callback error:", ex);
+      }
+    }
+  }
+  return returnValue;
+}

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/bcbbf9a7/sdks/html5-javascript/tests/mocha/test.js
----------------------------------------------------------------------
diff --git a/sdks/html5-javascript/tests/mocha/test.js b/sdks/html5-javascript/tests/mocha/test.js
index b75f187..86ce5ca 100644
--- a/sdks/html5-javascript/tests/mocha/test.js
+++ b/sdks/html5-javascript/tests/mocha/test.js
@@ -1,12 +1,11 @@
-
 /*
 	Creates a generic usergrid client with logging and buildCurl disabled
 
  */
-function getClient(){
+function getClient() {
 	return new Usergrid.Client({
-		orgName:'yourorgname',
-		appName:'sandbox',
+		orgName: 'rbridges',
+		appName: 'sandbox',
 		logging: false, //optional - turn on logging, off by default
 		buildCurl: true //optional - turn on curl commands, off by default
 	});
@@ -15,305 +14,388 @@ function getClient(){
 	A convenience function that will test for the presence of an API error
 	and run any number of additional tests
  */
-function usergridTestHarness(err, data, done, tests, ignoreError){
-	if(!ignoreError) assert(!err, data.error_description);
-	if(tests){
-		if("function"===typeof tests){
+function usergridTestHarness(err, data, done, tests, ignoreError) {
+	if (!ignoreError) assert(!err, data.error_description);
+	if (tests) {
+		if ("function" === typeof tests) {
 			tests(err, data);
-		}else if(tests.length){
-			tests.forEach(function(test){
-				if("function"===typeof test){
+		} else if (tests.length) {
+			tests.forEach(function(test) {
+				if ("function" === typeof test) {
 					test(err, data);
 				}
 			})
 		}
-	}	
+	}
 	done();
 }
-describe('Usergrid', function(){
+describe('Usergrid', function() {
 	var client = getClient();
-
-	before(function(done){
-    	//Make sure our dog doesn't already exist
-		client.request({method:'DELETE',endpoint:'users/fred'}, function (err, data) {
-			done();
-	    });
-  	});
-	describe('Usergrid CRUD', function(){
+	describe('Usergrid CRUD', function() {
+		before(function(done) {
+			//Make sure our dog doesn't already exist
+			client.request({
+				method: 'DELETE',
+				endpoint: 'users/fred'
+			}, function(err, data) {
+				done();
+			});
+		});
 		var options = {
-			method:'GET',
-			endpoint:'users'
+			method: 'GET',
+			endpoint: 'users'
 		};
-		it('should CREATE a new user', function(done){
-			client.request({method:'POST',endpoint:'users', body:{ username:'fred', password:'secret'
}}, function (err, data) {
+		it('should CREATE a new user', function(done) {
+			client.request({
+				method: 'POST',
+				endpoint: 'users',
+				body: {
+					username: 'fred',
+					password: 'secret'
+				}
+			}, function(err, data) {
 				usergridTestHarness(err, data, done, [
-					function(err, data){assert(true)}
+
+					function(err, data) {
+						assert(true)
+					}
 				]);
-		    });
+			});
 		});
-		it('should RETRIEVE an existing user', function(done){
-			client.request({method:'GET',endpoint:'users/fred', body:{}}, function (err, data) {
+		it('should RETRIEVE an existing user', function(done) {
+			client.request({
+				method: 'GET',
+				endpoint: 'users/fred',
+				body: {}
+			}, function(err, data) {
 				usergridTestHarness(err, data, done, [
-					function(err, data){assert(true)}
+
+					function(err, data) {
+						assert(true)
+					}
 				]);
-		    });
+			});
 		});
-		it('should UPDATE an existing user', function(done){
-			client.request({method:'PUT',endpoint:'users/fred', body:{ newkey:'newvalue' }}, function
(err, data) {
+		it('should UPDATE an existing user', function(done) {
+			client.request({
+				method: 'PUT',
+				endpoint: 'users/fred',
+				body: {
+					newkey: 'newvalue'
+				}
+			}, function(err, data) {
 				usergridTestHarness(err, data, done, [
-					function(err, data){assert(true)}
+
+					function(err, data) {
+						assert(true)
+					}
 				]);
-		    });
+			});
 		});
-		it('should DELETE the user from the database', function(done){
-			client.request({method:'DELETE',endpoint:'users/fred'}, function (err, data) {
+		it('should DELETE the user from the database', function(done) {
+			client.request({
+				method: 'DELETE',
+				endpoint: 'users/fred'
+			}, function(err, data) {
 				usergridTestHarness(err, data, done, [
-					function(err, data){assert(true)}
+
+					function(err, data) {
+						assert(true)
+					}
 				]);
-		    });
+			});
 		});
 	});
-	describe('Usergrid REST', function(){
-		it('should return a list of users', function(done){
-			client.request({method:'GET',endpoint:'users'}, function (err, data) {
+	describe('Usergrid REST', function() {
+		it('should return a list of users', function(done) {
+			client.request({
+				method: 'GET',
+				endpoint: 'users'
+			}, function(err, data) {
 				usergridTestHarness(err, data, done, [
-					function(err, data){assert(data.entities.length >=0)}
+
+					function(err, data) {
+						assert(data.entities.length >= 0);
+						console.log(JSON.stringify(data))
+					}
 				]);
-		    });
+			});
 		});
-		it('should return no entities when an endpoint does not exist', function(done){
-			client.request({method:'GET',endpoint:'nonexistantendpoint'}, function (err, data) {
+		it('should return no entities when an endpoint does not exist', function(done) {
+			client.request({
+				method: 'GET',
+				endpoint: 'nonexistantendpoint'
+			}, function(err, data) {
 				usergridTestHarness(err, data, done, [
-					function(err, data){assert(data.entities.length === 0)}
+
+					function(err, data) {
+						assert(data.entities.length === 0)
+					}
 				]);
-		    });
+			});
 		});
 	});
-	describe('Usergrid Entity', function(){
+	describe('Usergrid Entity', function() {
 		var dog;
-		before(function(done){
-	    	//Make sure our dog doesn't already exist
-	    	client.request({method:'DELETE',endpoint:'dogs/Rocky'}, function (err, data) {
-	    		assert(true);
-	    		done();
-		    });
-	  	});
-		it('should CREATE a new dog', function(done){
+		before(function(done) {
+			//Make sure our dog doesn't already exist
+			client.request({
+				method: 'DELETE',
+				endpoint: 'dogs/Rocky'
+			}, function(err, data) {
+				assert(true);
+				done();
+			});
+		});
+		it('should CREATE a new dog', function(done) {
 			var options = {
-				type:'dogs',
-				name:'Rocky'
+				type: 'dogs',
+				name: 'Rocky'
 			}
 
-			client.createEntity(options, function (err, data) {
+			client.createEntity(options, function(err, data) {
 				assert(!err, "dog not created");
-				dog=data;
+				dog = data;
 				done();
 			});
 		});
-		it('should RETRIEVE the dog', function(done){
-			if(!dog){
+		it('should RETRIEVE the dog', function(done) {
+			if (!dog) {
 				assert(false, "dog not created");
 				done();
 				return;
 			}
 			//once the dog is created, you can set single properties:
-			dog.fetch(function(err){
+			dog.fetch(function(err) {
 				assert(!err, "dog not fetched");
 				done();
 			});
 		});
-		it('should UPDATE the dog', function(done){
-			if(!dog){
+		it('should UPDATE the dog', function(done) {
+			if (!dog) {
 				assert(false, "dog not created");
 				done();
 				return;
 			}
 			//once the dog is created, you can set single properties:
-			dog.set('breed','Dinosaur');
+			dog.set('breed', 'Dinosaur');
 
 			//the set function can also take a JSON object:
 			var data = {
-				master:'Fred',
-				state:'hungry'
+				master: 'Fred',
+				state: 'hungry'
 			}
 			//set is additive, so previously set properties are not overwritten
 			dog.set(data);
 
 			//finally, call save on the object to save it back to the database
-			dog.save(function(err){
+			dog.save(function(err) {
 				assert(!err, "dog not saved");
 				done();
 			});
 		});
-		it('should DELETE the dog', function(done){
-			if(!dog){
+		it('should DELETE the dog', function(done) {
+			if (!dog) {
 				assert(false, "dog not created");
 				done();
 				return;
 			}
 			//once the dog is created, you can set single properties:
-			dog.destroy(function(err){
+			dog.destroy(function(err) {
 				assert(!err, "dog not removed");
 				done();
 			});
 		});
-
 	});
-	describe('Usergrid Collections', function(){
+	describe('Usergrid Collections', function() {
 		var client = getClient();
-		var dog, dogs={};
-		function loop(done){
-			while(dogs.hasNextEntity()) {
+		var dog, dogs = {};
+
+		function loop(done) {
+			while (dogs.hasNextEntity()) {
 				//get a reference to the dog
 				dog = dogs.getNextEntity();
 				console.log(dog.get('name'));
 			}
-			if(done)done();
+			if (done) done();
 		}
-		before(function(done){
-	    	//Make sure our dog doesn't already exist
+		before(function(done) {
+			//Make sure our dog doesn't already exist
 			var options = {
-				type:'dogs',
-				qs:{limit:50} //limit statement set to 50
+				type: 'dogs',
+				qs: {
+					limit: 50
+				} //limit statement set to 50
 			}
 
-			client.createCollection(options, function (err, dogs) {
+			client.createCollection(options, function(err, dogs) {
 				if (!err) {
-					assert(!err, "could not retrieve list of dogs: "+dogs.error_description);
+					assert(!err, "could not retrieve list of dogs: " + dogs.error_description);
 					//we got 50 dogs, now display the Entities:
 					//do doggy cleanup
-					if(dogs.hasNextEntity()){
-						while(dogs.hasNextEntity()) {
+					if (dogs.hasNextEntity()) {
+						while (dogs.hasNextEntity()) {
 							//get a reference to the dog
 							var dog = dogs.getNextEntity();
 							//notice('removing dog ' + dogname + ' from database');
 							dog.destroy(function(err, data) {
-								assert(!err, dog.get('name')+" not removed: "+data.error_description);
-								if(!dogs.hasNextEntity()){
+								assert(!err, dog.get('name') + " not removed: " + data.error_description);
+								if (!dogs.hasNextEntity()) {
 									done();
 								}
 							});
 						}
-					}else{
+					} else {
 						done();
 					}
 				}
 			});
-	  	});
-		before(function(done){
+		});
+		before(function(done) {
 			this.timeout(10000);
-			var totalDogs=30;
-			Array.apply(0, Array(totalDogs)).forEach(function (x, y) { 
-				var dogNum=y+1;
+			var totalDogs = 30;
+			Array.apply(0, Array(totalDogs)).forEach(function(x, y) {
+				var dogNum = y + 1;
 				var options = {
-					type:'dogs',
-					name:'dog'+dogNum,
-					index:y
+					type: 'dogs',
+					name: 'dog' + dogNum,
+					index: y
 				}
-				client.createEntity(options, function (err, dog) {
-					assert(!err, " not created: "+dog.error_description);
-					if(dogNum===totalDogs){
+				client.createEntity(options, function(err, dog) {
+					assert(!err, " not created: " + dog.error_description);
+					if (dogNum === totalDogs) {
 						done();
 					}
 				});
 			})
 		});
-		it('should CREATE a new dogs collection', function(done){
+		it('should CREATE a new dogs collection', function(done) {
 			var options = {
-				type:'dogs',
-				qs:{ql:'order by index'}
+				type: 'dogs',
+				qs: {
+					ql: 'order by index'
+				}
 			}
 
-			client.createCollection(options, function (err, data) {
-				assert(!err, "could not create dogs collection: "+data.error_description);
-				dogs=data;
+			client.createCollection(options, function(err, data) {
+				assert(!err, "could not create dogs collection: " + data.error_description);
+				dogs = data;
 				done();
 			});
 		});
-		it('should RETRIEVE dogs from the collection', function(done){
+		it('should RETRIEVE dogs from the collection', function(done) {
 			loop(done);
 		});
-		it('should RETRIEVE the next page of dogs from the collection', function(done){
-			if(dogs.hasNextPage()){
-				dogs.getNextPage(function(err){loop(done);});
-			}else{
+		it('should RETRIEVE the next page of dogs from the collection', function(done) {
+			if (dogs.hasNextPage()) {
+				dogs.getNextPage(function(err) {
+					loop(done);
+				});
+			} else {
 				done();
 			}
 		});
-		it('should RETRIEVE the previous page of dogs from the collection', function(done){
-			if(dogs.hasPreviousPage()){
-				dogs.getPreviousPage(function(err){loop(done);});
-			}else{
+		it('should RETRIEVE the previous page of dogs from the collection', function(done) {
+			if (dogs.hasPreviousPage()) {
+				dogs.getPreviousPage(function(err) {
+					loop(done);
+				});
+			} else {
 				done();
 			}
 		});
 	});
-	describe('Usergrid Counters', function(){
+	describe('Usergrid Counters', function() {
 		var counter;
-		var MINUTE=1000*60;
-		var HOUR=MINUTE*60;
-		var time=Date.now()-HOUR;
+		var MINUTE = 1000 * 60;
+		var HOUR = MINUTE * 60;
+		var time = Date.now() - HOUR;
 
-		it('should CREATE a counter', function(done){
-			counter = new Usergrid.Counter({client:client, data:{category:'mocha_test', timestamp:time,
name:"test", counters:{test:0,test_counter:0}}}, function(err, data){
+		it('should CREATE a counter', function(done) {
+			counter = new Usergrid.Counter({
+				client: client,
+				data: {
+					category: 'mocha_test',
+					timestamp: time,
+					name: "test",
+					counters: {
+						test: 0,
+						test_counter: 0
+					}
+				}
+			}, function(err, data) {
 				assert(!err, data.error_description);
 				console.log(data);
 				done();
 			});
 		});
-		it('should save a counter', function(done){
-			counter.save(function(err, data){
+		it('should save a counter', function(done) {
+			counter.save(function(err, data) {
 				assert(!err, data.error_description);
 				console.log(data);
 				done();
 			});
 		});
-		it('should reset a counter', function(done){
-			time+=MINUTE*10
+		it('should reset a counter', function(done) {
+			time += MINUTE * 10
 			counter.set("timestamp", time);
-			counter.reset({name:'test'}, function(err, data){
+			counter.reset({
+				name: 'test'
+			}, function(err, data) {
 				assert(!err, data.error_description);
 				console.log(data);
 				done();
 			});
 		});
-		it("should increment 'test' counter", function(done){
-			time+=MINUTE*10
+		it("should increment 'test' counter", function(done) {
+			time += MINUTE * 10
 			counter.set("timestamp", time);
-			counter.increment({name:'test', value:1}, function(err, data){
+			counter.increment({
+				name: 'test',
+				value: 1
+			}, function(err, data) {
 				assert(!err, data.error_description);
 				console.log(data);
 				done();
 			});
 		});
-		it("should increment 'test_counter' counter by 4", function(done){
-			time+=MINUTE*10
+		it("should increment 'test_counter' counter by 4", function(done) {
+			time += MINUTE * 10
 			counter.set("timestamp", time);
-			counter.increment({name:'test_counter', value:4}, function(err, data){
+			counter.increment({
+				name: 'test_counter',
+				value: 4
+			}, function(err, data) {
 				assert(!err, data.error_description);
-				console.log(JSON.stringify(data,null,4));
+				console.log(JSON.stringify(data, null, 4));
 				done();
 			});
 		});
-		it("should decrement 'test' counter", function(done){
-			time+=MINUTE*10
+		it("should decrement 'test' counter", function(done) {
+			time += MINUTE * 10
 			counter.set("timestamp", time);
-			counter.decrement({name:'test', value:1}, function(err, data){
+			counter.decrement({
+				name: 'test',
+				value: 1
+			}, function(err, data) {
 				assert(!err, data.error_description);
-				console.log(JSON.stringify(data,null,4));
+				console.log(JSON.stringify(data, null, 4));
 				done();
 			});
-		});	
-		it('should fetch the counter', function(done){
-			counter.fetch(function(err, data){
+		});
+		it('should fetch the counter', function(done) {
+			counter.fetch(function(err, data) {
 				assert(!err, data.error_description);
-				console.log(JSON.stringify(data,null,4));
+				console.log(JSON.stringify(data, null, 4));
 				console.log(time, Date.now());
 				done();
 			});
 		});
-		it('should fetch counter data', function(done){
-			counter.getData({resolution:'all', counters:['test', 'test_counter']}, function(err, data){
+		it('should fetch counter data', function(done) {
+			counter.getData({
+				resolution: 'all',
+				counters: ['test', 'test_counter']
+			}, function(err, data) {
 				assert(!err, data.error_description);
 				console.log(data);
 				console.log(time, Date.now());
@@ -321,5 +403,181 @@ describe('Usergrid', function(){
 			});
 		});
 	});
+	describe('Usergrid Folders and Assets', function() {
+		var folder,
+			asset,
+			user,
+			image_type,
+			image_url = 'http://placekitten.com/160/90',
+			// image_url="https://api.usergrid.com/yourorgname/sandbox/assets/a4025e7a-8ab1-11e3-b56c-5d3c6e4ca93f/data",
+			test_image,
+			filesystem,
+			file_url,
+			filename = "kitten.jpg",
+			foldername = "kittens",
+			folderpath = '/test/' + Math.round(10000 * Math.random()),
+			filepath = [folderpath, foldername, filename].join('/');
+		before(function(done) {
+			var req = new XMLHttpRequest();
+			req.open('GET', image_url, true);
+			req.responseType = 'blob';
+			req.onload = function() {
+				test_image = req.response;
+				image_type = req.getResponseHeader('Content-Type');
+				done();
+			}
+			req.onerror = function(err) {
+				done();
+			};
+			req.send(null);
+		});
+		before(function(done) {
+			this.timeout(10000);
+			client.request({
+				method: 'GET',
+				endpoint: 'Assets'
+			}, function(err, data) {
+				var assets = data.entities.filter(function(asset) {
+					return asset.name === filename
+				});
+				if (assets.length) {
+					assets.forEach(function(asset) {
+						client.request({
+							method: 'DELETE',
+							endpoint: 'assets/' + asset.uuid
+						});
+					});
+					done();
+				} else {
+					done();
+				}
+			});
+		});
+		before(function(done) {
+			this.timeout(10000);
+			client.request({
+				method: 'GET',
+				endpoint: 'folders'
+			}, function(err, data) {
+				var folders = data.entities.filter(function(folder) {
+					return folder.name === foldername
+				});
+				if (folders.length) {
+					folders.forEach(function(folder) {
+						client.request({
+							method: 'DELETE',
+							endpoint: 'folders/' + folder.uuid
+						});
+					});
+					done();
+				} else {
+					done();
+				}
+			});
+		});
+		before(function(done) {
+			this.timeout(10000);
+			user = new Usergrid.Entity({
+				client: client,
+				data: {
+					type: 'users',
+					username: 'assetuser'
+				}
+			});
+			user.fetch(function(err, data) {
+				console.log(user);
+				if (err) {
+					user.save(function() {
+						done();
+					})
+				} else {
+					done();
+				}
+			})
+		});
+		it('should CREATE a folder', function(done) {
+			console.log("FOLDERNAME:", foldername);
+			folder = new Usergrid.Folder({
+				client: client,
+				data: {
+					name: foldername,
+					owner: user.get("uuid"),
+					path: folderpath
+				}
+			}, function(err, data) {
+				assert(!err, data.error_description);
+				done();
+			});
+		});
+		it('should CREATE an asset', function(done) {
+			asset = new Usergrid.Asset({
+				client: client,
+				data: {
+					name: filename,
+					owner: user.get("uuid"),
+					path: filepath
+				}
+			}, function(err, data) {
+				assert(!err, data.error_description);
+				//console.log(data);
+				done();
+			});
+		});
+		it('should upload asset data', function(done) {
+			this.timeout(15000);
+			setTimeout(function() {
+				asset.upload(test_image, function(err, data) {
+					assert(!err, data.error_description);
+					done();
+				});
+			}, 10000);
+		});
+		it('should retrieve asset data', function(done) {
+			asset.download(function(err, data) {
+				assert(!err, data.error_description);
+				assert(data.type == test_image.type, "MIME types don't match");
+				assert(data.size == test_image.size, "sizes don't match");
+				done();
+			});
+		});
+		it('should add the asset to a folder', function(done) {
+			folder.addAsset({
+				asset: asset
+			}, function(err, data) {
+				assert(!err, data.error_description);
+				//console.log(data['entities']);
+				done();
+			})
+		});
+		it('should list the assets from a folder', function(done) {
+			folder.getAssets(function(err, assets) {
+				assert(!err, assets.error_description);
+				//console.log(folder['assets']);
+				done();
+			})
+		});
+		it('should remove the asset from a folder', function(done) {
+			folder.removeAsset({
+				asset: asset
+			}, function(err, data) {
+				assert(!err, data.error_description);
+				//console.log(data['entities']);
+				done();
+			})
+		});
+		it('should DELETE the asset', function(done) {
+			asset.destroy(function(err, data) {
+				assert(!err, data.error_description);
+				//console.log(data);
+				done();
+			})
+		});
+		it('should DELETE the folder', function(done) {
+			folder.destroy(function(err, data) {
+				assert(!err, data.error_description);
+				//console.log(data);
+				done();
+			})
+		});
+	});
 });
-


Mime
View raw message