usergrid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From snoopd...@apache.org
Subject [09/41] git commit: code cleanup
Date Tue, 11 Feb 2014 23:20:53 GMT
code cleanup


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

Branch: refs/heads/master
Commit: 4245f53aa6190304822f28649e48ca0d61ad885a
Parents: 9166895
Author: ryan bridges <rbridges@apigee.com>
Authored: Mon Feb 3 09:07:08 2014 -0500
Committer: ryan bridges <rbridges@apigee.com>
Committed: Mon Feb 3 09:07:08 2014 -0500

----------------------------------------------------------------------
 sdks/html5-javascript/lib/Client.js | 55 +++++++++++---------------
 sdks/html5-javascript/lib/Entity.js | 66 ++++++++++++++++----------------
 2 files changed, 56 insertions(+), 65 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/4245f53a/sdks/html5-javascript/lib/Client.js
----------------------------------------------------------------------
diff --git a/sdks/html5-javascript/lib/Client.js b/sdks/html5-javascript/lib/Client.js
index f29a51e..7b5a425 100644
--- a/sdks/html5-javascript/lib/Client.js
+++ b/sdks/html5-javascript/lib/Client.js
@@ -531,12 +531,13 @@ Usergrid.Client.prototype.getObject = function(key) {
 
 Usergrid.Client.prototype.get = function (key) {
   var keyStore = 'apigee_' + key;
+  var value=null;
   if (this[key]) {
-    return this[key];
+    value= this[key];
   } else if(typeof(Storage)!=="undefined") {
-    return localStorage.getItem(keyStore);
+    value= localStorage.getItem(keyStore);
   }
-  return null;
+  return value;
 };
 
 /*
@@ -778,10 +779,7 @@ Usergrid.Client.prototype.getLoggedInUser = function (callback) {
  *  @return {boolean} Returns true the user is logged in (has token and uuid), false if not
  */
 Usergrid.Client.prototype.isLoggedIn = function () {
-  if (this.getToken() && this.getToken() != 'null') {
-    return true;
-  }
-  return false;
+  return "undefined" !== typeof this.getToken();
 };
 
 /*
@@ -804,32 +802,23 @@ Usergrid.Client.prototype.logout = function () {
  *  @return {string} curl
  */
 Usergrid.Client.prototype.buildCurlCall = function (options) {
-  var curl = 'curl';
+  var curl = ['curl'];
   var method = (options.method || 'GET').toUpperCase();
-  var body = options.body || {};
+  var body = options.body;
   var uri = options.uri;
 
   //curl - add the method to the command (no need to add anything for GET)
-  if (method === 'POST') {
-    curl += ' -X POST';
-  } else if (method === 'PUT') {
-    curl += ' -X PUT';
-  } else if (method === 'DELETE') {
-    curl += ' -X DELETE';
-  } else {
-    curl += ' -X GET';
-  }
+  curl.push('-X');
+  curl.push((['POST','PUT','DELETE'].indexOf(method)>=0)?method:'GET');
 
   //curl - append the path
-  curl += ' ' + uri;
+  curl.push(uri);
 
-  //curl - add the body
-  if("undefined"!== typeof window){body = JSON.stringify(body);}//only in node module
-  if (body !== '"{}"' && method !== 'GET' && method !== 'DELETE') {
-    //curl - add in the json obj
-    curl += " -d '" + body + "'";
+  if("object"===typeof body && Object.keys(body).length>0 && ['POST','PUT'].indexOf(method)!==-1){
+    curl.push('-d');
+    curl.push("'"+JSON.stringify(body)+"'");
   }
-
+  curl=curl.join(' ');
   //log the curl command to the console
   console.log(curl);
 
@@ -837,18 +826,18 @@ Usergrid.Client.prototype.buildCurlCall = function (options) {
 }
 
 Usergrid.Client.prototype.getDisplayImage = function (email, picture, size) {
+    size = size || 50;
+    var image='https://apigee.com/usergrid/images/user_profile.png';
   try {
     if (picture) {
-      return picture;
-    }
-    var size = size || 50;
-    if (email.length) {
-      return 'https://secure.gravatar.com/avatar/' + MD5(email) + '?s=' + size + encodeURI("&d=https://apigee.com/usergrid/images/user_profile.png");
-    } else {
-      return 'https://apigee.com/usergrid/images/user_profile.png';
+      image= picture;
+    }else if (email.length) {
+      image= 'https://secure.gravatar.com/avatar/' + MD5(email) + '?s=' + size + encodeURI("&d=https://apigee.com/usergrid/images/user_profile.png");
     }
   } catch(e) {
-    return 'https://apigee.com/usergrid/images/user_profile.png';
+    //TODO do something with this error?
+  }finally{
+    return image;
   }
 }
 

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/4245f53a/sdks/html5-javascript/lib/Entity.js
----------------------------------------------------------------------
diff --git a/sdks/html5-javascript/lib/Entity.js b/sdks/html5-javascript/lib/Entity.js
index c4fe016..cb728be 100644
--- a/sdks/html5-javascript/lib/Entity.js
+++ b/sdks/html5-javascript/lib/Entity.js
@@ -20,7 +20,7 @@ Usergrid.Entity = function(options) {
  *  @params {any} obj - any variable
  *  @return {boolean} Returns true or false
  */
-Usergrid.isEntity = function(obj){
+Usergrid.Entity.isEntity = function(obj){
   return (obj && obj instanceof Usergrid.Entity);
 }
 
@@ -33,7 +33,7 @@ Usergrid.isEntity = function(obj){
  *  @params {any} obj - any variable
  *  @return {boolean} Returns true or false
  */
-Usergrid.isPersistedEntity = function(obj){
+Usergrid.Entity.isPersistedEntity = function(obj){
   return (isEntity(obj) && isUUID(obj.get('uuid')));
 }
 
@@ -58,11 +58,7 @@ Usergrid.Entity.prototype.serialize = function () {
  *  @return {string} || {object} data
  */
 Usergrid.Entity.prototype.get = function (field) {
-  if (field) {
-    return this._data[field];
-  } else {
-    return this._data;
-  }
+    return (field) ? this._data[field] : this._data;
 };
 
 /*
@@ -100,33 +96,39 @@ Usergrid.Entity.prototype.set = function (key, value) {
  *  @return {callback} callback(err, data)
  */
 Usergrid.Entity.prototype.save = function (callback) {
-  var type = this.get('type');
-  var method = 'POST';
-  if (isUUID(this.get('uuid'))) {
-    method = 'PUT';
-    type += '/' + this.get('uuid');
-  }
+  var self = this,
+    type = this.get('type'),
+    method = 'POST',
+    entityId=this.get("uuid"),
+    data = {},
+    entityData = this.get(),
+    password = this.get('password'),
+    oldpassword = this.get('oldpassword'),
+    newpassword = this.get('newpassword'),
+    SYSTEM_PROPERTIES=['metadata','created','modified','oldpassword','newpassword','type','activated','uuid'],
+    options={
+      method:method,
+      endpoint:type
+    };
 
   //update the entity
-  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 SYSTEM_PROPERTIES=['metadata','created','modified','oldpassword','newpassword','type','activated','uuid'];
-  //remove system specific properties
-  for (var item in entityData) {
-    if (SYSTEM_PROPERTIES.indexOf(item) !== -1) {
-      continue;
-    }
-    data[item] = entityData[item];
+  console.log("entityId", entityId);
+  if (entityId) {
+    options.method = 'PUT';
+    options.endpoint += '/' + entityId;
   }
-  var options =  {
-    method:method,
-    endpoint:type,
-    body:data
-  };
+
+  //remove system-specific properties
+  Object
+    .keys(entityData) /* get the list of properties we have */
+    .filter(function(key){
+      return (SYSTEM_PROPERTIES.indexOf(key)===-1) /* remove system properties from the payload
*/
+    })
+    .forEach(function(key){
+      data[key]= entityData[key];
+    });
+    options.body=data;
+    console.log(options.body);
   //save the entity first
   this._client.request(options, function (err, retdata) {
       //clear out pw info if present
@@ -353,7 +355,7 @@ Usergrid.Entity.prototype.getEntityId = function (entity) {
   if (isUUID(entity.get('uuid'))) {
     id = entity.get('uuid');
   } else {
-    if (type === 'users') {
+    if (this.get("type") === 'users') {
       id = entity.get('username');
     } else if (entity.get('name')) {
       id = entity.get('name');


Mime
View raw message