diff --git a/docs/data.html b/docs/data.html new file mode 100644 index 0000000..0a70556 --- /dev/null +++ b/docs/data.html @@ -0,0 +1,47 @@ +
data.js | |
---|---|
/*
+ * Copyright (C) 2011 by Christopher Johnson
+ *
+ * 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.
+ */
+
+exports.ArrayCollection = ArrayCollection;
+
+function ArrayCollection() {
+ this._data = [];
+}
+
+ArrayCollection.prototype = {
+ add : function (o) {
+ this._data[o._id] = o;
+ },
+
+ remove : function (id) {
+ delete this._data[id];
+ },
+
+ get : function (id) {
+ var o = this._data[id];
+ if (o === undefined) {
+ throw new Error('Object, ' + id + ', not found');
+ }
+ return o;
+ }
+};
+
+ |
net.js | |
---|---|
/*
+ * Copyright (C) 2011 by Christopher Johnson
+ *
+ * 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.
+ */
+
+var http = require('http'),
+ _url = require('url'),
+ querystring = require('querystring'); | |
net.request(url, options, callback)+ +
+
+At least one of | var request = exports.request = function (url, options, callback) {
+ if (typeof url === 'object') {
+ callback = options;
+ options = url;
+ url = options.url;
+ } else if (!callback && typeof options === 'function') {
+ callback = options;
+ options = {};
+ }
+ |
convert url into an http request | var parse = _url.parse(url);
+ var params = {
+ host : parse.hostname,
+ port : parseInt(parse.port, 10) || 80,
+ method : options.method ? options.method.toUpperCase() : 'GET',
+ path : parse.pathname
+ };
+ |
append the querystring to the url for get requests | if (params.method === 'GET' && options.data) {
+ var query = querystring.stringify(options.data);
+ if (query) {
+ parse.search = (parse.search ? parse.search + '&' : '?') + query;
+ }
+ }
+ if (parse.search) {
+ params.path += parse.search;
+ }
+ |
initialize the http request | var request = http.request(params, function (response) {
+ callback && readHttpResponse(response, callback);
+ });
+ |
add posted data | if (options.method !== 'GET' && options.data) {
+ var post = querystring.stringify(options.data);
+ request.setHeader('Content-Type', 'application/x-www-form-urlencoded');
+ request.setHeader('Content-Length', post.length);
+ request.write(post);
+ }
+ |
commit the request | request.end();
+}; |
net.get(url, data, callback)+ +Simpler than | exports.get = function (url, data, callback) {
+ if (typeof data === 'function') {
+ callback = data;
+ data = null;
+ }
+ request(url, { method: 'GET', data: data }, callback);
+}; |
net.post(url, data, callback)+ +Like | exports.post = function (url, data, callback) {
+ if (typeof data === 'function') {
+ callback = data;
+ data = null;
+ }
+ request(url, { method: 'POST', data: data }, callback);
+}; |
net.serverTest(server, options, test)+ +
Open a new instance of the server. +Send a single test request. +Execute an assertion test against the response. | exports.serverTest = function (server, options, test) {
+ if (!test && typeof options === 'function') {
+ test = options;
+ options = {};
+ }
+ var port = 3000;
+ options.url = 'http://localhost:'+port+'/';
+ server.listen(port, function () {
+ exports.request(options, function (err, response) {
+ server.close();
+ test(err, response);
+ });
+ });
+}; |
net.readHttpResponse(res, callback)+ +Reads all data from an http response,
+compiling the body into a single string.
+See TODO: failure | var readHttpResponse = exports.readHttpResponse = function (res, callback) {
+ var result = {
+ statusCode : res.statusCode,
+ headers : res.headers,
+ body : ''
+ };
+ res.setEncoding('utf8');
+ res.on('data', function (chunk) {
+ result.body += chunk;
+ });
+ res.on('end', function () {
+ result.trailers = res.trailers;
+ callback(null, result);
+ });
+};
+
+ |