This repository has been archived by the owner on Jun 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathindex.js
349 lines (325 loc) · 9.29 KB
/
index.js
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/**
* JawboneUp API wrapper for Node.js
*
* @module jawbone-up
* @author Ryan Seys
*/
const packagejson = require('./package.json');
const request = require('request');
const BASE_URL = 'https://jawbone.com/nudge/api/v.1.1';
const ERROR_NO_XID = 'Error: No xid provided. Please specify a xid.';
const ERROR_NOT_IMPLEMENTED = 'Error: Not yet implemented.';
const ERROR_BAD_PARAMS_CB = 'Error: Bad parameters. Callback function required.';
const ERROR_BAD_PARAMS_OPT = 'Error: Bad parameters. Options object required.';
const ERROR_NO_CLIENT_SECRET = "Error: No client secret provided.";
const ERROR_NO_URL_PROVIDED = "Error: No URL provided.";
module.exports = function(options) {
var self = this;
if(typeof(options) !== 'object') {
options = {};
}
var version = packagejson.version;
var client_id = options.client_id;
var client_secret = options.client_secret;
var access_token = options.access_token;
/**
* Serializes an object into a parameter string
* for use with making REST API calls.
*
* @tutorial http://stackoverflow.com/a/1714899/2953164
* @private
* @param {Object} obj Object to serialize.
* @return {String} String representation of object as param string.
*/
var serialize = function(obj) {
var str = [];
for(var p in obj) {
if (obj.hasOwnProperty(p)) {
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
}
}
return str.join("&");
};
/**
* Checks if params are good. If they are, returns true.
* Else it will return false and call the callback with an error (if available).
*
* @private
* @param {Object} options Object part of params to check.
* @param {Function} callback Callback function part of params to check.
* @return {Boolean} true if params are good, false otherwise.
*/
var good_params = function(options, callback) {
if(typeof(callback) !== 'function') {
return false;
}
else if(typeof(options) !== 'object') {
var error = { error: true, message: ERROR_BAD_PARAMS_OPT };
callback(error, null);
return false;
}
else {
return true;
}
};
/**
* Makes a GET request to the API with options.
*
* @private
* @param {Object} options Options that includes the `url` to GET.
* @param {Function} callback Function to callback with response.
*/
var api_get = function(options, callback) {
request({
url: options.url,
headers: {
'Authorization': 'Bearer ' + access_token,
'Accept': 'application/json'
}
},
function(error, response, body) {
callback(error, body);
});
};
/**
* Makes a POST request to the API with options.
*
* @private
* @param {Object} options Options that includes the `url` to POST and
* `data` object to include in POST request.
*
* @param {Function} callback Function to callback with response.
*/
var api_post = function(options, callback) {
request({
method: 'post',
url: options.url,
headers: {
'Authorization': 'Bearer ' + access_token,
'Content-Type': 'application/x-www-form-urlencoded'
},
form: options.data
},
function(error, response, body) {
callback(error, body);
});
};
/**
* Makes a DELETE request to the API with options.
*
* @private
* @param {Object} options Options that includes the `url` to DELETE.
* @param {Function} callback Function to callback with response.
*/
var api_delete = function(options, callback) {
request({
method: 'delete',
url: options.url,
headers: {
'Authorization': 'Bearer ' + access_token
}
},
function(error, response, body) {
callback(error, body);
});
};
function create_getter_xid(str) {
return function(options, callback) {
if(good_params(options, callback)) {
var xid = options.xid;
var url = BASE_URL + (xid ? '/' + str + '/' + xid : '/users/@me/' + str + '?' + serialize(options));
api_get({ url: url }, callback);
}
};
}
function create_getter(str) {
return function(options, callback) {
if(good_params(options, callback)) {
var url = BASE_URL + '/users/@me/' + str;
api_get({ url: url }, callback);
}
};
}
function create_getter_type(obj_str, str_type) {
return function(options, callback) {
if(good_params(options, callback)) {
var xid = options.xid;
if(!xid) {
callback({ error: true, message: ERROR_NO_XID }, null);
}
else {
var url = BASE_URL + '/'+ obj_str + '/' + xid + '/' + str_type;
api_get({ url: url }, callback);
}
}
};
}
function create_creator(obj_str) {
return function(data, callback) {
if(good_params(data, callback)) {
var url = BASE_URL + '/users/@me/' + obj_str;
api_post({ url: url, data: data }, callback);
}
};
}
function create_deletor(obj_str) {
return function(options, callback) {
if(good_params(options, callback)) {
var xid = options.xid;
if(!xid) {
return callback({ error: true, message: ERROR_NO_XID }, null);
}
else {
var url = BASE_URL + '/' + obj_str + '/' + xid;
api_delete({ url: url }, callback);
}
}
};
}
function create_updater(obj_str) {
return function(options, callback) {
if(good_params(options, callback)) {
var xid = options.xid;
if(!xid) {
return callback({ error: true, message: ERROR_NO_XID }, null);
}
else {
var url = BASE_URL + '/' + obj_str + '/' + xid + '/partialUpdate';
api_post({ url: url, data: options.data }, callback);
}
}
};
}
var get_refreshToken = function(callback) {
if(client_secret) {
var url = BASE_URL + '/users/@me/refreshToken';
api_post({ url: url, data: { secret: client_secret } }, callback);
}
else {
var error = { error: true, message: ERROR_NO_CLIENT_SECRET };
callback(error, null);
return false;
}
};
var create_webhook = function(webhook_url, callback) {
if(webhook_url) {
var url = BASE_URL + '/users/@me/pubsub?webhook=' + webhook_url;
api_post({ url: url }, callback);
}
else {
var error = { error: true, message: ERROR_NO_URL_PROVIDED };
callback(error, null);
return false;
}
};
var delete_webhook = function(callback) {
if(good_params(options, callback)) {
var url = BASE_URL + '/users/@me/pubsub';
api_delete({ url: url }, callback);
}
};
return {
get access_token() {
return access_token;
},
set access_token(token) {
if(typeof(token) === 'string') {
access_token = token;
}
},
get version() {
return version;
},
/** @class me */
me: {
get: create_getter('')
},
/** @class moves */
moves: {
get: create_getter_xid('moves'),
image: create_getter_type('moves', 'image'),
ticks: create_getter_type('moves', 'ticks')
},
/** @class meals */
meals: {
get: create_getter_xid('meals'),
create: create_creator('meals'),
'delete': create_deletor('meals'),
update: create_updater('meals')
},
/** @class events */
events: {
/** @class events.body */
body: {
get: create_getter_xid('body_events'),
create: create_creator('body_events'),
'delete': create_deletor('body_events')
},
/** @class events.cardiac */
cardiac: {
get: create_getter_xid('cardiac_events'),
create: create_creator('cardiac_events'),
'delete': create_deletor('cardiac_events')
},
/** @class events.generic */
generic: {
get: create_getter_xid('generic_events'),
create: create_creator('generic_events'),
update: create_updater('generic_events'),
'delete': create_deletor('generic_events')
},
band: {
get: create_getter('bandevents')
}
},
/** @class timezone */
timezone: {
get: create_getter('timezone')
},
/** @class friends */
friends: {
get: create_getter('friends')
},
/** @class mood */
mood: {
get: create_getter_xid('mood'),
create: create_creator('mood'),
'delete': create_deletor('mood')
},
/** @class workouts */
workouts: {
get: create_getter_xid('workouts'),
create: create_creator('workouts'),
image: create_getter_type('workouts', 'image'),
ticks: create_getter_type('workouts', 'ticks'),
update: create_updater('workouts'),
'delete': create_deletor('workouts')
},
/** @class trends */
trends: {
get: create_getter('trends')
},
/** @class goals */
goals: {
get: create_getter('goals')
},
/** @class sleeps */
sleeps: {
get: create_getter_xid('sleeps'),
image: create_getter_type('sleeps', 'image'),
ticks: create_getter_type('sleeps', 'ticks'),
create: create_creator('sleeps'),
'delete': create_deletor('sleeps')
},
refreshToken: {
get: get_refreshToken
},
settings: {
get: create_getter('settings')
},
webhook: {
create: create_webhook,
'delete': delete_webhook
}
};
};