Skip to content

Commit

Permalink
add xray.prepare(str, fn) for custom 'title | uppercase' filters
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewmueller committed Feb 7, 2015
1 parent d62f2a0 commit 4cf1897
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
22 changes: 21 additions & 1 deletion lib/x-ray.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/

var debug = require('debug')('x-ray');
var assign = require('object-assign');
var Select = require('x-ray-select');
var request = require('./request');
var yieldly = require('yieldly');
Expand Down Expand Up @@ -41,6 +42,7 @@ function Xray(url) {
this._paginate = false;
this._limit = Infinity;
this._throws = true;
this.prepares = {};
this.selects = {};
this.url = url;
this.keys = [];
Expand Down Expand Up @@ -122,6 +124,23 @@ Xray.prototype.format = function(fn) {
return this;
};

/**
* Prepare
*/

Xray.prototype.prepare = function(str, fn) {
if (!arguments.length) return this.prepares;

if (1 == arguments.length) {
this.prepares = assign(this.prepares, str);
} else {
this.prepares[str] = fn;
}

return this;
};



/**
* Paginate
Expand Down Expand Up @@ -255,6 +274,7 @@ Xray.prototype.traverse = function(fn, done) {
var get = this.request ? this.request : (this.request = this.use(request()).request);
var limit = this._paginate ? this._limit : 1;
var paginate = this._paginate;
var prepares = this.prepares;
var selects = this.selects;
var throws = this._throws;
var format = this._format;
Expand All @@ -270,7 +290,7 @@ Xray.prototype.traverse = function(fn, done) {
debug('received response');

var $ = load(body);
var select = Select($);
var select = Select($, prepares);
var json = select(selects);
var href = select(paginate);

Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
"name": "x-ray",
"version": "1.0.2",
"description": "structure any website",
"keywords": ["scraper", "cheerio", "web"],
"keywords": [
"scraper",
"cheerio",
"web"
],
"author": "Matthew Mueller <[email protected]>",
"repository": {
"type": "git",
Expand All @@ -12,6 +16,7 @@
"catch-stdout": "0.0.1",
"cheerio": "^0.17.0",
"debug": "^2.0.0",
"object-assign": "^2.0.0",
"superagent": "^0.21.0",
"x-ray-select": "^1.0.3",
"yieldly": "0.0.1"
Expand Down
17 changes: 17 additions & 0 deletions test/x-ray.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,23 @@ describe('x-ray', function() {
});
})

describe('prepares', function() {
it('should support prepares', function(done) {
function uppercase(str) {
return str.toUpperCase();
}

xray('http://mat.io')
.select('title | uppercase')
.prepare({ uppercase: uppercase })
.run(function(err, str) {
if (err) return done(err);
assert.equal('MAT.IO', str)
done();
});
})
});

it('should support arrays', function(done) {
xray('http://mat.io')
.select(['.Header-list-item a'])
Expand Down

0 comments on commit 4cf1897

Please sign in to comment.