Permalink
Browse files

Update JSAPI to version 1.1.3

  • Loading branch information...
mohamedmansour committed Dec 8, 2011
1 parent 4fd229b commit 46ad2661827afe29aee93648b27e58db057bc88a
Showing with 89 additions and 42 deletions.
  1. +23 −4 js/jsapi/jsapi_abstract_database.js
  2. +10 −0 js/jsapi/jsapi_database.js
  3. +38 −20 js/jsapi/jsapi_for_google_plus.js
  4. +18 −18 js/jsapi/jsapi_helper.js
@@ -1,3 +1,22 @@
+/**
+ * Mock Entity for testing.
+ */
+MockEntity = function(db, name) {};
+MockEntity.prototype.tableDefinition = function() {};
+MockEntity.prototype.initialize = function(callback) {callback({data: {}, status: true})};
+MockEntity.prototype.getWhereObject = function(callback) {callback({keys: [], values: []})};
+MockEntity.prototype.getName = function() {return 'mock'};
+MockEntity.prototype.drop = function(callback) {callback({data: {}, status: true})};
+MockEntity.prototype.create = function(obj, callback) {callback({data: {}, status: true})};
+MockEntity.prototype.clear = function(callback) {callback({data: {}, status: true})};
+MockEntity.prototype.fireCallback = function(obj, callback) {callback({data: {}, status: true})};
+MockEntity.prototype.destroy = function(id, callback) {callback({data: {}, status: true})};
+MockEntity.prototype.update = function(obj, callback) {callback({data: {}, status: true})};
+MockEntity.prototype.find = function(select, where, callback) {callback({data: {}, status: true})};
+MockEntity.prototype.findAll = function(callback) {callback({data: {}, status: true})};
+MockEntity.prototype.count = function(obj, callback) {callback({data: {}, status: true})};
+MockEntity.prototype.save = function(obj, callback) {callback({data: {}, status: true})};
+
/**
* Represents a table entity.
*
@@ -32,11 +51,11 @@ AbstractEntity.prototype.initialize = function(callback) {
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
var val = obj[key];
- if (_.isString(val)) {
+ if (JSAPIHelper.isString(val)) {
sql.push(key + ' ' + val);
}
else if (key == 'unique') {
- _.each(val, function(uniqueItem) {
+ val.forEach(function(uniqueItem, index) {
sql.push('UNIQUE (' + uniqueItem.join(', ') + ')');
});
}
@@ -156,7 +175,7 @@ AbstractEntity.prototype.clear = function(callback) {
*/
AbstractEntity.prototype.create = function(obj, callback) {
var self = this;
- if (!_.isArray(obj)) {
+ if (!JSAPIHelper.isArray(obj)) {
obj = [obj];
}
this.db.transaction(function(tx) {
@@ -213,7 +232,7 @@ AbstractEntity.prototype.destroy = function(id, callback) {
*/
AbstractEntity.prototype.update = function(obj, callback) {
var self = this;
- if (!_.isArray(obj)) {
+ if (!JSAPIHelper.isArray(obj)) {
obj = [obj];
}
View
@@ -1,3 +1,13 @@
+/**
+ * Mock DB for testing.
+ */
+MockDB = function () {};
+MockDB.prototype.open = function() {};
+MockDB.prototype.getCircleEntity = function() {return new MockEntity()};
+MockDB.prototype.getPersonEntity = function() {return new MockEntity()};
+MockDB.prototype.getPersonCircleEntity = function() {return new MockEntity()};
+MockDB.prototype.clearAll = function(callback) {};
+
/**
* Storage class responsible for managing the database tansactions for Google+
*
@@ -4,7 +4,7 @@
* Mohamed Mansour (http://mohamedmansour.com) *
* @constructor
*/
-GooglePlusAPI = function() {
+GooglePlusAPI = function(opt) {
//------------------------ Constants --------------------------
// Implemented API
this.CIRCLE_API = 'https://plus.google.com/u/0/_/socialgraph/lookup/circles/?m=true';
@@ -36,7 +36,8 @@ GooglePlusAPI = function() {
this.MEMBER_SUGGESTION_API = 'https://plus.google.com/u/0/_/socialgraph/lookup/circle_member_suggestions/'; // s=[[[null, null, "116805285176805120365"]]]&at=
//------------------------ Private Fields --------------------------
- this._db = new PlusDB();
+ this._opt = opt || {};
+ this._db = this._opt.use_mockdb ? new MockDB() : new PlusDB();
this._session = null;
this._info = null;
@@ -429,19 +430,25 @@ GooglePlusAPI.prototype.refreshInfo = function(callback) {
var self = this;
this._requestService(function(response) {
var responseMap = self._parseJSON(response[1]);
- info = {};
+ self._info = {};
// Just get the fist result of the Map.
for (var i in responseMap) {
var detail = responseMap[i];
var emailParse = detail[20].match(/(.+) <(.+)>/);
- info.full_email = emailParse[0];
- info.name = emailParse[1];
- info.email = emailParse[2];
- info.id = detail[0];
- info.acl = '"' + (detail[1][14][0][0]).replace(/"/g, '\\"') + '"';
+ self._info.full_email = emailParse[0];
+ self._info.name = emailParse[1];
+ self._info.email = emailParse[2];
+ self._info.id = detail[0];
+ self._info.acl = '"' + (detail[1][14][0][0]).replace(/"/g, '\\"') + '"';
+ self._info.circles = detail[10][1].map(function(element) {
+ return {id: element[0], name: element[1]}
+ });
break;
}
- self._fireCallback(callback, true);
+ self._fireCallback(callback, {
+ status: true,
+ data: self._info
+ });
}, this.INITIAL_DATA_API);
};
@@ -618,18 +625,32 @@ GooglePlusAPI.prototype.getProfile = function(callback, id) {
* user. The circle data is basically just the circle ID.
*
* @param {function(boolean)} callback
- * @param {string} id The profile ID
+ * @param {Array<string>} id The profile ID
*/
-GooglePlusAPI.prototype.lookupUser = function(callback, id) {
+GooglePlusAPI.prototype.lookupUsers = function(callback, ids) {
var self = this;
- var params = '?n=6&m=[[[null,null,"' + id + '"]]]';
+ var usersParam = [];
+ if (!JSAPIHelper.isArray(ids)) {
+ ids = [ids];
+ }
+ ids.forEach(function(element, i) {
+ usersParam.push('[null,null,"' + element + '"]');
+ });
+ var params = '?n=6&m=[[' + usersParam.join(', ') + ']]';
var data = 'at=' + this._getSession();
this._requestService(function(response) {
- var userObj = self._parseUser(response[1][0][1], true);
- self._fireCallback(callback, {
- user: userObj[0],
- circles: userObj[1]
+ var usersArr = response[1];
+ var users = {};
+ usersArr.forEach(function(element, i) {
+ var userObj = self._parseUser(element[1], true);
+ var user = userObj[0];
+ var circles = userObj[1];
+ users[user.id] = {
+ data: user,
+ circles: circles
+ };
});
+ self._fireCallback(callback, users);
}, this.LOOKUP_API + params, data);
};
@@ -781,10 +802,7 @@ GooglePlusAPI.prototype.search = function(callback, query, opt_extra) {
/**
* @return {Object.<string, string>} The information from the user.
- * - id
- * - name
- * - email
- * - acl
+ * - id | name | email | acl
*/
GooglePlusAPI.prototype.getInfo = function() {
return this._info;
View
@@ -50,10 +50,10 @@ JSAPIHelper.isString = function(obj) {
* return {boolean} True if the object is an array, otherwise false.
*/
JSAPIHelper.isArray = function(obj) {
- if (!obj) {
- return false;
- }
- return obj.constructor == Array;
+ if (!obj) {
+ return false;
+ }
+ return obj.constructor == Array;
};
/**
@@ -72,22 +72,22 @@ JSAPIHelper.isArray = function(obj) {
* false if not found.
*/
JSAPIHelper.searchArray = function(needle, haystack) {
- if (!JSAPIHelper.isArray(haystack)) {
- return false;
+ if (!JSAPIHelper.isArray(haystack)) {
+ return false;
+ }
+ for (var i = 0; i < haystack.length; i++) {
+ var currentValue = haystack[i];
+ if (JSAPIHelper.isArray(currentValue)) {
+ path = JSAPIHelper.searchArray(needle, currentValue);
+ if (path) {
+ return [i].concat(path);
+ }
}
- for (var i = 0; i < haystack.length; i++) {
- var currentValue = haystack[i];
- if (JSAPIHelper.isArray(currentValue)) {
- path = JSAPIHelper.searchArray(needle, currentValue);
- if (path) {
- return [i].concat(path);
- }
- }
- if (currentValue == needle) {
- return [i];
- }
+ if (currentValue == needle) {
+ return [i];
}
- return false;
+ }
+ return false;
};
/**

0 comments on commit 46ad266

Please sign in to comment.