Skip to content

Commit

Permalink
add xray.format(fn)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewmueller committed Feb 4, 2015
1 parent a1289d9 commit bfea5f0
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 83 deletions.
7 changes: 5 additions & 2 deletions lib/x-ray.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ var Select = require('x-ray-select');
var request = require('./request');
var cheerio = require('cheerio');
var isArray = Array.isArray;
var noop = function() {};
var url = require('url');
var keys = Object.keys;
var fs = require('fs');
Expand Down Expand Up @@ -37,10 +36,10 @@ function Xray(url) {
if (!(this instanceof Xray)) return new Xray(url);


this._format = function(o) { return o };
this._paginate = false;
this._limit = Infinity;
this._throws = true;
this._format = noop;
this.selects = {};
this.url = url;
this.keys = [];
Expand Down Expand Up @@ -237,6 +236,7 @@ Xray.prototype.traverse = function(fn, done) {
var paginate = this._paginate;
var selects = this.selects;
var throws = this._throws;
var format = this._format;
var url = this.url;
var self = this;

Expand All @@ -253,6 +253,9 @@ Xray.prototype.traverse = function(fn, done) {
var json = select(selects);
var href = select(paginate);

// support formatting
json = isArray(json) ? json.map(format) : format(json);

// check the pagination
if (--limit <= 0) {
debug('reached limit, finishing up.');
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
},
"devDependencies": {
"mocha": "*",
"should": "*"
"should": "*",
"subs": "0.0.1"
},
"main": "index"
}
}
173 changes: 94 additions & 79 deletions test/x-ray.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
var rm = require('fs').unlinkSync;
var join = require('path').join;
var assert = require('assert');
var subs = require('subs');
var xray = require('..');
var fs = require('fs');

Expand All @@ -14,95 +15,109 @@ var fs = require('fs');

describe('x-ray', function() {

describe('http', function() {
it('should select keys', function(done) {
var fixture = get('select-keys');
xray('http://mat.io')
.select(fixture.input)
.run(function(err, arr) {
if (err) return done(err);
assert.deepEqual(arr.pop(), fixture.expected);
done();
});
});

it('should select keys', function(done) {
var fixture = get('select-keys');
xray('http://mat.io')
.select(fixture.input)
.run(function(err, arr) {
if (err) return done(err);
assert.deepEqual(arr.pop(), fixture.expected);
done();
});
});
it('should stream to a file', function(done) {
var fixture = get('select-keys');
var path = join(__dirname, 'out.json');

it('should stream to a file', function(done) {
var fixture = get('select-keys');
var path = join(__dirname, 'out.json');
xray('http://mat.io')
.select(fixture.input)
.write(path)
.on('error', done)
.on('close', function() {
var str = fs.readFileSync(path, 'utf8');
var arr = JSON.parse(str);
assert.deepEqual(arr.pop(), fixture.expected);
rm(path);
done();
});
})

xray('http://mat.io')
.select(fixture.input)
.write(path)
.on('error', done)
.on('close', function() {
var str = fs.readFileSync(path, 'utf8');
var arr = JSON.parse(str);
assert.deepEqual(arr.pop(), fixture.expected);
rm(path);
done();
});
})
it('should paginate', function(done) {
var fixture = get('paginate');
xray('https:/stars/matthewmueller')
.select(fixture.input)
.paginate('.pagination a:last-child[href]')
.limit(2)
.run(function(err, arr) {
if (err) return done(err);
fixture.expected(arr);
done();
});
});

it('should paginate', function(done) {
var fixture = get('paginate');
xray('https:/stars/matthewmueller')
.select(fixture.input)
.paginate('.pagination a:last-child[href]')
.limit(2)
.run(function(err, arr) {
if (err) return done(err);
fixture.expected(arr);
done();
});
});
it('should add delay to pagination', function(done) {
var fixture = get('paginate');
xray('https:/stars/matthewmueller')
.select(fixture.input)
.paginate('.pagination a:last-child[href]')
.limit(2)
.delay(2000)
.run(function(err, arr) {
if (err) return done(err);
fixture.expected(arr);
done();
});
})

it('should add delay to pagination', function(done) {
var fixture = get('paginate');
xray('https:/stars/matthewmueller')
.select(fixture.input)
.paginate('.pagination a:last-child[href]')
.limit(2)
.delay(2000)
.run(function(err, arr) {
if (err) return done(err);
fixture.expected(arr);
done();
});
})
it('should stream to a file and paginate', function(done) {
var fixture = get('paginate');
var path = join(__dirname, 'out.json');

it('should stream to a file and paginate', function(done) {
var fixture = get('paginate');
var path = join(__dirname, 'out.json');
xray('https:/stars/matthewmueller')
.select(fixture.input)
.paginate('.pagination a:last-child[href]')
.limit(2)
.write(path)
.on('error', done)
.on('close', function() {
var str = fs.readFileSync(path, 'utf8');
var arr = JSON.parse(str);
fixture.expected(arr);
rm(path);
done();
});
});

xray('https:/stars/matthewmueller')
.select(fixture.input)
.paginate('.pagination a:last-child[href]')
.limit(2)
.write(path)
.on('error', done)
.on('close', function() {
var str = fs.readFileSync(path, 'utf8');
var arr = JSON.parse(str);
fixture.expected(arr);
rm(path);
done();
});
});
it('should yield an empty array on unmatched collections', function(done) {
xray('http://mat.io')
.select([{
title: '.titlez'
}])
.run(function(err, arr) {
if (err) return done(err);
assert.deepEqual([], arr);
done();
})
})

it('should yield an empty array on unmatched collections', function(done) {
xray('http://mat.io')
.select([{
title: '.titlez'
}])
.run(function(err, arr) {
if (err) return done(err);
assert.deepEqual([], arr);
done();
it('should support formatting', function(done) {
var fixture = get('select-keys');
xray('http://mat.io')
.select(fixture.input)
.format(format)
.run(function(err, arr) {
if (err) return done(err);
arr.forEach(function(item) {
assert(~item.indexOf('<a href="http'));
})
})
});
done();
})

function format(obj) {
return subs('<a href="{link}">{thumb}</a>', obj);
}
})
})

/**
Expand Down

0 comments on commit bfea5f0

Please sign in to comment.