You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@usergrid.apache.org by sn...@apache.org on 2014/04/10 17:21:05 UTC

[08/12] git commit: Added convenience methods for createAsset and createCounter

Added convenience methods for createAsset and createCounter


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

Branch: refs/heads/master
Commit: 3c47925e72db0dc06a09f1175634248b1057b92a
Parents: bc03fb8
Author: ryan bridges <rb...@apigee.com>
Authored: Tue Apr 1 14:18:37 2014 -0400
Committer: ryan bridges <rb...@apigee.com>
Committed: Tue Apr 1 14:18:37 2014 -0400

----------------------------------------------------------------------
 sdks/html5-javascript/lib/modules/Client.js  | 49 +++++++++++++
 sdks/html5-javascript/lib/modules/Counter.js |  4 +-
 sdks/html5-javascript/tests/mocha/test.js    | 42 +++++++-----
 sdks/html5-javascript/usergrid.js            | 83 ++++++++++++++++++++---
 sdks/html5-javascript/usergrid.min.js        |  4 +-
 5 files changed, 149 insertions(+), 33 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/3c47925e/sdks/html5-javascript/lib/modules/Client.js
----------------------------------------------------------------------
diff --git a/sdks/html5-javascript/lib/modules/Client.js b/sdks/html5-javascript/lib/modules/Client.js
index d9ab1b2..a87c935 100644
--- a/sdks/html5-javascript/lib/modules/Client.js
+++ b/sdks/html5-javascript/lib/modules/Client.js
@@ -207,6 +207,55 @@
     var entity = new Usergrid.Entity(options);
     return entity;
   };
+  /*
+   *  Main function for creating new counters - should be called directly.
+   *
+   *  options object: options {timestamp:0, category:'value', counters:{name : value}}
+   *
+   *  @method createCounter
+   *  @public
+   *  @params {object} options
+   *  @param {function} callback
+   *  @return {callback} callback(err, response, counter)
+   */
+  Usergrid.Client.prototype.createCounter = function(options, callback) {
+    var counter = new Usergrid.Counter({
+      client: this,
+      data: options
+    });
+    counter.save(callback);
+  };
+  /*
+   *  Main function for creating new assets - should be called directly.
+   *
+   *  options object: options {name:"photo.jpg", path:"/user/uploads", "content-type":"image/jpeg", owner:"F01DE600-0000-0000-0000-000000000000", file: FileOrBlobObject }
+   *
+   *  @method createCounter
+   *  @public
+   *  @params {object} options
+   *  @param {function} callback
+   *  @return {callback} callback(err, response, counter)
+   */
+  Usergrid.Client.prototype.createAsset = function(options, callback) {
+    var file=options.file;
+    if(file){
+      options.name=options.name||file.name;
+      options['content-type']=options['content-type']||file.type;
+      options.path=options.path||'/';
+      delete options.file;
+    }
+    var asset = new Usergrid.Asset({
+      client: this,
+      data: options
+    });
+    asset.save(function(err, response, asset){
+      if(file && !err){
+        asset.upload(file, callback);
+      }else{
+        doCallback(callback, [err, response, asset], asset);
+      }
+    });
+  };
 
   /*
    *  Main function for creating new collections - should be called directly.

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/3c47925e/sdks/html5-javascript/lib/modules/Counter.js
----------------------------------------------------------------------
diff --git a/sdks/html5-javascript/lib/modules/Counter.js b/sdks/html5-javascript/lib/modules/Counter.js
index 5de76c8..03953f5 100644
--- a/sdks/html5-javascript/lib/modules/Counter.js
+++ b/sdks/html5-javascript/lib/modules/Counter.js
@@ -5,7 +5,7 @@
  *  @param {object} options {timestamp:0, category:'value', counters:{name : value}}
  *  @returns {callback} callback(err, event)
  */
-Usergrid.Counter = function(options, callback) {
+Usergrid.Counter = function(options) {
   // var self=this;
   this._client = options.client;
   this._data = options.data || {};
@@ -13,7 +13,7 @@ Usergrid.Counter = function(options, callback) {
   this._data.timestamp = options.timestamp || 0;
   this._data.type = "events";
   this._data.counters = options.counters || {};
-  doCallback(callback, [false, this], this);
+  // doCallback(callback, [false, this], this);
   //this.save(callback);
 };
 var COUNTER_RESOLUTIONS = [

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/3c47925e/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 d27949c..a00ff6e 100644
--- a/sdks/html5-javascript/tests/mocha/test.js
+++ b/sdks/html5-javascript/tests/mocha/test.js
@@ -800,11 +800,9 @@ describe('Usergrid', function(){
                         test_counter: 0
                     }
                 }
-            }, function(err, data) {
-                assert(!err, data.error_description);
-                console.log(data);
-                done();
             });
+            assert(counter, "Counter not created");
+            done();
         });
         it('should save a counter', function(done) {
             counter.save(function(err, data) {
@@ -902,7 +900,6 @@ describe('Usergrid', function(){
             req.onload = function() {
                 test_image = req.response;
                 image_type = req.getResponseHeader('Content-Type');
-                console.log(test_image, image_type);
                 done();
             }
             req.onerror = function(err) {
@@ -1011,24 +1008,33 @@ describe('Usergrid', function(){
                 done();
             });
         });
+        it('should RETRIEVE an asset', function(done) {
+            asset.fetch(function(err, response, entity){
+                if(err){
+                    assert(false, err);
+                }else{
+                    asset=entity;
+                }
+                done();
+            })
+        });
         it('should upload asset data', function(done) {
-            this.timeout(15000);
-            setTimeout(function() {
-                asset.upload(test_image, function(err, response, asset) {
-                    if(err){
-                        assert(false, err.error_description);
-                    }
-                    done();
-                });
-            }, 10000);
+            this.timeout(5000);
+            asset.upload(test_image, function(err, response, asset) {
+                if(err){
+                    assert(false, err.error_description);
+                }
+                done();
+            });
         });
         it('should retrieve asset data', function(done) {
+            this.timeout(5000);
             asset.download(function(err, response, asset) {
                 if(err){
                     assert(false, err.error_description);
                 }
-                assert(asset.type == test_image.type, "MIME types don't match");
-                assert(asset.size == test_image.size, "sizes don't match");
+                assert(asset.get('content-type') == test_image.type, "MIME types don't match");
+                assert(asset.get('size') == test_image.size, "sizes don't match");
                 done();
             });
         });
@@ -1063,7 +1069,7 @@ describe('Usergrid', function(){
                 done();
             })
         });
-        it('should DELETE the asset', function(done) {
+        after(function(done) {
             asset.destroy(function(err, data) {
                 if(err){
                     assert(false, err.error_description);
@@ -1072,7 +1078,7 @@ describe('Usergrid', function(){
                 done();
             })
         });
-        it('should DELETE the folder', function(done) {
+        after(function(done) {
             folder.destroy(function(err, data) {
                 if(err){
                     assert(false, err.error_description);

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/3c47925e/sdks/html5-javascript/usergrid.js
----------------------------------------------------------------------
diff --git a/sdks/html5-javascript/usergrid.js b/sdks/html5-javascript/usergrid.js
index 98fa616..79ab355 100644
--- a/sdks/html5-javascript/usergrid.js
+++ b/sdks/html5-javascript/usergrid.js
@@ -747,6 +747,55 @@ function doCallback(callback, params, context) {
         return entity;
     };
     /*
+   *  Main function for creating new counters - should be called directly.
+   *
+   *  options object: options {timestamp:0, category:'value', counters:{name : value}}
+   *
+   *  @method createCounter
+   *  @public
+   *  @params {object} options
+   *  @param {function} callback
+   *  @return {callback} callback(err, response, counter)
+   */
+    Usergrid.Client.prototype.createCounter = function(options, callback) {
+        var counter = new Usergrid.Counter({
+            client: this,
+            data: options
+        });
+        counter.save(callback);
+    };
+    /*
+   *  Main function for creating new assets - should be called directly.
+   *
+   *  options object: options {name:"photo.jpg", path:"/user/uploads", "content-type":"image/jpeg", owner:"F01DE600-0000-0000-0000-000000000000", file: FileOrBlobObject }
+   *
+   *  @method createCounter
+   *  @public
+   *  @params {object} options
+   *  @param {function} callback
+   *  @return {callback} callback(err, response, counter)
+   */
+    Usergrid.Client.prototype.createAsset = function(options, callback) {
+        var file = options.file;
+        if (file) {
+            options.name = options.name || file.name;
+            options["content-type"] = options["content-type"] || file.type;
+            options.path = options.path || "/";
+            delete options.file;
+        }
+        var asset = new Usergrid.Asset({
+            client: this,
+            data: options
+        });
+        asset.save(function(err, response, asset) {
+            if (file && !err) {
+                asset.upload(file, callback);
+            } else {
+                doCallback(callback, [ err, response, asset ], asset);
+            }
+        });
+    };
+    /*
    *  Main function for creating new collections - should be called directly.
    *
    *  options object: options {client:client, type: type, qs:qs}
@@ -2487,7 +2536,7 @@ Usergrid.Group.prototype.createGroupActivity = function(options, callback) {
  *  @param {object} options {timestamp:0, category:'value', counters:{name : value}}
  *  @returns {callback} callback(err, event)
  */
-Usergrid.Counter = function(options, callback) {
+Usergrid.Counter = function(options) {
     // var self=this;
     this._client = options.client;
     this._data = options.data || {};
@@ -2495,7 +2544,6 @@ Usergrid.Counter = function(options, callback) {
     this._data.timestamp = options.timestamp || 0;
     this._data.type = "events";
     this._data.counters = options.counters || {};
-    doCallback(callback, [ false, this ], this);
 };
 
 var COUNTER_RESOLUTIONS = [ "all", "minute", "five_minutes", "half_hour", "hour", "six_day", "day", "week", "month" ];
@@ -2925,18 +2973,32 @@ Usergrid.Asset.prototype.upload = function(data, callback) {
         return;
     }
     var self = this;
+    var args = arguments;
+    var attempts = self.get("attempts");
+    if (isNaN(attempts)) {
+        attempts = 3;
+    }
+    self.set("content-type", data.type);
+    self.set("size", data.size);
     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 UsergridError("The File APIs are not fully supported by your browser.") ], self);
+        doCallback(callback, [ new UsergridError("The File APIs are not fully supported by your browser.") ], xhr, self);
     };
     xhr.onload = function(ev) {
-        if (xhr.status >= 300) {
-            doCallback(callback, [ new UsergridError(JSON.parse(xhr.responseText)), null, self ], self);
+        if (xhr.status >= 500 && attempts > 0) {
+            self.set("attempts", --attempts);
+            setTimeout(function() {
+                self.upload.apply(self, args);
+            }, 100);
+        } else if (xhr.status >= 300) {
+            self.set("attempts");
+            doCallback(callback, [ new UsergridError(JSON.parse(xhr.responseText)), xhr, self ], self);
         } else {
+            self.set("attempts");
             doCallback(callback, [ null, xhr, self ], self);
         }
     };
@@ -2944,9 +3006,8 @@ Usergrid.Asset.prototype.upload = function(data, callback) {
     fr.onload = function() {
         var binary = fr.result;
         xhr.overrideMimeType("application/octet-stream");
-        setTimeout(function() {
-            xhr.sendAsBinary(binary);
-        }, 1e3);
+        // setTimeout(function() {
+        xhr.sendAsBinary(binary);
     };
     fr.readAsBinaryString(data);
 };
@@ -2966,13 +3027,13 @@ Usergrid.Asset.prototype.download = function(callback) {
     xhr.responseType = "blob";
     xhr.onload = function(ev) {
         var blob = xhr.response;
-        //callback(null, blob);
-        doCallback(callback, [ null, blob, self ], self);
+        doCallback(callback, [ null, xhr, self ], self);
     };
     xhr.onerror = function(err) {
         callback(true, err);
-        doCallback(callback, [ new UsergridError(err), err, self ], self);
+        doCallback(callback, [ new UsergridError(err), xhr, self ], self);
     };
+    xhr.overrideMimeType(self.get("content-type"));
     xhr.send();
 };