This repository has been archived by the owner on Dec 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
rtm.js
274 lines (234 loc) · 7.77 KB
/
rtm.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
/**
* Javascript Library - Remember The Milk
*
* @author Michael Day
* @since January 27th, 2013
* @see http://www.rememberthemilk.com/services/api/
*
* Uses crypto module under node.js. Requires a global
* md5 function on other platforms. I recommend this one:
* http://www.myersdaily.org/joseph/javascript/md5-text.html
*
* Based on RTM PHP Library by Adam Magaña
* @see https://github.com/adammagana/rtm-php-library
*
* License (The MIT License)
*
* Copyright (c) 2011 Michael Day <manveru.alma@gmail.com>
*
* 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.
*/
(function (root, factory) {
if (typeof exports === "object" && exports) {
module.exports = factory; // CommonJS
} else if (typeof define === "function" && define.amd) {
define(factory); // AMD
} else {
root.RememberTheMilk = factory; // <script>
}
}(this, (function () {
var exports = function (appKey, appSecret, permissions, format) {
var https, crypto, ajax;
this.authUrl = 'https://www.rememberthemilk.com/services/auth/';
this.baseUrl = 'https://api.rememberthemilk.com/services/rest/';
this.isPebble = (typeof Pebble !== 'undefined');
this.isWinJS = (typeof WinJS !== 'undefined');
this.isNode = (!this.isPebble && typeof module !== 'undefined' && module.exports);
this.isFirefoxOS = (typeof MozActivity !== 'undefined'); //Best way to do it right now (also working on Fifrefox for Android, and temporary as everything is built to eventually be a standard)
if (this.isNode) {
https = require('https');
crypto = require('crypto');
this.md5 = function(string) {
return crypto.createHash('md5').update(string, 'utf8').digest("hex");
};
} else if (this.isPebble) {
this.md5 = require('md5');
ajax = require('ajax');
} else {
this.md5 = md5;
}
var appKey = (appKey) ? appKey : '',
appSecret = (appSecret) ? appSecret : '',
permissions = (permissions) ? permissions : 'read',
format = (format) ? format : 'json';
if (!appKey || !appSecret) {
throw 'Error: App Key and Secret Key must be defined.';
}
this.appKey = appKey;
this.appSecret = appSecret;
this.permissions = permissions;
this.format = format;
/**
* Encodes request parameters into URL format
*
* @param params Array of parameters to be URL encoded
* @param signed Boolean specfying whether or not the URL should be signed
* @return Returns the URL encoded string of parameters
*/
this.encodeUrlParams = function (params, signed, excludeFormat) {
var params = (params) ? params : {},
signed = (signed) ? signed : false,
excludeFormat = (excludeFormat) ? excludeFormat : false,
paramString = '',
count;
if(!excludeFormat){
params.format = this.format;
}
params.api_key = this.appKey;
count = 0;
// Encode the parameter keys and values
for (var key in params) {
if (count === 0) {
paramString += '?' + key + '=' + encodeURIComponent(params[key]);
} else {
paramString += '&' + key + '=' + encodeURIComponent(params[key]);
}
count++;
}
// Append an auth signature if needed
if (signed) {
paramString += this.generateSig(params);
}
return paramString;
};
/**
* Generates a URL encoded authentication signature
*
* @param params The parameters used to generate the signature
* @return Returns the URL encoded authentication signature
*/
this.generateSig = function (params) {
var params = (params) ? params : {},
signature,
signatureUrl,
i,
keys;
signature = '';
signatureUrl = '&api_sig=';
keys = Object.keys(params);
keys.sort();
for (i = 0; i < keys.length; i++) {
signature += keys[i] + params[keys[i]];
}
signature = this.appSecret + signature;
signatureUrl += this.md5(signature);
return signatureUrl;
};
/**
* Generates a RTM authentication URL
*
* @param frob Optional frob for use in desktop applications
* @return Returns the reponse from the RTM API
*/
this.getAuthUrl = function (frob) {
var params, url;
params = {
api_key: this.appKey,
perms: this.permissions
};
if (frob) {
params.frob = frob;
}
url = this.authUrl + this.encodeUrlParams(params, true, true);
return url;
};
/**
* Main method for making API calls
*
* @param method Specifies what API method to be used
* @param params Array of API parameters to accompany the method parameter
* @param callback Callback to fire after the request comes back
* @return Returns the reponse from the RTM API
*/
this.get = function (method, params, callback) {
var method = (method) ? method : '',
params = (params) ? params : {},
callbackName,
requestUrl,
s;
if (!callback && typeof params == 'function') {
callback = params;
params = {};
}
if (!callback) {
callback = function () {};
}
if (!method) {
throw 'Error: API Method must be defined.';
}
params.method = method;
if (!this.isPebble && !this.isWinJS && !this.isNode && !this.isFirefoxOS) {
callbackName = 'RememberTheMilk' + new Date().getTime();
params.callback = callbackName;
}
if (this.auth_token) {
params.auth_token = this.auth_token;
}
requestUrl = this.baseUrl + this.encodeUrlParams(params, true);
if (this.isPebble) {
ajax({url: requestUrl, type: 'json'},
function completed(data, status, request) {
callback.call(this, data);
},
function failure(error, status, request) {
console.log("AJAX Error: "+error);
});
} else if (this.isWinJS) {
return WinJS.xhr({responseType: 'json', url: requestUrl}).done(
function completed(resp) {
callback.call(this, JSON.parse(resp.responseText));
}
);
} else if (this.isNode) {
https.get(requestUrl, function(response){
var resp = '';
response.on('data', function (chunk) {
resp += chunk;
});
response.on('end', function () {
resp = JSON.parse(resp);
callback.call(this, resp);
});
}).end();
} else if (this.isFirefoxOS) {
var xhr = new XMLHttpRequest({mozSystem: true});
xhr.open("POST", requestUrl, true);
xhr.onreadystatechange = function () {
if (xhr.status === 200 && xhr.readyState === 4) {
callback.call(this, JSON.parse(xhr.response));
}
};
xhr.onerror = function () {
console.log("XHR error");
};
xhr.send();
} else {
window[callbackName] = function (resp) {
callback.call(this, resp);
window[callbackName] = null;
};
s = document.createElement('script');
s.src = requestUrl;
document.body.appendChild(s);
}
};
}
return exports;
}())));