Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

updated Parse.js

  • Loading branch information...
commit 1874bb483451bfa3091896f3357bcadee5b2856b 1 parent 3762082
Michael Leveton authored

Showing 1 changed file with 62 additions and 5 deletions. Show diff stats Hide diff stats

  1. +62 5 lib/Parse.js
67 lib/Parse.js
@@ -17,20 +17,65 @@ Parse.prototype = {
17 17 parseRequest.call(this, 'POST', '/1/classes/' + className, object, callback);
18 18 },
19 19
  20 + // add files
  21 + insertFile: function(fileName, data, contentType, callback){
  22 + parseRequest.call(this, 'POST', '/1/files/' + fileName, data, callback, contentType);
  23 + },
  24 +
20 25 // get objects from class store
21 26 find: function (className, query, callback) {
22 27 if (typeof query === 'string') {
23   - parseRequest.call(this, 'GET', '/1/classes/' + className + '/' + query, null, callback);
  28 + parseRequest.call(this, 'GET', '/1/' + className + '/' + query, null, callback);
24 29 } else {
25   - parseRequest.call(this, 'GET', '/1/classes/' + className, { where: JSON.stringify(query) }, callback);
  30 + parseRequest.call(this, 'GET', '/1/' + className, { where: JSON.stringify(query) }, callback);
26 31 }
27 32 },
28 33
  34 + // 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
  35 + getUser: function (userName, passWord, callback) {
  36 + parseRequest.call(this, 'GET', '/1/login/?username=' + userName + '&password=' + passWord, null, callback);
  37 + },
  38 +
  39 + // get an object belonging to a certain User
  40 + getFileByUser: function(userId, callback) {
  41 + queryString = 'where={"user":' + '"' + userId + '"' + '}'
  42 + encodedString = encodeURIComponent(queryString);
  43 + parseRequest.call(this, 'GET', '/1/classes/contracts?' + encodedString, null, callback)
  44 + },
  45 +
  46 + // insert an object into Parse
  47 + insertCustom: function (className, object, callback) {
  48 + parseRequest.call(this, 'POST', '/1/' + className, object, callback);
  49 + },
  50 +
29 51 // update an object in the class store
30 52 update: function (className, objectId, object, callback) {
31 53 parseRequest.call(this, 'PUT', '/1/classes/' + className + '/' + objectId, object, callback);
32 54 },
  55 +
  56 + // update a User's email address
  57 + updateUserEmail: function(objectId, data, callback) {
  58 + data = { email: data }
  59 + parseRequest.call(this, 'PUT', '/1/users/' + objectId, data, callback)
  60 + },
  61 +
  62 + // update a User's username
  63 + updateUserName: function(objectId, data, callback) {
  64 + data = { username: data }
  65 + parseRequest.call(this, 'PUT', '/1/users/' + objectId, data, callback)
  66 + },
  67 +
  68 + // reset a User's password
  69 + passwordReset: function (data, callback) {
  70 + data = { email: data }
  71 + parseRequest.call(this, 'POST', '/1/requestPasswordReset/', data, callback)
  72 + },
33 73
  74 + // delete a file. Note: file still shows up on the parse databrowser, but attempting to access it will result in a 401
  75 + deleteFile: function (fileName, callback) {
  76 + parseRequest.call(this, 'DELETE', '/1/files/' + fileName, null, callback)
  77 + },
  78 +
34 79 // remove an object from the class store
35 80 'delete': function (className, objectId, callback) {
36 81 parseRequest.call(this, 'DELETE', '/1/classes/' + className + '/' + objectId, null, callback);
@@ -38,12 +83,13 @@ Parse.prototype = {
38 83 };
39 84
40 85 // Parse.com https api request
41   -function parseRequest(method, path, data, callback) {
  86 +function parseRequest(method, path, data, callback, contentType) {
42 87 var auth = 'Basic ' + new Buffer(this._application_id + ':' + this._master_key).toString('base64');
43 88 var headers = {
44 89 Authorization: auth,
45 90 Connection: 'Keep-alive'
46 91 };
  92 +
47 93 var body = null;
48 94
49 95 switch (method) {
@@ -54,8 +100,14 @@ function parseRequest(method, path, data, callback) {
54 100 break;
55 101 case 'POST':
56 102 case 'PUT':
57   - body = JSON.stringify(data);
  103 + if(contentType){
  104 + body = data;
  105 + headers['Content-type'] = contentType;
  106 + console.log('Sending data type: ' + contentType + ' of length: ' + body.length);
  107 + }else{
58 108 headers['Content-type'] = 'application/json';
  109 + body = JSON.stringify(data);
  110 + }
59 111 headers['Content-length'] = body.length;
60 112 break;
61 113 case 'DELETE':
@@ -109,10 +161,15 @@ function parseRequest(method, path, data, callback) {
109 161 });
110 162 });
111 163
  164 + if(contentType)
  165 + {
  166 + body && req.write(body,'binary');
  167 + }else{
112 168 body && req.write(body);
  169 + }
113 170 req.end();
114 171
115 172 req.on('error', function (err) {
116 173 callback && callback(err);
117 174 });
118   -}
  175 +}

0 comments on commit 1874bb4

Please sign in to comment.
Something went wrong with that request. Please try again.