Skip to content

Instantly share code, notes, and snippets.

@aaronksaunders
Forked from stephenfeather/parse.js
Last active August 29, 2015 14:19
Show Gist options
  • Select an option

  • Save aaronksaunders/9ca67cd65cab00e70065 to your computer and use it in GitHub Desktop.

Select an option

Save aaronksaunders/9ca67cd65cab00e70065 to your computer and use it in GitHub Desktop.

Revisions

  1. aaronksaunders revised this gist Apr 19, 2015. 1 changed file with 252 additions and 182 deletions.
    434 changes: 252 additions & 182 deletions parse.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,14 @@
    // Copyright Stephen Feather and other contributors.
    //
    // Copyright Aaron K. Saunders and other contributors. 2015
    //
    // Primarily based on work by Stephen Feather - https://gist.github.com/sfeather/4400387
    // with additions listed below
    // - added promise functionality (https://github.com/kriskowal/q), and removing callbacks
    // - added url query param support to allow for more interesting queries
    // - added promises.notify to support in progress information from http request
    // - added init function to extract credentials from the library
    // - fare number of the user functions are not correct
    // - clean up parameter naming
    //
    // Permission is hereby granted, free of charge, to any person obtaining a
    // copy of this software and associated documentation files (the
    @@ -22,266 +32,326 @@
    /**
    * Usage:
    * var Parse = require('parse');
    *
    * Parse.getObjects('myClass', myCallback);
    *
    * Notes: Some folks like to do their error handling/forking in the Library.
    * I don't. So I only pass one callback into each function.
    *
    * Parse.getObjects('myClass', '{urlParams:{}}');
    *
    * Notes: Some folks like to do their error handling/forking in the Library.
    * I don't. So I only pass one callback into each function.
    * if you want a specific error function, add another param.
    *
    *
    * Push notification registration borrowed/modified from Matt Berg (https://gist.github.com/3761738).
    *
    *
    */

    // will use this for promises
    var Q = require('q');

    var baseURL = 'https://api.parse.com/1/',
    appId = 'XXXXXXXXXXXXXXX',
    apiKey = 'XXXXXXXXXXXXXX'; // Be sure to use your REST API key and NOT your master as bad stuff can happen.

    appIdd,
    apiKey;

    // Be sure to use your REST API key and NOT your master as bad stuff can happen.

    function ParseClient() {

    function ParseClient(){

    }

    ParseClient.prototype.saveUserRecord = function(user){
    Ti.App.Properties.setObject('parseUser', user);
    ParseClient.prototype.init = function(_config) {
    appId = _config.appId,
    apiKey = _config.apiKey;
    };

    ParseClient.prototype.setSessionToken = function(token){
    Ti.App.Properties.setString('parseSessionToken', token);
    ParseClient.prototype.saveUserRecord = function(user) {
    Ti.App.Properties.setObject('parseUser', user);
    };

    ParseClient.prototype.getSessionToken = function () {
    return Ti.App.Properties.getString('parseSessionToken');
    ParseClient.prototype.removeUserRecord = function(user) {
    Ti.App.Properties.removeObject('parseUser', user);
    };


    ParseClient.prototype.createObject = function(_class, data, callback){
    var url = baseURL+'classes/'+_class;
    var params = {
    method: 'POST',
    body: data
    };

    this._request(url, params, callback);
    ParseClient.prototype.setSessionToken = function(token) {
    Ti.App.Properties.setString('parseSessionToken', token);
    };

    ParseClient.prototype.updateObject = function(_class, _objectID, data, callback){
    var url = baseURL+'classes/'+_class+'/'+_objectID;
    var params = {
    method: 'PUT',
    body: data
    };

    this._request(url, params, callback);
    ParseClient.prototype.getSessionToken = function() {
    return Ti.App.Properties.getString('parseSessionToken');
    };

    ParseClient.prototype.getObjects = function(_class, callback){
    var url = baseURL+'classes/'+_class;
    var params = {
    method: 'GET'
    };

    this._request(url, params, callback);
    ParseClient.prototype.createObject = function(_class, data, callback) {
    var url = baseURL + 'classes/' + _class;
    var params = {
    method : 'POST',
    body : data
    };

    return this._request(url, params, callback);
    };

    ParseClient.prototype.getObject = function(_class, _objectID, callback){
    var url = baseURL+'classes/'+_class+'/'+_objectID;
    this._request(url, callback);
    ParseClient.prototype.updateObject = function(_class, _objectID, data, callback) {
    var url = baseURL + 'classes/' + _class + '/' + _objectID;
    var params = {
    method : 'PUT',
    body : data
    };

    return this._request(url, params, callback);
    };

    ParseClient.prototype.deleteObject = function(_class, _objectID, callback){
    var url = baseURL+'classes/'+_class+'/'+_objectID;
    var params = {
    method: 'DELETE'
    };

    this._request(url, params, callback);
    ParseClient.prototype.getObjects = function(_class, _params, callback) {
    var url = baseURL + 'classes/' + _class;
    var params = {
    method : 'GET'
    };

    params = _.extend(params, _params);

    return this._request(url, params, callback);
    };

    ParseClient.prototype.createUser = function(data, callback){
    var url = baseURL+'users';
    var params = {
    method: 'POST',
    body: data
    };

    function cb(success, response, code){
    if (success === 1){
    response = JSON.parse(response);
    parse.setSessionToken(response.sessionToken);
    callback(success, response, code);
    } else {
    callback(success, response, code);
    }
    }

    this._request(url, params, cb);
    ParseClient.prototype.getObject = function(_class, _objectID, callback) {
    var url = baseURL + 'classes/' + _class + '/' + _objectID;
    return this._request(url, callback);
    };

    ParseClient.prototype.getUsers = function(callback){
    var url = baseURL+'users';
    this._request(url, callback);
    ParseClient.prototype.deleteObject = function(_class, _objectID, callback) {
    var url = baseURL + 'classes/' + _class + '/' + _objectID;
    var params = {
    method : 'DELETE'
    };

    return this._request(url, params, callback);
    };

    ParseClient.prototype.getUser = function(_userObject, callback){
    var url = baseURL+'users/'+_userObject;
    this._request(url, callback);
    ParseClient.prototype.createUser = function(data, callback) {
    var url = baseURL + 'users';
    var params = {
    method : 'POST',
    body : data
    };

    function cb(success, response, code) {
    if (success === 1) {
    response = JSON.parse(response);
    parse.setSessionToken(response.sessionToken);
    callback(success, response, code);
    } else {
    callback(success, response, code);
    }
    }

    return this._request(url, params, cb);
    };

    ParseClient.prototype.loginUser = function(_username, _password, callback){
    var url = baseURL+'login?username='+_username+'&password='+_password;
    function cb(success, response, code){
    if (success === 1){
    response = JSON.parse(response);
    parse.setSessionToken(response.sessionToken);
    parse.saveUserRecord(response);
    callback(success, response, code);
    } else {
    callback(success, response, code);
    }
    }
    this._request(url, cb);
    ParseClient.prototype.getUsers = function(_params, callback) {
    var url = baseURL + 'users';
    var params = {
    method : 'GET',
    };

    params = _.extend(params, _params);

    return this._request(url, params, callback);
    };

    ParseClient.prototype.updateUser = function(_userObject, data, callback){
    var url = baseURL+'users/'+_userObject;
    var params = {
    method: 'PUT',
    body: data
    };

    this._request(url, params, callback);
    /**
    * @TODO - not updating saved user object
    */
    ParseClient.prototype.getUser = function(_userId, callback) {
    var url = baseURL + 'users/' + _userId;
    return this._request(url, callback);
    };

    ParseClient.prototype.deleteUser = function(_userObject, callback){
    var url = baseURL+'users/'+_userObject;
    var params = {
    method: 'DELETE'
    };

    this._request(url, params, callback);
    ParseClient.prototype.loginUser = function(_username, _password) {
    var deferred = Q.defer();

    var url = baseURL + 'login?username=' + _username + '&password=' + _password;

    this._request(url, null).then(function(_response) {
    var response = _response.response;
    parse.setSessionToken(response.sessionToken);
    parse.saveUserRecord(response);
    return deferred.resolve(response);
    }, function(_error) {
    return deferred.reject(_error);
    });

    return deferred.promise;
    };

    ParseClient.prototype.passwordReset = function(_email, callback){
    var url = baseURL+'requestPasswordReset';
    _email = {
    email: _email
    };

    var params = {
    method: 'POST',
    body: _email
    };


    this._request(url, params, callback);
    /**
    * @TODO - not updating saved user object
    */
    ParseClient.prototype.updateUser = function(_userObject, data, callback) {
    var url = baseURL + 'users/' + _userObject;
    var params = {
    method : 'PUT',
    body : data
    };

    return this._request(url, params, callback);
    };

    ParseClient.prototype.deleteUser = function(_userObject, callback) {
    var url = baseURL + 'users/' + _userObject;
    var params = {
    method : 'DELETE'
    };

    ParseClient.prototype.uploadImage = function (_contentType, _filename, _blob, callback){
    var url = baseURL+'files/'+_filename;
    var params = {
    method: 'POST',
    type: 'image',
    body: _blob,
    headers : {}
    };
    params.headers['Content-Type'] = _contentType;
    //Ti.API.error(params);
    this._request(url, params, callback);
    return this._request(url, params, callback);
    };

    // -- functions below here not fully functional/tested --
    ParseClient.prototype.passwordReset = function(_email, callback) {
    var url = baseURL + 'requestPasswordReset';
    _email = {
    email : _email
    };

    var params = {
    method : 'POST',
    body : _email
    };

    return this._request(url, params, callback);
    };

    ParseClient.prototype.uploadFile = function(_contentType, _filename, _blob, callback) {

    console.log("_filename: " + _filename);

    var url = baseURL + 'files/' + _filename;
    var params = {
    method : 'POST',
    type : 'image',
    body : _blob,
    headers : {}
    };
    params.headers['Content-Type'] = _contentType;
    //Ti.API.error(params);
    return this._request(url, params, callback);
    };

    // -- functions below here not fully functional/tested --
    // @TODO - clean this up!!

    ParseClient.prototype.registerPush = function(params, callback) {
    var method = 'POST',
    url = config.parse.baseUrl + '/installations',
    payload = (params) ? JSON.stringify(params) : '';
    this._request(url, method, payload, function(data, status) {

    return this._request(url, method, payload, function(data, status) {
    Ti.API.log('completed registration: ' + JSON.stringify(status));
    callabck(1, data, status);
    }, function(xhr, error) {
    Ti.API.log('xhr error registration: ' + JSON.stringify(error));
    callback(0,error);
    callback(0, error);
    });
    };


    /**
    *
    * @param {Object} url
    * @param {Object} params
    * @param {Object} callback
    */
    ParseClient.prototype._request = function(url, params, callback) {
    if (typeof params === 'function'){
    callback = params;
    params = {};

    var Q = require('q');
    var deferred = Q.defer();

    if ( typeof params === 'function') {
    callback = params;
    params = {};
    }



    params = params || {};

    // Clean up the call type, defaulting to GET if no method set
    params.method = params.method || 'GET';
    params.method = params.method.toUpperCase();
    params.method = params.method.toUpperCase();

    // If not specified, use a 20 second timeout
    params.timeout = ('timeout' in params) ? params.timeout : 15000;
    params.body = params.body || {};
    params.query = params.query || {};
    params.url = url || baseURL;
    //params.url += url;
    params.headers = params.headers || {};
    params.headers['X-Parse-Application-Id'] = appId;
    params.headers['X-Parse-REST-API-Key'] = apiKey;
    if (!params.headers['Content-Type']){
    params.headers['Content-Type'] = 'application/json';
    }
    params.headers['Accept'] = params.headers['Accept'] || 'application/json';
    if(!('login' in params) || !params.login){
    params.headers['X-Parse-Session-Token'] = this.getSessionToken();
    }

    params.query = params.query || {};
    params.url = url || baseURL;
    //params.url += url;
    params.headers = params.headers || {};
    params.headers['X-Parse-Application-Id'] = appId;
    params.headers['X-Parse-REST-API-Key'] = apiKey;
    params.headers['X-Parse-Revocable-Session'] = 1;
    if (!params.headers['Content-Type']) {
    params.headers['Content-Type'] = 'application/json';
    }
    params.headers['Accept'] = params.headers['Accept'] || 'application/json';
    if (!('login' in params) || !params.login) {
    params.headers['X-Parse-Session-Token'] = this.getSessionToken();
    }

    // Need to clear some properties depending on method
    if ((params.method === 'GET') || (params.method === 'DELETE')){
    params.body = null;
    if ((params.method === 'GET') || (params.method === 'DELETE')) {
    params.body = null;
    } else {
    if (params.type === 'image'){
    params.body = params.body;
    } else {
    params.body = JSON.stringify(params.body);
    }
    params.query = null;
    if (params.type === 'image') {
    params.body = params.body;
    } else {
    params.body = JSON.stringify(params.body);
    }
    params.query = null;
    }



    var xhr = Ti.Network.createHTTPClient({
    onsendstream: function(e){
    Ti.API.error(e.progress);
    }
    onsendstream : function(e) {
    Ti.API.debug("progress: " + JSON.stringify(e));
    deferred.notify(e.progress);
    }
    });

    xhr.setTimeout(params.timeout);

    xhr.onerror = function(e) {
    callback(0,this.responseText, this.status, this);
    callback && callback(0, this.responseText, this.status, this);

    deferred.reject({
    error : this.responseText ? JSON.parse(this.responseText) : {},
    status : this.status
    });
    };

    xhr.onload = function() {
    callback(1,this.responseText, this.status);
    callback && callback(1, this.responseText, this.status);
    deferred.resolve({
    response : this.responseText ? JSON.parse(this.responseText) : {},
    status : this.status
    });
    };



    //params = params.replace(/\./g, '_');


    params.url = encodeData(params.urlparams, params.url);

    Ti.API.debug('params.url: ' + params.url);

    xhr.open(params.method, params.url);

    for (var key in params.headers) {
    xhr.setRequestHeader(key, params.headers[key]);
    }
    xhr.setRequestHeader(key, params.headers[key]);
    }
    xhr.send(params.body);

    return deferred.promise;
    };


    function encodeData(_params, _url) {
    var str = [];
    for (var p in _params) {
    str.push(Ti.Network.encodeURIComponent(p) + "=" + Ti.Network.encodeURIComponent(_params[p]));
    }

    if (_.indexOf(_url, "?") == -1) {
    return _url + "?" + str.join("&");
    } else {
    return _url + "&" + str.join("&");
    }
    }

    var parse = new ParseClient();
    module.exports = parse;
    module.exports = parse;
  2. @stephenfeather stephenfeather revised this gist Jan 25, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion parse.js
    Original file line number Diff line number Diff line change
    @@ -233,7 +233,7 @@ ParseClient.prototype._request = function(url, params, callback) {
    params.url = url || baseURL;
    //params.url += url;
    params.headers = params.headers || {};
    params.headers['X-Parse-Application-Id'] = appID;
    params.headers['X-Parse-Application-Id'] = appId;
    params.headers['X-Parse-REST-API-Key'] = apiKey;
    if (!params.headers['Content-Type']){
    params.headers['Content-Type'] = 'application/json';
  3. @stephenfeather stephenfeather revised this gist Jan 1, 2013. 1 changed file with 39 additions and 16 deletions.
    55 changes: 39 additions & 16 deletions parse.js
    Original file line number Diff line number Diff line change
    @@ -42,6 +42,19 @@ function ParseClient(){

    }

    ParseClient.prototype.saveUserRecord = function(user){
    Ti.App.Properties.setObject('parseUser', user);
    };

    ParseClient.prototype.setSessionToken = function(token){
    Ti.App.Properties.setString('parseSessionToken', token);
    };

    ParseClient.prototype.getSessionToken = function () {
    return Ti.App.Properties.getString('parseSessionToken');
    };


    ParseClient.prototype.createObject = function(_class, data, callback){
    var url = baseURL+'classes/'+_class;
    var params = {
    @@ -163,21 +176,22 @@ ParseClient.prototype.passwordReset = function(_email, callback){

    this._request(url, params, callback);
    };
    // -- functions below here not fully functional/tested --


    ParseClient.prototype.saveUserRecord = function(user){
    Ti.App.Properties.setObject('parseUser', user);
    };

    ParseClient.prototype.setSessionToken = function(token){
    Ti.App.Properties.setString('parseSessionToken', token);
    };

    ParseClient.prototype.getSessionToken = function () {
    return Ti.App.Properties.getString('parseSessionToken');
    ParseClient.prototype.uploadImage = function (_contentType, _filename, _blob, callback){
    var url = baseURL+'files/'+_filename;
    var params = {
    method: 'POST',
    type: 'image',
    body: _blob,
    headers : {}
    };
    params.headers['Content-Type'] = _contentType;
    //Ti.API.error(params);
    this._request(url, params, callback);
    };

    // -- functions below here not fully functional/tested --



    @@ -221,30 +235,39 @@ ParseClient.prototype._request = function(url, params, callback) {
    params.headers = params.headers || {};
    params.headers['X-Parse-Application-Id'] = appID;
    params.headers['X-Parse-REST-API-Key'] = apiKey;
    params.headers['Content-Type'] = 'application/json';
    if (!params.headers['Content-Type']){
    params.headers['Content-Type'] = 'application/json';
    }
    params.headers['Accept'] = params.headers['Accept'] || 'application/json';
    if(!('login' in params) || !params.login){

    params.headers['X-Parse-Session-Token'] = this.getSessionToken();
    }

    // Need to clear some properties depending on method
    if ((params.method === 'GET') || (params.method === 'DELETE')){
    params.body = null;
    } else {
    params.body = JSON.stringify(params.body);
    if (params.type === 'image'){
    params.body = params.body;
    } else {
    params.body = JSON.stringify(params.body);
    }
    params.query = null;
    }


    var xhr = Ti.Network.createHTTPClient();
    var xhr = Ti.Network.createHTTPClient({
    onsendstream: function(e){
    Ti.API.error(e.progress);
    }
    });

    xhr.setTimeout(params.timeout);

    xhr.onerror = function(e) {
    callback(0,this.responseText, this.status, this);
    };

    xhr.onload = function() {
    callback(1,this.responseText, this.status);
    };
  4. @stephenfeather stephenfeather revised this gist Dec 31, 2012. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions parse.js
    Original file line number Diff line number Diff line change
    @@ -121,6 +121,7 @@ ParseClient.prototype.loginUser = function(_username, _password, callback){
    if (success === 1){
    response = JSON.parse(response);
    parse.setSessionToken(response.sessionToken);
    parse.saveUserRecord(response);
    callback(success, response, code);
    } else {
    callback(success, response, code);
  5. @stephenfeather stephenfeather revised this gist Dec 31, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions parse.js
    Original file line number Diff line number Diff line change
    @@ -241,9 +241,9 @@ ParseClient.prototype._request = function(url, params, callback) {
    xhr.setTimeout(params.timeout);

    xhr.onerror = function(e) {
    callback(0,this, e);
    callback(0,this.responseText, this.status, this);
    };

    xhr.onload = function() {
    callback(1,this.responseText, this.status);
    };
  6. @stephenfeather stephenfeather revised this gist Dec 30, 2012. 1 changed file with 14 additions and 1 deletion.
    15 changes: 14 additions & 1 deletion parse.js
    Original file line number Diff line number Diff line change
    @@ -148,7 +148,20 @@ ParseClient.prototype.deleteUser = function(_userObject, callback){
    this._request(url, params, callback);
    };


    ParseClient.prototype.passwordReset = function(_email, callback){
    var url = baseURL+'requestPasswordReset';
    _email = {
    email: _email
    };

    var params = {
    method: 'POST',
    body: _email
    };


    this._request(url, params, callback);
    };
    // -- functions below here not fully functional/tested --


  7. @stephenfeather stephenfeather revised this gist Dec 30, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions parse.js
    Original file line number Diff line number Diff line change
    @@ -205,8 +205,8 @@ ParseClient.prototype._request = function(url, params, callback) {
    params.url = url || baseURL;
    //params.url += url;
    params.headers = params.headers || {};
    params.headers['X-Parse-Application-Id'] = config.parse.appID;
    params.headers['X-Parse-REST-API-Key'] = config.parse.apiKey;
    params.headers['X-Parse-Application-Id'] = appID;
    params.headers['X-Parse-REST-API-Key'] = apiKey;
    params.headers['Content-Type'] = 'application/json';
    params.headers['Accept'] = params.headers['Accept'] || 'application/json';
    if(!('login' in params) || !params.login){
  8. @stephenfeather stephenfeather revised this gist Dec 30, 2012. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions parse.js
    Original file line number Diff line number Diff line change
    @@ -139,10 +139,6 @@ ParseClient.prototype.updateUser = function(_userObject, data, callback){
    this._request(url, params, callback);
    };

    // -- functions below here not fully functional/tested --



    ParseClient.prototype.deleteUser = function(_userObject, callback){
    var url = baseURL+'users/'+_userObject;
    var params = {
    @@ -152,6 +148,10 @@ ParseClient.prototype.deleteUser = function(_userObject, callback){
    this._request(url, params, callback);
    };


    // -- functions below here not fully functional/tested --


    ParseClient.prototype.saveUserRecord = function(user){
    Ti.App.Properties.setObject('parseUser', user);
    };
  9. @stephenfeather stephenfeather revised this gist Dec 30, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion parse.js
    Original file line number Diff line number Diff line change
    @@ -139,7 +139,7 @@ ParseClient.prototype.updateUser = function(_userObject, data, callback){
    this._request(url, params, callback);
    };

    // --
    // -- functions below here not fully functional/tested --



  10. @stephenfeather stephenfeather revised this gist Dec 30, 2012. 1 changed file with 150 additions and 35 deletions.
    185 changes: 150 additions & 35 deletions parse.js
    Original file line number Diff line number Diff line change
    @@ -42,58 +42,135 @@ function ParseClient(){

    }

    ParseClient.prototype.setUser = function(user){
    Ti.App.Properties.setObject('parseUser', user);
    };

    ParseClient.prototype.setSessionToken = function(token){
    Ti.App.Properties.setString('parseSessionToken', token);
    ParseClient.prototype.createObject = function(_class, data, callback){
    var url = baseURL+'classes/'+_class;
    var params = {
    method: 'POST',
    body: data
    };

    this._request(url, params, callback);
    };

    ParseClient.prototype.getSessionToken = function () {
    return Ti.App.Properties.getString('parseSessionToken');
    ParseClient.prototype.updateObject = function(_class, _objectID, data, callback){
    var url = baseURL+'classes/'+_class+'/'+_objectID;
    var params = {
    method: 'PUT',
    body: data
    };

    this._request(url, params, callback);
    };

    ParseClient.prototype.getObjects = function(_class, callback){
    var url = baseURL+'classes/'+_class;
    this._request(url, 'GET', '', callback);
    };

    ParseClient.prototype.getObject = function(_class, objectID, callback){
    var url = baseURL+'classes/'+_class+'/'+objectID;
    this._request(url, 'GET', '', callback);
    var params = {
    method: 'GET'
    };

    this._request(url, params, callback);
    };

    ParseClient.prototype.createObject = function(_class, data, callback){
    var url = baseURL+'classes/'+_class;
    this._request(url, 'POST', data, callback);
    ParseClient.prototype.getObject = function(_class, _objectID, callback){
    var url = baseURL+'classes/'+_class+'/'+_objectID;
    this._request(url, callback);
    };

    ParseClient.prototype.updateObject = function(_class, objectID, data, callback){
    var url = baseURL+'classes/'+_class+'/'+objectID;
    this._request(url, 'PUT', data, callback);
    ParseClient.prototype.deleteObject = function(_class, _objectID, callback){
    var url = baseURL+'classes/'+_class+'/'+_objectID;
    var params = {
    method: 'DELETE'
    };

    this._request(url, params, callback);
    };

    ParseClient.prototype.newUser = function(data, callback){
    ParseClient.prototype.createUser = function(data, callback){
    var url = baseURL+'users';
    this._request(url, 'POST', data, callback);
    var params = {
    method: 'POST',
    body: data
    };

    function cb(success, response, code){
    if (success === 1){
    response = JSON.parse(response);
    parse.setSessionToken(response.sessionToken);
    callback(success, response, code);
    } else {
    callback(success, response, code);
    }
    }

    this._request(url, params, cb);
    };

    ParseClient.prototype.getUsers = function(callback){
    var url = baseURL+'users';
    this._request(url, 'GET', '', callback);
    this._request(url, callback);
    };

    ParseClient.prototype.getUser = function(_userObject, callback){
    var url = baseURL+'users/'+_userObject;
    this._request(url, 'GET', '', callback);
    this._request(url, callback);
    };

    ParseClient.prototype.loginUser = function(_username, _password, callback){
    var url = baseURL+'login?username='+_username+'&password='+_password;
    function cb(success, response, code){
    if (success === 1){
    response = JSON.parse(response);
    parse.setSessionToken(response.sessionToken);
    callback(success, response, code);
    } else {
    callback(success, response, code);
    }
    }
    this._request(url, cb);
    };

    ParseClient.prototype.updateObject = function(_userObject, data, callback){
    ParseClient.prototype.updateUser = function(_userObject, data, callback){
    var url = baseURL+'users/'+_userObject;
    this._request(url, 'PUT', data, callback);
    var params = {
    method: 'PUT',
    body: data
    };

    this._request(url, params, callback);
    };

    // --



    ParseClient.prototype.deleteUser = function(_userObject, callback){
    var url = baseURL+'users/'+_userObject;
    var params = {
    method: 'DELETE'
    };

    this._request(url, params, callback);
    };

    ParseClient.prototype.saveUserRecord = function(user){
    Ti.App.Properties.setObject('parseUser', user);
    };

    ParseClient.prototype.setSessionToken = function(token){
    Ti.App.Properties.setString('parseSessionToken', token);
    };

    ParseClient.prototype.getSessionToken = function () {
    return Ti.App.Properties.getString('parseSessionToken');
    };








    ParseClient.prototype.registerPush = function(params, callback) {
    var method = 'POST',
    url = config.parse.baseUrl + '/installations',
    @@ -108,10 +185,47 @@ ParseClient.prototype.registerPush = function(params, callback) {
    });
    };

    ParseClient.prototype._request = function(url, method, params, callback) {
    ParseClient.prototype._request = function(url, params, callback) {
    if (typeof params === 'function'){
    callback = params;
    params = {};
    }


    params = params || {};

    // Clean up the call type, defaulting to GET if no method set
    params.method = params.method || 'GET';
    params.method = params.method.toUpperCase();

    // If not specified, use a 20 second timeout
    params.timeout = ('timeout' in params) ? params.timeout : 15000;
    params.body = params.body || {};
    params.query = params.query || {};
    params.url = url || baseURL;
    //params.url += url;
    params.headers = params.headers || {};
    params.headers['X-Parse-Application-Id'] = config.parse.appID;
    params.headers['X-Parse-REST-API-Key'] = config.parse.apiKey;
    params.headers['Content-Type'] = 'application/json';
    params.headers['Accept'] = params.headers['Accept'] || 'application/json';
    if(!('login' in params) || !params.login){

    params.headers['X-Parse-Session-Token'] = this.getSessionToken();
    }

    // Need to clear some properties depending on method
    if ((params.method === 'GET') || (params.method === 'DELETE')){
    params.body = null;
    } else {
    params.body = JSON.stringify(params.body);
    params.query = null;
    }


    var xhr = Ti.Network.createHTTPClient();

    xhr.setTimeout(15000);
    xhr.setTimeout(params.timeout);

    xhr.onerror = function(e) {
    callback(0,this, e);
    @@ -122,14 +236,15 @@ ParseClient.prototype._request = function(url, method, params, callback) {
    };


    params = params.replace(/\./g, '_');
    //params = params.replace(/\./g, '_');

    xhr.open(method, url);
    xhr.setRequestHeader('X-Parse-Application-Id', config.parse.appID);
    xhr.setRequestHeader('X-Parse-REST-API-Key', config.parse.apiKey);
    xhr.setRequestHeader('Content-Type', 'application/json');
    xhr.send(params);
    xhr.open(params.method, params.url);

    for (var key in params.headers) {
    xhr.setRequestHeader(key, params.headers[key]);
    }
    xhr.send(params.body);
    };

    var parse = new ParseClient();
    module.exports = parse;
    module.exports = parse;
  11. @stephenfeather stephenfeather revised this gist Dec 29, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion parse.js
    Original file line number Diff line number Diff line change
    @@ -50,7 +50,7 @@ ParseClient.prototype.setSessionToken = function(token){
    Ti.App.Properties.setString('parseSessionToken', token);
    };

    APIClient.prototype.getSessionToken = function () {
    ParseClient.prototype.getSessionToken = function () {
    return Ti.App.Properties.getString('parseSessionToken');
    };

  12. @stephenfeather stephenfeather revised this gist Dec 29, 2012. 1 changed file with 17 additions and 20 deletions.
    37 changes: 17 additions & 20 deletions parse.js
    Original file line number Diff line number Diff line change
    @@ -37,8 +37,9 @@ var baseURL = 'https://api.parse.com/1/',
    appId = 'XXXXXXXXXXXXXXX',
    apiKey = 'XXXXXXXXXXXXXX'; // Be sure to use your REST API key and NOT your master as bad stuff can happen.


    function ParseClient(){
    }

    ParseClient.prototype.setUser = function(user){
    @@ -53,65 +54,61 @@ APIClient.prototype.getSessionToken = function () {
    return Ti.App.Properties.getString('parseSessionToken');
    };

    // Returns all the objects from a class
    ParseClient.prototype.getObjects = function(_class, callback){
    var url = baseURL+'classes/'+_class;
    this._helper(url, 'GET', '', callback);
    this._request(url, 'GET', '', callback);
    };

    // Returns an individual object
    ParseClient.prototype.getObject = function(_class, objectID, callback){
    var url = baseURL+'classes/'+_class+'/'+objectID;
    this._helper(url, 'GET', '', callback);
    this._request(url, 'GET', '', callback);
    };

    // Creates a new object in the specified class
    ParseClient.prototype.createObject = function(_class, data, callback){
    var url = baseURL+'classes/'+_class;
    this._helper(url, 'POST', data, callback);
    this._request(url, 'POST', data, callback);
    };

    // Updates an individual object
    ParseClient.prototype.updateObject = function(_class, objectID, data, callback){
    var url = baseURL+'classes/'+_class+'/'+objectID;
    this._helper(url, 'PUT', data, callback);
    this._request(url, 'PUT', data, callback);
    };

    ParseClient.prototype.newUser = function(data, callback){
    var url = baseURL+'users';
    this._helper(url, 'POST', data, callback);
    this._request(url, 'POST', data, callback);
    };

    ParseClient.prototype.getUsers = function(callback){
    var url = baseURL+'users';
    this._helper(url, 'GET', '', callback);
    this._request(url, 'GET', '', callback);
    };

    ParseClient.prototype.getUser = function(_userObject, callback){
    var url = baseURL+'users/'+_userObject;
    this._helper(url, 'GET', '', callback);
    this._request(url, 'GET', '', callback);
    };

    ParseClient.prototype.updateObject = function(_userObject, data, callback){
    var url = baseURL+'users/'+_userObject;
    this._helper(url, 'PUT', data, callback);
    this._request(url, 'PUT', data, callback);
    };

    ParseClient.prototype.registerPush = function(params, callback) {
    var method = 'POST',
    url = config.parse.baseUrl + '/installations',
    payload = (params) ? JSON.stringify(params) : '';

    this._helper(url, method, payload, function(data, status) {
    this._request(url, method, payload, function(data, status) {
    Ti.API.log('completed registration: ' + JSON.stringify(status));
    callabck(1, data, status);
    }, function(xhr, error) {
    Ti.API.log('xhr error registration: ' + JSON.stringify(error));
    callback(0,error);
    });
    };

    ParseClient.prototype._helper = function(url, method, params, callback) {
    ParseClient.prototype._request = function(url, method, params, callback) {
    var xhr = Ti.Network.createHTTPClient();

    xhr.setTimeout(15000);
    @@ -128,11 +125,11 @@ ParseClient.prototype._helper = function(url, method, params, callback) {
    params = params.replace(/\./g, '_');

    xhr.open(method, url);
    xhr.setRequestHeader('X-Parse-Application-Id', appID);
    xhr.setRequestHeader('X-Parse-REST-API-Key', apiKey);
    xhr.setRequestHeader('X-Parse-Application-Id', config.parse.appID);
    xhr.setRequestHeader('X-Parse-REST-API-Key', config.parse.apiKey);
    xhr.setRequestHeader('Content-Type', 'application/json');
    xhr.send(params);
    };

    var parse = new ParseClient();
    module.exports = parse;
    module.exports = parse;
  13. @stephenfeather stephenfeather revised this gist Dec 29, 2012. 1 changed file with 21 additions and 0 deletions.
    21 changes: 21 additions & 0 deletions parse.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,24 @@
    // Copyright Stephen Feather and other contributors.
    //
    // Permission is hereby granted, free of charge, to any person obtaining a
    // copy of this software and associated documentation files (the
    // "Software"), to deal in the Software without restriction, including
    // without limitation the rights to use, copy, modify, merge, publish,
    // distribute, sublicense, and/or sell copies of the Software, and to permit
    // persons to whom the Software is furnished to do so, subject to the
    // following conditions:
    //
    // The above copyright notice and this permission notice shall be included
    // in all copies or substantial portions of the Software.
    //
    // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
    // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
    // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
    // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
    // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
    // USE OR OTHER DEALINGS IN THE SOFTWARE.

    /**
    * Usage:
    * var Parse = require('parse');
  14. @stephenfeather stephenfeather revised this gist Dec 29, 2012. 1 changed file with 12 additions and 0 deletions.
    12 changes: 12 additions & 0 deletions parse.js
    Original file line number Diff line number Diff line change
    @@ -20,6 +20,18 @@ function ParseClient(){

    }

    ParseClient.prototype.setUser = function(user){
    Ti.App.Properties.setObject('parseUser', user);
    };

    ParseClient.prototype.setSessionToken = function(token){
    Ti.App.Properties.setString('parseSessionToken', token);
    };

    APIClient.prototype.getSessionToken = function () {
    return Ti.App.Properties.getString('parseSessionToken');
    };

    // Returns all the objects from a class
    ParseClient.prototype.getObjects = function(_class, callback){
    var url = baseURL+'classes/'+_class;
  15. @stephenfeather stephenfeather revised this gist Dec 28, 2012. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions parse.js
    Original file line number Diff line number Diff line change
    @@ -49,6 +49,11 @@ ParseClient.prototype.newUser = function(data, callback){
    this._helper(url, 'POST', data, callback);
    };

    ParseClient.prototype.getUsers = function(callback){
    var url = baseURL+'users';
    this._helper(url, 'GET', '', callback);
    };

    ParseClient.prototype.getUser = function(_userObject, callback){
    var url = baseURL+'users/'+_userObject;
    this._helper(url, 'GET', '', callback);
  16. @stephenfeather stephenfeather revised this gist Dec 28, 2012. 1 changed file with 15 additions and 0 deletions.
    15 changes: 15 additions & 0 deletions parse.js
    Original file line number Diff line number Diff line change
    @@ -44,6 +44,21 @@ ParseClient.prototype.updateObject = function(_class, objectID, data, callback){
    this._helper(url, 'PUT', data, callback);
    };

    ParseClient.prototype.newUser = function(data, callback){
    var url = baseURL+'users';
    this._helper(url, 'POST', data, callback);
    };

    ParseClient.prototype.getUser = function(_userObject, callback){
    var url = baseURL+'users/'+_userObject;
    this._helper(url, 'GET', '', callback);
    };

    ParseClient.prototype.updateObject = function(_userObject, data, callback){
    var url = baseURL+'users/'+_userObject;
    this._helper(url, 'PUT', data, callback);
    };

    ParseClient.prototype.registerPush = function(params, callback) {
    var method = 'POST',
    url = config.parse.baseUrl + '/installations',
  17. @stephenfeather stephenfeather revised this gist Dec 28, 2012. 1 changed file with 18 additions and 0 deletions.
    18 changes: 18 additions & 0 deletions parse.js
    Original file line number Diff line number Diff line change
    @@ -7,11 +7,15 @@
    * Notes: Some folks like to do their error handling/forking in the Library.
    * I don't. So I only pass one callback into each function.
    * if you want a specific error function, add another param.
    *
    * Push notification registration borrowed/modified from Matt Berg (https://gist.github.com/3761738).
    *
    */

    var baseURL = 'https://api.parse.com/1/',
    appId = 'XXXXXXXXXXXXXXX',
    apiKey = 'XXXXXXXXXXXXXX'; // Be sure to use your REST API key and NOT your master as bad stuff can happen.

    function ParseClient(){

    }
    @@ -40,6 +44,20 @@ ParseClient.prototype.updateObject = function(_class, objectID, data, callback){
    this._helper(url, 'PUT', data, callback);
    };

    ParseClient.prototype.registerPush = function(params, callback) {
    var method = 'POST',
    url = config.parse.baseUrl + '/installations',
    payload = (params) ? JSON.stringify(params) : '';

    this._helper(url, method, payload, function(data, status) {
    Ti.API.log('completed registration: ' + JSON.stringify(status));
    callabck(1, data, status);
    }, function(xhr, error) {
    Ti.API.log('xhr error registration: ' + JSON.stringify(error));
    callback(0,error);
    });
    };

    ParseClient.prototype._helper = function(url, method, params, callback) {
    var xhr = Ti.Network.createHTTPClient();

  18. @stephenfeather stephenfeather revised this gist Dec 28, 2012. 1 changed file with 5 additions and 4 deletions.
    9 changes: 5 additions & 4 deletions parse.js
    Original file line number Diff line number Diff line change
    @@ -9,8 +9,9 @@
    * if you want a specific error function, add another param.
    */

    var baseURL = 'https://api.parse.com/1/';

    var baseURL = 'https://api.parse.com/1/',
    appId = 'XXXXXXXXXXXXXXX',
    apiKey = 'XXXXXXXXXXXXXX'; // Be sure to use your REST API key and NOT your master as bad stuff can happen.
    function ParseClient(){

    }
    @@ -56,8 +57,8 @@ ParseClient.prototype._helper = function(url, method, params, callback) {
    params = params.replace(/\./g, '_');

    xhr.open(method, url);
    xhr.setRequestHeader('X-Parse-Application-Id', config.parse.appID);
    xhr.setRequestHeader('X-Parse-REST-API-Key', config.parse.apiKey);
    xhr.setRequestHeader('X-Parse-Application-Id', appID);
    xhr.setRequestHeader('X-Parse-REST-API-Key', apiKey);
    xhr.setRequestHeader('Content-Type', 'application/json');
    xhr.send(params);
    };
  19. @stephenfeather stephenfeather revised this gist Dec 28, 2012. 1 changed file with 8 additions and 4 deletions.
    12 changes: 8 additions & 4 deletions parse.js
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,10 @@
    * var Parse = require('parse');
    *
    * Parse.getObjects('myClass', myCallback);
    *
    * Notes: Some folks like to do their error handling/forking in the Library.
    * I don't. So I only pass one callback into each function.
    * if you want a specific error function, add another param.
    */

    var baseURL = 'https://api.parse.com/1/';
    @@ -14,25 +18,25 @@ function ParseClient(){
    // Returns all the objects from a class
    ParseClient.prototype.getObjects = function(_class, callback){
    var url = baseURL+'classes/'+_class;
    this._helper(url, 'GET', '');
    this._helper(url, 'GET', '', callback);
    };

    // Returns an individual object
    ParseClient.prototype.getObject = function(_class, objectID, callback){
    var url = baseURL+'classes/'+_class+'/'+objectID;
    this._helper(url, 'GET', '');
    this._helper(url, 'GET', '', callback);
    };

    // Creates a new object in the specified class
    ParseClient.prototype.createObject = function(_class, data, callback){
    var url = baseURL+'classes/'+_class;
    this._helper(url, 'POST', data);
    this._helper(url, 'POST', data, callback);
    };

    // Updates an individual object
    ParseClient.prototype.updateObject = function(_class, objectID, data, callback){
    var url = baseURL+'classes/'+_class+'/'+objectID;
    this._helper(url, 'PUT', data);
    this._helper(url, 'PUT', data, callback);
    };

    ParseClient.prototype._helper = function(url, method, params, callback) {
  20. @stephenfeather stephenfeather revised this gist Dec 28, 2012. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions parse.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,8 @@
    /**
    * Usage:
    * var Parse = require('parse');
    *
    *
    *
    * Parse.getObjects('myClass', myCallback);
    */

    var baseURL = 'https://api.parse.com/1/';
  21. @stephenfeather stephenfeather revised this gist Dec 28, 2012. 1 changed file with 10 additions and 1 deletion.
    11 changes: 10 additions & 1 deletion parse.js
    Original file line number Diff line number Diff line change
    @@ -1,25 +1,34 @@
    /**
    *
    *
    *
    */

    var baseURL = 'https://api.parse.com/1/';

    function ParseClient(){

    }

    // Returns all the objects from a class
    ParseClient.prototype.getObjects = function(_class, callback){
    var url = baseURL+'classes/'+_class;
    this._helper(url, 'GET', '');
    };

    // Returns an individual object
    ParseClient.prototype.getObject = function(_class, objectID, callback){
    var url = baseURL+'classes/'+_class+'/'+objectID;
    this._helper(url, 'GET', '');
    };


    // Creates a new object in the specified class
    ParseClient.prototype.createObject = function(_class, data, callback){
    var url = baseURL+'classes/'+_class;
    this._helper(url, 'POST', data);
    };

    // Updates an individual object
    ParseClient.prototype.updateObject = function(_class, objectID, data, callback){
    var url = baseURL+'classes/'+_class+'/'+objectID;
    this._helper(url, 'PUT', data);
  22. @stephenfeather stephenfeather created this gist Dec 28, 2012.
    52 changes: 52 additions & 0 deletions parse.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    var baseURL = 'https://api.parse.com/1/';

    function ParseClient(){

    }

    ParseClient.prototype.getObjects = function(_class, callback){
    var url = baseURL+'classes/'+_class;
    this._helper(url, 'GET', '');
    };

    ParseClient.prototype.getObject = function(_class, objectID, callback){
    var url = baseURL+'classes/'+_class+'/'+objectID;
    this._helper(url, 'GET', '');
    };


    ParseClient.prototype.createObject = function(_class, data, callback){
    var url = baseURL+'classes/'+_class;
    this._helper(url, 'POST', data);
    };

    ParseClient.prototype.updateObject = function(_class, objectID, data, callback){
    var url = baseURL+'classes/'+_class+'/'+objectID;
    this._helper(url, 'PUT', data);
    };

    ParseClient.prototype._helper = function(url, method, params, callback) {
    var xhr = Ti.Network.createHTTPClient();

    xhr.setTimeout(15000);

    xhr.onerror = function(e) {
    callback(0,this, e);
    };

    xhr.onload = function() {
    callback(1,this.responseText, this.status);
    };


    params = params.replace(/\./g, '_');

    xhr.open(method, url);
    xhr.setRequestHeader('X-Parse-Application-Id', config.parse.appID);
    xhr.setRequestHeader('X-Parse-REST-API-Key', config.parse.apiKey);
    xhr.setRequestHeader('Content-Type', 'application/json');
    xhr.send(params);
    };

    var parse = new ParseClient();
    module.exports = parse;