Permalink
Browse files

updated Parse.js

  • Loading branch information...
Leveton committed Jun 4, 2012
1 parent 3762082 commit 1874bb483451bfa3091896f3357bcadee5b2856b
Showing with 62 additions and 5 deletions.
  1. +62 −5 lib/Parse.js
View
@@ -17,33 +17,79 @@ Parse.prototype = {
parseRequest.call(this, 'POST', '/1/classes/' + className, object, callback);
},
+ // add files
+ insertFile: function(fileName, data, contentType, callback){
+ parseRequest.call(this, 'POST', '/1/files/' + fileName, data, callback, contentType);
+ },
+
// get objects from class store
find: function (className, query, callback) {
if (typeof query === 'string') {
- parseRequest.call(this, 'GET', '/1/classes/' + className + '/' + query, null, callback);
+ parseRequest.call(this, 'GET', '/1/' + className + '/' + query, null, callback);
} else {
- parseRequest.call(this, 'GET', '/1/classes/' + className, { where: JSON.stringify(query) }, callback);
+ parseRequest.call(this, 'GET', '/1/' + className, { where: JSON.stringify(query) }, callback);
}
},
+ // get a user from the Parse's special User class. See https://parse.com/questions/why-does-querying-for-a-user-create-a-second-user-class
+ getUser: function (userName, passWord, callback) {
+ parseRequest.call(this, 'GET', '/1/login/?username=' + userName + '&password=' + passWord, null, callback);
+ },
+
+ // get an object belonging to a certain User
+ getFileByUser: function(userId, callback) {
+ queryString = 'where={"user":' + '"' + userId + '"' + '}'
+ encodedString = encodeURIComponent(queryString);
+ parseRequest.call(this, 'GET', '/1/classes/contracts?' + encodedString, null, callback)
+ },
+
+ // insert an object into Parse
+ insertCustom: function (className, object, callback) {
+ parseRequest.call(this, 'POST', '/1/' + className, object, callback);
+ },
+
// update an object in the class store
update: function (className, objectId, object, callback) {
parseRequest.call(this, 'PUT', '/1/classes/' + className + '/' + objectId, object, callback);
},
+
+ // update a User's email address
+ updateUserEmail: function(objectId, data, callback) {
+ data = { email: data }
+ parseRequest.call(this, 'PUT', '/1/users/' + objectId, data, callback)
+ },
+
+ // update a User's username
+ updateUserName: function(objectId, data, callback) {
+ data = { username: data }
+ parseRequest.call(this, 'PUT', '/1/users/' + objectId, data, callback)
+ },
+
+ // reset a User's password
+ passwordReset: function (data, callback) {
+ data = { email: data }
+ parseRequest.call(this, 'POST', '/1/requestPasswordReset/', data, callback)
+ },
+ // delete a file. Note: file still shows up on the parse databrowser, but attempting to access it will result in a 401
+ deleteFile: function (fileName, callback) {
+ parseRequest.call(this, 'DELETE', '/1/files/' + fileName, null, callback)
+ },
+
// remove an object from the class store
'delete': function (className, objectId, callback) {
parseRequest.call(this, 'DELETE', '/1/classes/' + className + '/' + objectId, null, callback);
}
};
// Parse.com https api request
-function parseRequest(method, path, data, callback) {
+function parseRequest(method, path, data, callback, contentType) {
var auth = 'Basic ' + new Buffer(this._application_id + ':' + this._master_key).toString('base64');
var headers = {
Authorization: auth,
Connection: 'Keep-alive'
};
+
var body = null;
switch (method) {
@@ -54,8 +100,14 @@ function parseRequest(method, path, data, callback) {
break;
case 'POST':
case 'PUT':
- body = JSON.stringify(data);
+ if(contentType){
+ body = data;
+ headers['Content-type'] = contentType;
+ console.log('Sending data type: ' + contentType + ' of length: ' + body.length);
+ }else{
headers['Content-type'] = 'application/json';
+ body = JSON.stringify(data);
+ }
headers['Content-length'] = body.length;
break;
case 'DELETE':
@@ -109,10 +161,15 @@ function parseRequest(method, path, data, callback) {
});
});
+ if(contentType)
+ {
+ body && req.write(body,'binary');
+ }else{
body && req.write(body);
+ }
req.end();
req.on('error', function (err) {
callback && callback(err);
});
-}
+}

0 comments on commit 1874bb4

Please sign in to comment.