Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewmueller committed Feb 4, 2015
1 parent 0989cdc commit a1289d9
Showing 1 changed file with 103 additions and 62 deletions.
165 changes: 103 additions & 62 deletions lib/x-ray.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ module.exports = Xray;
function Xray(url) {
if (!(this instanceof Xray)) return new Xray(url);


this._paginate = false;
this._limit = Infinity;
this._throws = true;
this._format = noop;
this.selects = {};
this._end = noop;
this.url = url;
this.keys = [];
this.from = 0;
Expand All @@ -61,7 +62,100 @@ Xray.prototype.select = function(select) {
}

/**
* Run
* Add a plugin
*
* @param {Function} fn
* @retun {Xray}
* @api public
*/

Xray.prototype.use = function(fn) {
fn(this);
return this;
}

/**
* Throw errors or not
*
* @param {Boolean} throws
* @return {Boolean|Xray}
* @api public
*/

Xray.prototype.throws = function(throws) {
if (!arguments.length) return this._throws;
this._throws = !!throws;
return this;
};

/**
* Delay the next request between
* `from` and `to` ms
*
* @param {Number} from
* @param {Number} to
* @return {Xray|Number}
* @api public
*/

Xray.prototype.delay = function delay(from, to) {
if (arguments.length) {
this.from = from;
this.to = to || from;
return this;
} else {
return Math.floor(Math.random() * this.to) + this.from;
}
};

/**
* Format the output
*
* @param {Function} fn;
* @return {Xray}
* @api public
*/

Xray.prototype.format = function(fn) {
if (!arguments.length) return this._format;
this._format = fn;
return this;
};


/**
* Paginate
*
* @param {String} el
* @return {Xray}
* @api public
*/

Xray.prototype.paginate = function(el) {
if (!arguments.length) return this._paginate;
this._paginate = el;
return this;
};

/**
* A maximum number of pages to traverse
*
* @param {Number} limit
* @return {Xray}
* @api public
*/

Xray.prototype.limit = function(n) {
if (!arguments.length) return this._limit;
this._limit = n;
return this;
};

/**
* Run `x-ray`
*
* @param {Function} fn
* @return Xray
*/

Xray.prototype.run = function(fn) {
Expand All @@ -70,6 +164,7 @@ Xray.prototype.run = function(fn) {
var out = [];

this.traverse(next, done);
return this;

// each selection
function next(json) {
Expand All @@ -96,14 +191,14 @@ Xray.prototype.run = function(fn) {
*
* @param {String|Stream} file
* @return {Stream}
* @api public
*/

Xray.prototype.write = function(file) {
var stream = 'string' == typeof file ? fs.createWriteStream(file) : file;
var written = false;

this.traverse(next, done);

return stream;

function next(json) {
Expand All @@ -129,7 +224,11 @@ Xray.prototype.write = function(file) {
}

/**
* Traverse
* Traverse the pages
*
* @param {Function} fn
* @param {Function} done
* @api private
*/

Xray.prototype.traverse = function(fn, done) {
Expand Down Expand Up @@ -191,61 +290,3 @@ Xray.prototype.traverse = function(fn, done) {
return $;
}
};

/**
* Use
*/

Xray.prototype.use = function(fn) {
fn(this);
return this;
}

/**
* throws
*/

Xray.prototype.throws = function(throws) {
this._throws = !!throws;
return this;
};

/**
* Delay the next request
*/

Xray.prototype.delay = function delay(from, to) {
if (arguments.length) {
this.from = from;
this.to = to || from;
return this;
} else {
return Math.floor(Math.random() * this.to) + this.from;
}
};

/**
* Paginate
*
* @param {String} el
* @return {Xray}
* @api public
*/

Xray.prototype.paginate = function(el) {
this._paginate = el;
return this;
};

/**
* A maximum number of pages to traverse
*
* @param {Number} limit
* @return {Xray}
* @api public
*/

Xray.prototype.limit = function(n) {
this._limit = n;
return this;
};

0 comments on commit a1289d9

Please sign in to comment.