Files
iD/js/lib/ohauth.js
T
2012-11-19 21:08:32 -05:00

54 lines
1.5 KiB
JavaScript

(function(context) {
var ohauth = {};
ohauth.qsString = function(obj) {
return Object.keys(obj).sort().map(function(key) {
return encodeURIComponent(key) + '=' + encodeURIComponent(obj[key]);
}).join('&');
};
ohauth.stringQs = function(str) {
return str.split('&').reduce(function(obj, pair){
var parts = pair.split('=');
obj[parts[0]] = (null === parts[1]) ? '' : decodeURIComponent(parts[1]);
return obj;
}, {});
};
ohauth.post = function(url, data, callback) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (4 == xhr.readyState && 0 !== xhr.status) callback(xhr);
};
xhr.open('POST', url, true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.send(data);
};
ohauth.nonce = function() {
for (var o = ''; o.length < 6;) {
o += '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz'[Math.floor(Math.random() * 61)];
}
return o;
};
ohauth.timestamp = function() { return ~~((+new Date()) / 1000); };
ohauth.percentEncode = function(s) {
return encodeURIComponent(s)
.replace(/\!/g, '%21').replace(/\'/g, '%27')
.replace(/\*/g, '%2A').replace(/\(/g, '%28').replace(/\)/g, '%29');
};
ohauth.baseString = function(method, url, params) {
return [
method,
ohauth.percentEncode(url),
ohauth.percentEncode(ohauth.qsString(params))].join('&');
};
context.ohauth = ohauth;
})(this);