Skip to content

Commit

Permalink
Add caching support, add minify script, add searhc tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikito Takada committed Aug 25, 2011
1 parent ac97c6b commit 2cca55d
Show file tree
Hide file tree
Showing 11 changed files with 595 additions and 130 deletions.
84 changes: 82 additions & 2 deletions lib/basicauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,88 @@ BasicAuth.prototype.status = function() {
};

BasicAuth.prototype.auth = function(opts) {
opts.headers['Authorization'] = new Buffer(this.email+':'+this.password).toString('base64');
opts.headers || (opts.headers = {});
// opts.headers['Authorization'] = new Buffer(this.email+':'+this.password).toString('base64');
opts.headers['Authorization'] = this.base64_encode(this.email+':'+this.password);
return opts;
};

module.exports = BasicAuth;
BasicAuth.prototype.base64_encode = function (data) {
var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
var o1, o2, o3, h1, h2, h3, h4, bits, i = 0,
ac = 0,
enc = "",
tmp_arr = [];

if (!data) {
return data;
}

data = this.utf8_encode(data + '');

do { // pack three octets into four hexets
o1 = data.charCodeAt(i++);
o2 = data.charCodeAt(i++);
o3 = data.charCodeAt(i++);

bits = o1 << 16 | o2 << 8 | o3;

h1 = bits >> 18 & 0x3f;
h2 = bits >> 12 & 0x3f;
h3 = bits >> 6 & 0x3f;
h4 = bits & 0x3f;

// use hexets to index into b64, and append result to encoded string
tmp_arr[ac++] = b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4);
} while (i < data.length);

enc = tmp_arr.join('');

var r = data.length % 3;

return (r ? enc.slice(0, r - 3) : enc) + '==='.slice(r || 3);

}

BasicAuth.prototype.utf8_encode = function (argString) {
if (argString === null || typeof argString === "undefined") {
return "";
}

var string = (argString + ''); // .replace(/\r\n/g, "\n").replace(/\r/g, "\n");
var utftext = "",
start, end, stringl = 0;

start = end = 0;
stringl = string.length;
for (var n = 0; n < stringl; n++) {
var c1 = string.charCodeAt(n);
var enc = null;

if (c1 < 128) {
end++;
} else if (c1 > 127 && c1 < 2048) {
enc = String.fromCharCode((c1 >> 6) | 192) + String.fromCharCode((c1 & 63) | 128);
} else {
enc = String.fromCharCode((c1 >> 12) | 224) + String.fromCharCode(((c1 >> 6) & 63) | 128) + String.fromCharCode((c1 & 63) | 128);
}
if (enc !== null) {
if (end > start) {
utftext += string.slice(start, end);
}
utftext += enc;
start = end = n + 1;
}
}

if (end > start) {
utftext += string.slice(start, stringl);
}

return utftext;
}


if(typeof exports != 'undefined') {
module.exports = BasicAuth;
}
16 changes: 16 additions & 0 deletions lib/cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var Cache = function() {
this.store = {};
};

Cache.prototype.has = function(type, id){

};

Cache.prototype.get = function(type, id) {

};

Cache.prototype.set = function(type, id, data) {

};

102 changes: 102 additions & 0 deletions lib/client.jquery.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@

var Client = function(config) {
Client.host = config.host;
Client.port = config.port;
Client.secure = config.secure || false;
Client.auth = config.auth || { auth: function(opts) { return opts; } };
return Client;
};

Client.request = function(method, url) {
var data, callback;
var opts = {
type: method,
url: 'http://'+Client.host+url,
processData: false, // we'll do this ourself
dataType: 'text', // do not autoparse, some APIs like ZD's send empty responses as application/JSON and that causes problems
cache: false,
error: function(jqXHR, textStatus, errorThrown) {
console.log('Fail', textStatus, errorThrown);
if(callback) {
callback({}, {});
}
},
success: function(data, textStatus, jqXHR) {
console.log('Success', data, textStatus, jqXHR);
if(callback) {
callback(Client.json(data), {});
}
}
};
if(arguments.length == 3) {
// data is optional
callback = arguments[2];
} else if (arguments.length == 4) {
data = arguments[2];
callback = arguments[3];
if(method == 'GET') {
// append to QS
// console.log('GET append', data);
opts.url += '?'+jQuery.param(data);
} else if(false) {
// POST encoding
opts.contentType = 'application/x-www-form-urlencoded';
opts.data = jQuery.param(data);
} else {
// JSON encoding
opts.contentType = 'application/json';
opts.data = JSON.stringify(data);
}
}
opts = Client.auth.auth(opts);
console.log('jq.ajax', opts);
jQuery.ajax(opts);
};

/**
* Issue a GET request.
*/
Client.get = function(url, data, callback) {
var args = Array.prototype.slice.call(arguments);
args.unshift('GET');
Client.request.apply(Client, args);
};

/**
* Issue a POST request.
*/
Client.post = function(url, data, callback) {
var args = Array.prototype.slice.call(arguments);
args.unshift('POST');
Client.request.apply(Client, args);
};

/**
* Issue a PUT request.
*/
Client.put = function(url, data, callback) {
var args = Array.prototype.slice.call(arguments);
args.unshift('PUT');
Client.request.apply(Client, args);
};

/**
* Issue a DELETE request.
*/
Client.del = function(url, data, callback) {
var args = Array.prototype.slice.call(arguments);
args.unshift('DELETE');
Client.request.apply(Client, args);
};

/**
* Parse JSON.
*/
Client.json = function(data) {
try {
var json = (typeof data == 'string' ? JSON.parse(data) : data);
} catch(e) {
return data;
}
return json;
};
36 changes: 4 additions & 32 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ Client.request = function(method, url) {
opts.path += '?'+qs.stringify(data);
} else if(false) {
// POST encoding
data = qs.stringify(data);
opts.headers || (opts.headers = {});
opts.headers['Content-Length'] = data.length;
opts.headers['Content-Type'] = 'application/x-www-form-urlencoded';
data = qs.stringify(data);
opts.headers['Content-Length'] = data.length;
} else {
// JSON encoding
data = JSON.stringify(data);
opts.headers || (opts.headers = {});
opts.headers['Content-Length'] = data.length;
opts.headers['Content-Type'] = 'application/json';
data = JSON.stringify(data);
opts.headers['Content-Length'] = data.length;
}
}
var res_data = '';
Expand Down Expand Up @@ -107,34 +107,6 @@ Client.del = function(url, data, callback) {
Client.request.apply(Client, args);
};

/**
* Fetch results from one or more URLs, combine them into a single array and then call the callback
*/
Client.batch = {};

Client.batch.get = function(urls, data, callback) {
var count = 0;
var all_items = [];
urls.forEach(function(url) {
Client.get(url, data, function(text) {
var items = Client.json(text);
// add items to the accumulator
if (_.isArray(items)) {
items.forEach(function(item) {
all_items.push(item);
});
} else {
all_items.push(items);
}
count++;
// if all done, execute the callback
if(count == urls.length) {
callback(all_items);
}
});
});
};

/**
* Parse JSON.
*/
Expand Down
15 changes: 10 additions & 5 deletions lib/collection.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var _ = require('./underscore_subset.js');
var Search = require('./search.js');
if(typeof exports != 'undefined') {
var _ = require('./underscore_subset.js');
var Search = require('./search.js');
}

var Item = function( definition, data ) {
this.definition = definition;
Expand Down Expand Up @@ -59,7 +61,8 @@ Collection.prototype.makeItem = function(data) {
var self = this;
// map has_many to the item
if(this.definition && this.definition.has_many) {
Object.keys(this.definition.has_many).forEach(function(key) {
_.keys(this.definition.has_many).forEach(function(key) {
console.log('subitem', key);
item[key] = function(selectors, callback) {
selectors || (selectors = {});
selectors.user = "mixu";
Expand Down Expand Up @@ -103,7 +106,7 @@ Collection.prototype.each = function(callback) {
};

Collection.prototype.create = function(data, callback){
var item = self.makeItem({}).create(data, callback);
var item = this.makeItem({}).create(data, callback);
return item;
}

Expand Down Expand Up @@ -147,4 +150,6 @@ Collection.initialize = function(type, definition) {
};


module.exports = Collection;
if(typeof exports != 'undefined') {
module.exports = Collection;
}
14 changes: 8 additions & 6 deletions lib/oauth2.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

var qs = require('querystring'),
url = require('url');

if(typeof exports != 'undefined') {
var qs = require('querystring'),
url = require('url');
}
/**
* Oauth config
*
Expand Down Expand Up @@ -65,7 +65,7 @@ var OAuth = function(config) {
this.is_ready = true;
} else {
// fetch a new access_token
this.getAccessToken();
// this.getAccessToken();
}
};

Expand Down Expand Up @@ -134,4 +134,6 @@ OAuth.prototype.getAccessToken = function() {
req.end();
};

module.exports = OAuth;
if(typeof exports != 'undefined') {
module.exports = OAuth;
}
Loading

0 comments on commit 2cca55d

Please sign in to comment.