Plato on Github
Report Home
node_modules\atropa-Requester\src\atropa-Requester.js
Maintainability
70.55
Lines of code
203
Difficulty
22.74
Estimated Errors
0.60
Function weight
By Complexity
By SLOC
/// <reference path="../../docs/vsdoc/OpenLayersAll.js"/> var atropa = require('atropa-header'); atropa.ArgsInfo = require('atropa-ArgsInfo').ArgsInfo; /*jslint indent: 4, maxerr: 50, white: true, browser: true, devel: true, plusplus: true, regexp: true */ /*global atropa */ // end header (function () { "use strict"; atropa.requires( 'Requester', function () { var supported = true; [ atropa.ArgsInfo, XMLHttpRequest ].forEach(function (prerequisite) { if(prerequisite === undefined) { supported = false; } }); return supported; } ); }()); /** * This represents an XMLHttpRequest. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20130311 * @class This represents an XMLHttpRequest. * @returns {Requester} Returns a requester object. * @requires atropa.ArgsInfo#checkArgTypes * @example * var requester, formData; * * requester = new atropa.Requester(); * requester.timeout = 10000; // requests will abort after 10 seconds. * requester.requestHeaders = { * "aHeader" : "headerValue", * "anotherHeader" : "andValue" * }; * * function showRequestResults(status, request) { * console.log("Status: ' + status); * console.dir(request); // console dir may or may not * // be supported in your environment. * } * * formData = new FormData(); * formData.append('aFormFieldName', 'formFieldData'); * formData.append('anotherFormFieldName', 'andData'); * * requester.makeRequest( * "post", "http://example.com", formData, showRequestResults); */ atropa.Requester = function Requester() { "use strict"; atropa.supportCheck('Requester'); var expArgTypes, checkRequest, request; /** * Container object for the expected argument types * supplied to this.makeRequest. * @private * @type Expected Arg Types * @fieldOf atropa.Requester- */ expArgTypes = {}; expArgTypes.requestWithMessage = ['string', 'string', 'string', 'function']; expArgTypes.requestNullMessage = ['string', 'string', 'object', 'function']; /** * Used to check the arguments types supplied to this.makeRequest. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20120909 * @private * @methodOf atropa.Requester- * @param {Arguments} args An arguments array * @returns {Boolean} Returns true if args types match the * expected types. * @requires atropa.ArgsInfo#checkArgTypes */ checkRequest = function (args) { var checker; checker = new atropa.ArgsInfo(); checker.setExpectedArgTypes(expArgTypes); return checker.checkArgTypes(args); }; /** * Object whose properties and values are header names and values * respectively. * @type Object * @fieldOf atropa.Requester# */ this.requestHeaders = {}; /** * Set the timeout value for the request in milliseconds. The request will * abort after this amount of time has passed. * @type Number * @fieldOf atropa.Requester# */ this.timeout = 30000; /** * XMLHttpRequest object used by Requester. * @private * @type XMLHttpRequest * @fieldOf atropa.Requester- */ request = new XMLHttpRequest(); request.aborted = false; request.abort = function() { request.aborted = true; XMLHttpRequest.prototype.abort.call(this); }; /** * Makes an AJAX request. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20130311 * @methodOf atropa.Requester# * @param {String} method The HTTP method to be used for this request. * @param {String} url The URL to send the request to. * @param {String} messageBody The body of the request. * @param {Object} callback The callback function to execute * when readyState is 4. The callback is supplied with two arguments. The * first argument is a boolean indicating whether or not the http status * was 200. The second argument is the request object. * @throws atropa.Requester.makeRequest unexpected argument type */ this.makeRequest = function (method, url, messageBody, callback) { var hdr; try { checkRequest(arguments); } catch (e) { throw new Error('atropa.Requester.makeRequest unexpected ' + 'argument type'); } request.aborted = false; request.open(method, url, true); for (hdr in this.requestHeaders) { if (this.requestHeaders.hasOwnProperty(hdr)) { request.setRequestHeader(hdr, this.requestHeaders[hdr]); } } /** * Event listener function for the AJAX request. * This is what actually fires the callback supplied * to makeRequest. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20120909 * @methodOf atropa.Requester-request * @private */ request.onreadystatechange = function () { if (request.readyState === 4) { if (request.status === 200) { callback(true, request); } else { callback(false, request); } } }; request.send(messageBody); setTimeout(function () { if (request.aborted === false) { request.abort(); } }, this.timeout); }; }; while(atropa.data.requirements.length > 0) { atropa.data.requirements.pop()(); } module.exports = atropa;