Skip to content

Commit c9be44e

Browse files
committed
Finalize best practice updates
1 parent ca3f814 commit c9be44e

2 files changed

Lines changed: 54 additions & 31 deletions

File tree

ClearBlade.js

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,21 @@ if (!window.console) {
319319
newFilter[condition] = [newObj];
320320
if (typeof queryObj.query.FILTERS === 'undefined') {
321321
queryObj.query.FILTERS = [];
322+
queryObj.query.FILTERS.push([newFilter]);
323+
return;
324+
} else {
325+
for (var i = 0; i < queryObj.query.FILTERS[0].length; i++) {
326+
for (var k in queryObj.query.FILTERS[0][i]) {
327+
if (queryObj.query.FILTERS[0][i].hasOwnProperty(k)) {
328+
if (k === condition) {
329+
queryObj.query.FILTERS[0][i][k].push(newObj);
330+
return;
331+
}
332+
}
333+
}
334+
}
335+
queryObj.query.FILTERS[0].push(newFilter);
322336
}
323-
queryObj.query.FILTERS.push(newFilter);
324337
};
325338

326339
var addSortToQuery = function(queryObj, direction, column) {
@@ -357,7 +370,6 @@ if (!window.console) {
357370
if (params) {
358371
url += "?" + params;
359372
}
360-
361373
//begin XMLHttpRequest
362374
var httpRequest;
363375

@@ -386,8 +398,8 @@ if (!window.console) {
386398
if (authToken) {
387399
httpRequest.setRequestHeader("CLEARBLADE-USERTOKEN", authToken);
388400
} else {
389-
httpRequest.setRequestHeader("CLEARBLADE-APPKEY", ClearBlade.appKey);
390-
httpRequest.setRequestHeader("CLEARBLADE-APPSECRET", ClearBlade.appSecret);
401+
httpRequest.setRequestHeader("ClearBlade-SystemKey", ClearBlade.appKey);
402+
httpRequest.setRequestHeader("ClearBlade-SystemSecret", ClearBlade.appSecret);
391403
}
392404

393405
if (!isObjectEmpty(body) || params) {
@@ -514,6 +526,10 @@ if (!window.console) {
514526
_request(options, callback);
515527
};
516528

529+
var _parseOperationQuery = function(_query) {
530+
return encodeURIComponent(JSON.stringify(_query.query.FILTERS));
531+
};
532+
517533
var _parseQuery = function(_query) {
518534
var parsed = encodeURIComponent(JSON.stringify(_query));
519535
return parsed;
@@ -646,7 +662,7 @@ if (!window.console) {
646662
var reqOptions = {
647663
method: 'PUT',
648664
endpoint: 'api/v/1/data/' + this.ID,
649-
body: {query: _query.OR, $set: changes}
665+
body: {query: _query.query.FILTERS, $set: changes}
650666
};
651667
if (typeof callback === 'function') {
652668
_request(reqOptions, callback);
@@ -680,7 +696,7 @@ if (!window.console) {
680696
if (_query === undefined) {
681697
throw new Error("no query defined!");
682698
} else {
683-
query = 'query=' + _parseQuery(_query.OR);
699+
query = 'query=' + _parseOperationQuery(_query);
684700
}
685701

686702
var reqOptions = {
@@ -796,7 +812,7 @@ if (!window.console) {
796812
* //will only match if an item has an attribute 'age' that is less than or equal to 50
797813
*/
798814
ClearBlade.Query.prototype.lessThanEqualTo = function (field, value) {
799-
addToQuery(this, "LTE", field, value);
815+
addFilterToQuery(this, "LTE", field, value);
800816
return this;
801817
};
802818

@@ -811,7 +827,7 @@ if (!window.console) {
811827
* //will only match if an item has an attribute 'name' that is not equal to 'Jim'
812828
*/
813829
ClearBlade.Query.prototype.notEqualTo = function (field, value) {
814-
addToQuery(this, "NEQ", field, value);
830+
addFilterToQuery(this, "NEQ", field, value);
815831
return this;
816832
};
817833

@@ -843,7 +859,6 @@ if (!window.console) {
843859
* default is 1.
844860
*/
845861
ClearBlade.Query.prototype.setPage = function (pageSize, pageNum) {
846-
pageSize = pageSize - 1; // TODO: Get rid of this if the backend changes
847862
addToQuery(this, "PAGESIZE", pageSize);
848863
addToQuery(this, "PAGENUM", pageNum);
849864
return this;
@@ -858,10 +873,10 @@ if (!window.console) {
858873
reqOptions.qs = 'query=' + _parseQuery(this.query);
859874
break;
860875
case "PUT":
861-
reqOptions.body = this.query;
876+
reqOptions.body = this.query; // TOOD: check this shit
862877
break;
863878
case "DELETE":
864-
reqOptions.qs = 'query=' + _parseQuery(this.query);
879+
reqOptions.qs = 'query=' + _parseOperationQuery(this.query);
865880
break;
866881
default:
867882
throw new Error("The method " + method + " does not exist");
@@ -944,7 +959,7 @@ if (!window.console) {
944959
ClearBlade.Query.prototype.update = function (changes, callback) {
945960
var reqOptions = {
946961
method: 'PUT',
947-
body: {query: this.query, $set: changes}
962+
body: {query: this.query.FILTERS, $set: changes}
948963
};
949964

950965
var colID = this.collection;
@@ -995,7 +1010,7 @@ if (!window.console) {
9951010
ClearBlade.Query.prototype.remove = function (callback) {
9961011
var reqOptions = {
9971012
method: 'DELETE',
998-
qs: 'query=' + _parseQuery(this.query)
1013+
qs: 'query=' + _parseOperationQuery(this.query)
9991014
};
10001015

10011016
var colID = this.collection;

test/ClearBladeSpec.js

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,18 @@ var RTP_INFO = {
1212
serverAddress: "https://rtp.clearblade.com",
1313
messagingURI: "rtp.clearblade.com",
1414
messagingPort: 8904,
15-
appKey: "c899bfae0afca9b1c899c2fe841d",
16-
appSecret: "C899BFAE0ACAE5A3C2A086ECDCF801",
17-
safariCollection: "aaaabfae0ad0da86f2dde3bba761",
18-
chromeCollection: "949abfae0aae8aaeffb09f80f8c101",
19-
generalCollection: "b69abfae0ad28bceb1dba4eecb19",
20-
firefoxCollection: "a49abfae0ac8e987e699def0e4e301"
15+
noAuthAppKey: "8cc896b40a82d0d2b4e18bbbed0f",
16+
noAuthAppSecret: "8CC896B40A90DEA898C197B5E357",
17+
safariNoAuthCollection: "ccc896b40ae083ea8af480d4868401",
18+
chromeNoAuthCollection: "e8c896b40a90bfcd9bc78df5ca5d",
19+
generalNoAuthCollection: "82c996b40a90fab0dbf7ff83e312",
20+
firefoxNoAuthCollection: "a8c996b40aacdd919dadce92c430",
21+
appKey: "d6d096b40ab4f3c7ddb4899b8a30",
22+
appSecret: "D6D096B40ADAA885E0BF8F8D93CB01",
23+
safariCollection: "84d196b40aa883bec8cfdaf697df01",
24+
firefoxCollection: "9ed196b40a8083efa485e58aa9b901",
25+
generalCollection: "9cd296b40acac5e2f797a485ec8e01",
26+
chromeCollection: "bcd196b40ade8ddb9491b494fd9f01"
2127
};
2228
var PLATFORM_INFO = {
2329
serverAddress: "https://platform.clearblade.com",
@@ -209,7 +215,7 @@ describe("ClearBlade collection fetching with users", function () {
209215
}, "returnedData should not be undefined", TEST_TIMEOUT);
210216

211217
runs(function () {
212-
expect(returnedData[0].data.name).toEqual('aaron');
218+
expect(returnedData.DATA[0].name).toEqual('aaron');
213219
});
214220
});
215221
});
@@ -272,7 +278,7 @@ describe("ClearBlade Query usage with anonymous user", function() {
272278
}, "Query should be finished", TEST_TIMEOUT);
273279

274280
runs(function() {
275-
expect(returnedData[0].data.name).toEqual('aaron');
281+
expect(returnedData.DATA[0].name).toEqual('aaron');
276282
});
277283
});
278284

@@ -314,7 +320,7 @@ describe("ClearBlade Query usage with anonymous user", function() {
314320
}, "Query should be finished", TEST_TIMEOUT);
315321

316322
runs(function() {
317-
expect(returnedData).toEqual([]);
323+
expect(returnedData.DATA).toEqual([]);
318324
});
319325

320326
// Positive case -- should return an item
@@ -334,7 +340,7 @@ describe("ClearBlade Query usage with anonymous user", function() {
334340
}, "Query should be finished", TEST_TIMEOUT);
335341

336342
runs(function() {
337-
expect(returnedData[0].data.name).toEqual('aaron');
343+
expect(returnedData.DATA[0].name).toEqual('aaron');
338344
});
339345
});
340346

@@ -392,7 +398,7 @@ describe("ClearBlade Query usage with anonymous user", function() {
392398

393399
var isCharlieDeleted;
394400
runs(function() {
395-
expect(returnedData.length).toEqual(2);
401+
expect(returnedData.DATA.length).toEqual(2);
396402
// Cleanup
397403
var query2 = new ClearBlade.Query();
398404
query2.equalTo('name', 'charlie');
@@ -494,7 +500,7 @@ describe("ClearBlade collections fetching", function () {
494500
}, "returnedData should not be undefined", TEST_TIMEOUT);
495501

496502
runs(function () {
497-
expect(returnedData[0].data.name).toEqual('aaron');
503+
expect(returnedData.DATA[0].name).toEqual('aaron');
498504
});
499505
});
500506
});
@@ -555,6 +561,7 @@ describe("ClearBlade collections CRUD should", function () {
555561
var callback = function (err, data) {
556562
secondFlag = true;
557563
if (err) {
564+
returnedData = data;
558565
} else {
559566
returnedData = data;
560567
}
@@ -567,7 +574,7 @@ describe("ClearBlade collections CRUD should", function () {
567574
}, "returnedData should not be undefined", TEST_TIMEOUT);
568575

569576
runs(function () {
570-
expect(returnedData[0].data.name).toEqual('jim');
577+
expect(returnedData.DATA[0].name).toEqual('jim');
571578
});
572579
});
573580

@@ -588,7 +595,6 @@ describe("ClearBlade collections CRUD should", function () {
588595
var newThing = {
589596
name: 'john'
590597
};
591-
debugger;
592598
col.update(query, newThing, callback);
593599
});
594600

@@ -601,6 +607,7 @@ describe("ClearBlade collections CRUD should", function () {
601607
var callback = function (err, data) {
602608
secondFlag = true;
603609
if (err) {
610+
returnedData = data;
604611
} else {
605612
returnedData = data;
606613
}
@@ -613,7 +620,7 @@ describe("ClearBlade collections CRUD should", function () {
613620
}, "returnedData should not be undefined", TEST_TIMEOUT);
614621

615622
runs(function () {
616-
expect(returnedData[0].data.name).toEqual('john');
623+
expect(returnedData.DATA[0].name).toEqual('john');
617624
});
618625
});
619626

@@ -650,7 +657,7 @@ describe("ClearBlade collections CRUD should", function () {
650657
}, "returnedData should not be undefined", TEST_TIMEOUT);
651658

652659
runs(function () {
653-
expect(returnedData).toEqual([]);
660+
expect(returnedData.DATA).toEqual([]);
654661
});
655662
});
656663
});
@@ -731,7 +738,7 @@ describe("Query objects should", function () {
731738
}, "returned data should not be undefined", TEST_TIMEOUT);
732739

733740
runs(function () {
734-
expect(returnedData[0].data.name).toEqual('John');
741+
expect(returnedData.DATA[0].name).toEqual('John');
735742
});
736743
});
737744

@@ -753,7 +760,8 @@ describe("Query objects should", function () {
753760
query.update(changes, function (err, data) {
754761
flag = true;
755762
if (err) {
756-
console.error(err);
763+
expect(err).toBeFalsy();
764+
returnedData = data;
757765
} else {
758766
returnedData = data;
759767
}

0 commit comments

Comments
 (0)