-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathparse.gs
130 lines (115 loc) · 3.56 KB
/
parse.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Returns an object with the given attribute key/value pair
// If the object exists with that attribute, it merely finds and returns it.
// If the object does not exist, it creates and returns it.
// Example:
// obj = parseFindOrCreateByAttribute("GameScore", {
// "playerName" : "Sean Plott"
// });
// If multiple results are returned from the parseQuery, an array of the results is returned here.
// If one result is returned from the parseQuery, just that result is returned here.
function parseFindOrCreateByAttribute(className, attributeNameAndValue) {
var obj = parseQuery(className, attributeNameAndValue);
var result;
if (obj == false) {
result = parseInsert(className, attributeNameAndValue);
}
else {
if (obj.length > 1) { result = obj; }
else { result = obj[0]; }
}
return result;
}
// Query Parse to get results from server
// Example:
// var results = parseQuery("GameScore", {
// "playerName" : "Sean Plott"
// });
// results[0].playerName #=> Sean Plott
function parseQuery(className, params) {
var encoded_params = encodeURIComponent(Utilities.jsonStringify(params));
var url = "https://api.parse.com/1/classes/" + className + "?where=" + encoded_params;
var options = {
"method" : "get",
"headers" : makeHeaders(),
};
var resp = UrlFetchApp.fetch(url, options);
var result;
if (resp.getResponseCode() != 200) {
Logger.log(resp.getContentText());
result = false;
} else {
result = Utilities.jsonParse(resp.getContentText())["results"];
}
return result;
}
// Update an existing Parse object
// Example:
// parseUpdate("GameScore", "6K0FnTtGZC", {
// "playerName" : "Sean Plott Jr."
//});
function parseUpdate(className, objectId, params) {
var url = "https://api.parse.com/1/classes/" + className + "/" + objectId;
var payload = Utilities.jsonStringify(params);
var options = {
"method" : "put",
"payload" : payload,
"headers" : makeHeaders(),
"contentType" : "application/json"
};
var resp = UrlFetchApp.fetch(url, options);
var result;
if (resp.getResponseCode() != 200) {
Logger.log(resp.getContentText());
result = false;
} else {
result = Utilities.jsonParse(resp.getContentText());
}
return result;
}
// Sent POST request to insert into the database
// Example:
// parseInsert("GameScore", {
// "playerName" : "Sean Plott",
// "score" : "1337"
// });
function parseInsert(className, params) {
var url = "https://api.parse.com/1/classes/" + className;
var payload = Utilities.jsonStringify(params);
var options = {
"method" : "post",
"payload" : payload,
"headers" : makeHeaders(),
"contentType" : "application/json"
};
var resp = UrlFetchApp.fetch(url, options);
var result;
if (resp.getResponseCode() != 200) {
Logger.log(resp.getContentText());
result = false;
} else {
result = Utilities.jsonParse(resp.getContentText());
}
return result;
}
// Looks for the first Sheet in a Speadsheet
// and gets the first two rows of the 2nd column
function getAPIKeys() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
SpreadsheetApp.setActiveSheet(ss.getSheets()[0]);
var sheet = SpreadsheetApp.getActiveSheet();
var rows = getRows(sheet);
keys = {
"application_id" : rows[0][1],
"rest_api_key" : rows[1][1]
};
return keys;
}
// calls getAPIKeys() and assembles into HTTP headers
function makeHeaders() {
var keys = getAPIKeys();
var headers = {
"X-Parse-Application-Id": keys["application_id"],
"X-Parse-REST-API-Key": keys["rest_api_key"]
};
return headers;
}