Adding jsbeautifier grunt task

This commit is contained in:
Jacob Marttinen 2015-05-07 20:13:17 -04:00
parent 82cd4e5118
commit 9312aca8a1
5 changed files with 389 additions and 385 deletions

View file

@ -1,36 +1,48 @@
module.exports = function(grunt) { module.exports = function (grunt) {
grunt.initConfig({ grunt.initConfig({
simplemocha: { simplemocha: {
options: { options: {
globals: ['should'], globals: ['should'],
timeout: 3000, timeout: 3000,
ignoreLeaks: false, ignoreLeaks: false,
ui: 'bdd', ui: 'bdd',
reporter: 'list' reporter: 'list'
}, },
all: { src: ['test/**/*.js'] } all: {
}, src: ['test/**/*.js']
}
},
jshint: { jsbeautifier: {
options: { files: ['Gruntfile.js', 'index.js', 'lib/**/*.js', 'test/**/*.js'],
laxbreak: true options: {
}, js: {
all: [ 'Gruntfile.js', 'index.js', 'lib/**/*.js', 'test/**/*.js'] jslintHappy: true
}, }
}
},
jsdoc: { jshint: {
all: { options: {
src: ['index.js', 'lib/**/*.js'], laxbreak: true
dest: 'doc' },
} all: ['Gruntfile.js', 'index.js', 'lib/**/*.js', 'test/**/*.js']
} },
});
grunt.loadNpmTasks('grunt-contrib-jshint'); jsdoc: {
grunt.loadNpmTasks('grunt-simple-mocha'); all: {
grunt.loadNpmTasks('grunt-jsdoc'); src: ['index.js', 'lib/**/*.js'],
dest: 'doc'
}
}
});
grunt.registerTask('test', ['simplemocha:all']); grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.registerTask('build', ['jshint', 'test']); grunt.loadNpmTasks('grunt-jsbeautifier');
grunt.registerTask('default', ['build', 'jsdoc']); grunt.loadNpmTasks('grunt-simple-mocha');
grunt.loadNpmTasks('grunt-jsdoc');
grunt.registerTask('test', ['simplemocha:all']);
grunt.registerTask('build', ['jsbeautifier', 'jshint', 'test']);
grunt.registerTask('default', ['build', 'jsdoc']);
}; };

View file

@ -1,246 +1,226 @@
/*********************************************************
/********************************************************* * Simple Node.js module for using the Bing Search API *
* Simple Node.js module for using the Bing Search API * *********************************************************/
*********************************************************/
// Require dependencies
// Require dependencies var request = require('request'),
var request = require('request'), url = require('url'),
url = require('url'), _ = require('underscore'),
_ = require('underscore'), qs = require('querystring');
qs = require('querystring');
/**
/** * @param {Object} options Options to all Bing calls, allows overriding of
* @param {Object} options Options to all Bing calls, allows overriding of * rootUri, accKey (Bing API key), userAgent, reqTimeout
* rootUri, accKey (Bing API key), userAgent, reqTimeout * @returns {Bing}
* @returns {Bing} * @constructor
* @constructor */
*/ var Bing = function (options) {
var Bing = function( options ) {
if (!(this instanceof Bing)) return new Bing(options);
if( !(this instanceof Bing) ) return new Bing( options );
var defaults = {
var defaults = {
//Bing Search API URI
//Bing Search API URI rootUri: "https://api.datamarket.azure.com/Bing/Search/v1/",
rootUri: "https://api.datamarket.azure.com/Bing/Search/v1/",
//Account Key
//Account Key accKey: null,
accKey: null,
//Bing UserAgent
//Bing UserAgent userAgent: 'Bing Search Client for Node.js',
userAgent: 'Bing Search Client for Node.js',
//Request Timeout
//Request Timeout reqTimeout: 5000,
reqTimeout: 5000,
// Number of results (limited to 50 by API)
// Number of results (limited to 50 by API) top: 50,
top: 50,
// Number of skipped results (pagination)
// Number of skipped results (pagination) skip: 0
skip: 0
};
};
//merge options passed in with defaults
//merge options passed in with defaults this.options = _.extend(defaults, options);
this.options = _.extend(defaults, options);
this.searchVertical = function (query, vertical, options, callback) {
this.searchVertical = function(query, vertical, options, callback) { if (typeof options === 'function') {
if (typeof options === 'function') { callback = options;
callback = options; }
} if (typeof callback != 'function') {
if(typeof callback != 'function') { throw "Error: Callback function required!";
throw "Error: Callback function required!"; }
}
// Create a copy of the options, to avoid permanent overwrites
// Create a copy of the options, to avoid permanent overwrites var opts = JSON.parse(JSON.stringify(this.options));
var opts = JSON.parse(JSON.stringify(this.options));
if (typeof options === 'object') {
if (typeof options === 'object') { _.extend(opts, options);
_.extend(opts, options); }
}
var reqUri = opts.rootUri + vertical + "?$format=json&" + qs.stringify({
var reqUri = opts.rootUri "Query": "'" + query + "'"
+ vertical }) + "&$top=" + opts.top + "&$skip=" + opts.skip + (opts.sources ? "&Sources=%27" + opts.sources + "%27" : '') + (opts.newssortby ? "&NewsSortBy=%27" + opts.newssortby + "%27" : '') + (opts.newscategory ? "&NewsCategory=%27" + opts.newscategory + "%27" : '') + (opts.newslocationoverride ? "&NewsLocationOverride=%27" + opts.newslocationoverride + "%27" : '') + (opts.market ? "&Market=%27" + opts.market + "%27" : '') + (opts.adult ? "&Adult=%27" + opts.adult + "%27" : '') + (opts.imagefilters ? '&' + qs.stringify({
+ "?$format=json&" "ImageFilters": "'" + opts.imagefilters + "'"
+ qs.stringify({ "Query": "'" + query + "'" }) }) : '') + (opts.videofilters ? '&' + qs.stringify({
+ "&$top=" + opts.top "VideoFilters": "'" + opts.videofilters + "'"
+ "&$skip=" + opts.skip }) : '');
+ (opts.sources ? "&Sources=%27" + opts.sources + "%27" : '')
+ (opts.newssortby ? "&NewsSortBy=%27" + opts.newssortby + "%27" : '') request({
+ (opts.newscategory ? "&NewsCategory=%27" + opts.newscategory + "%27" : '') uri: reqUri,
+ (opts.newslocationoverride ? "&NewsLocationOverride=%27" + opts.newslocationoverride + "%27" : '') method: opts.method || "GET",
+ (opts.market ? "&Market=%27" + opts.market + "%27" : '') headers: {
+ (opts.adult ? "&Adult=%27" + opts.adult + "%27" : '') "User-Agent": opts.userAgent
+ (opts.imagefilters },
? '&' + qs.stringify({ "ImageFilters": "'" + opts.imagefilters + "'" }) auth: {
: '') user: opts.accKey,
+ (opts.videofilters pass: opts.accKey
? '&' + qs.stringify({ "VideoFilters": "'" + opts.videofilters + "'" }) },
: ''); timeout: opts.reqTimeout
request({ }, function (err, res, body) {
uri: reqUri,
method: opts.method || "GET", if (res && res.statusCode !== 200) {
headers: { err = new Error(body);
"User-Agent": opts.userAgent } else {
},
auth: { // Parse body, if body
user: opts.accKey, body = typeof body === 'string' ? JSON.parse(body) : body;
pass: opts.accKey }
},
timeout: opts.reqTimeout callback(err, res, body);
});
}, function(err, res, body){ };
if(res && res.statusCode !== 200){ };
err = new Error(body);
}else{
/**
// Parse body, if body * @callback requestCallback
body = typeof body === 'string' * @param {String} error Error evaluates to true when an error has occurred.
? JSON.parse(body) * @param {Object} response Response object from the Bing call.
: body; * @param {Object} body JSON of the response.
} */
callback(err, res, body);
}); /**
}; * Performs a Bing search in the Web vertical.
*
}; * @param {String} query Query term to search for.
*
* @param {Object} options Options to command, allows overriding
/** * of rootUri, accKey (Bing API key),
* @callback requestCallback * userAgent, reqTimeout, top, skip
* @param {String} error Error evaluates to true when an error has occurred. *
* @param {Object} response Response object from the Bing call. * @param {requestCallback} callback Callback called with (potentially
* @param {Object} body JSON of the response. * json-parsed) response.
*/ *
* @function
*/
/** Bing.prototype.web = function (query, options, callback) {
* Performs a Bing search in the Web vertical. this.searchVertical(query, "Web", options, callback);
* };
* @param {String} query Query term to search for.
* // Alias Bing.search to Bing.web
* @param {Object} options Options to command, allows overriding // Note: Keep this for compatibility with older versions
* of rootUri, accKey (Bing API key), Bing.prototype.search = Bing.prototype.web;
* userAgent, reqTimeout, top, skip
*
* @param {requestCallback} callback Callback called with (potentially /**
* json-parsed) response. * Performs a Bing search in the Composite vertical.
* *
* @function * @param {String} query Query term to search for.
*/ *
Bing.prototype.web = function(query, options, callback) { * @param {Object} options Options to command, allows overriding
this.searchVertical(query, "Web", options, callback); * of rootUri, accKey (Bing API key),
}; * userAgent, reqTimeout, top, skip,
*
// Alias Bing.search to Bing.web * @param {requestCallback} callback Callback called with (potentially
// Note: Keep this for compatibility with older versions * json-parsed) response.
Bing.prototype.search = Bing.prototype.web; * @function
*/
Bing.prototype.composite = function (query, options, callback) {
/** this.searchVertical(query, "Composite", options, callback);
* Performs a Bing search in the Composite vertical. };
*
* @param {String} query Query term to search for. /**
* * Performs a Bing search in the News vertical.
* @param {Object} options Options to command, allows overriding *
* of rootUri, accKey (Bing API key), * @param {String} query Query term to search for.
* userAgent, reqTimeout, top, skip, *
* * @param {Object} options Options to command, allows overriding
* @param {requestCallback} callback Callback called with (potentially * of rootUri, accKey (Bing API key),
* json-parsed) response. * userAgent, reqTimeout, top, skip,
* @function *
*/ * @param {requestCallback} callback Callback called with (potentially
Bing.prototype.composite = function(query, options, callback) { * json-parsed) response.
this.searchVertical(query, "Composite", options, callback); * @function
}; */
Bing.prototype.news = function (query, options, callback) {
/** this.searchVertical(query, "News", options, callback);
* Performs a Bing search in the News vertical. };
*
* @param {String} query Query term to search for. /**
* * Performs a Bing search in the Video vertical.
* @param {Object} options Options to command, allows overriding *
* of rootUri, accKey (Bing API key), * @param {String} query Query term to search for.
* userAgent, reqTimeout, top, skip, *
* * @param {Object} options Options to command, allows overriding
* @param {requestCallback} callback Callback called with (potentially * of rootUri, accKey (Bing API key),
* json-parsed) response. * userAgent, reqTimeout, top, skip,
* @function *
*/ * @param {requestCallback} callback Callback called with (potentially
Bing.prototype.news = function(query, options, callback) { * json-parsed) response.
this.searchVertical(query, "News", options, callback); * @function
}; */
Bing.prototype.video = function (query, options, callback) {
/** if (options && typeof options === 'object' && options.videofilters && typeof options.videofilters === 'object') {
* Performs a Bing search in the Video vertical. var filterQuery = '';
* var filters = Object.keys(options.videofilters);
* @param {String} query Query term to search for. filters.map(function (key, i) {
* filterQuery += capitalizeFirstLetter(key) + ':';
* @param {Object} options Options to command, allows overriding filterQuery += capitalizeFirstLetter(options.videofilters[key]);
* of rootUri, accKey (Bing API key), if (i < filters.length - 1)
* userAgent, reqTimeout, top, skip, filterQuery += '+';
* });
* @param {requestCallback} callback Callback called with (potentially options.videofilters = filterQuery;
* json-parsed) response. }
* @function this.searchVertical(query, "Video", options, callback);
*/ };
Bing.prototype.video = function(query, options, callback) {
if (options
&& typeof options === 'object'
&& options.videofilters /**
&& typeof options.videofilters === 'object') { * Performs a Bing search in the Images vertical.
var filterQuery = ''; *
var filters = Object.keys(options.videofilters); * @param {String} query Query term to search for.
filters.map(function(key, i) { *
filterQuery += capitalizeFirstLetter(key) + ':'; * @param {Object} options Options to command, allows overriding of
filterQuery += capitalizeFirstLetter(options.videofilters[key]); * rootUri, accKey (Bing API key),
if (i < filters.length - 1) * userAgent, reqTimeout, top, skip,
filterQuery += '+'; * imagefilters
}); *
options.videofilters = filterQuery; * @param {requestCallback} callback Callback called with (potentially
} * json-parsed) response.
this.searchVertical(query, "Video", options, callback); * @function
}; */
Bing.prototype.images = function (query, options, callback) {
if (options && typeof options === 'object' && options.imagefilters && typeof options.imagefilters === 'object') {
var filterQuery = '';
/** var filters = Object.keys(options.imagefilters);
* Performs a Bing search in the Images vertical. filters.map(function (key, i) {
* filterQuery += capitalizeFirstLetter(key) + ':';
* @param {String} query Query term to search for. filterQuery += capitalizeFirstLetter(options.imagefilters[key]);
* if (i < filters.length - 1)
* @param {Object} options Options to command, allows overriding of filterQuery += '+';
* rootUri, accKey (Bing API key), });
* userAgent, reqTimeout, top, skip, options.imagefilters = filterQuery;
* imagefilters }
* this.searchVertical(query, "Image", options, callback);
* @param {requestCallback} callback Callback called with (potentially };
* json-parsed) response.
* @function function capitalizeFirstLetter(s) {
*/ return s.charAt(0).toUpperCase() + s.slice(1);
Bing.prototype.images = function(query, options, callback) { }
if (options
&& typeof options === 'object' module.exports = Bing;
&& options.imagefilters
&& typeof options.imagefilters === 'object') {
var filterQuery = '';
var filters = Object.keys(options.imagefilters);
filters.map(function(key, i) {
filterQuery += capitalizeFirstLetter(key) + ':';
filterQuery += capitalizeFirstLetter(options.imagefilters[key]);
if (i < filters.length - 1)
filterQuery += '+';
});
options.imagefilters = filterQuery;
}
this.searchVertical(query, "Image", options, callback);
};
function capitalizeFirstLetter(s) {
return s.charAt(0).toUpperCase() + s.slice(1);
}
module.exports = Bing;

View file

@ -20,6 +20,7 @@
"express": "^4.9.5", "express": "^4.9.5",
"grunt": "^0.4.5", "grunt": "^0.4.5",
"grunt-contrib-jshint": "^0.10.0", "grunt-contrib-jshint": "^0.10.0",
"grunt-jsbeautifier": "~0.2.10",
"grunt-jsdoc": "^0.5.7", "grunt-jsdoc": "^0.5.7",
"grunt-simple-mocha": "^0.4.0", "grunt-simple-mocha": "^0.4.0",
"mocha": "^1.21.4", "mocha": "^1.21.4",

View file

@ -1,31 +1,28 @@
var validWebResponse = { var validWebResponse = {
"d": { "d": {
"results": [ "results": [{
{ "__metadata": {
"__metadata": { "uri": "https://api.datamarket.azure.com/Data.ashx/Bing/Search/Web?Query='xbox'&$skip=0&$top=1",
"uri": "https://api.datamarket.azure.com/Data.ashx/Bing/Search/Web?Query='xbox'&$skip=0&$top=1", "type": "WebResult"
"type": "WebResult" },
}, "ID": "26888c2a-d245-47dc-87de-dc3551249de7",
"ID": "26888c2a-d245-47dc-87de-dc3551249de7", "Title": "Xbox | Games and Entertainment on All Your Devices",
"Title": "Xbox | Games and Entertainment on All Your Devices", "Description": "Experience the new generation of games and entertainment with Xbox. Play Xbox games and stream video on all your devices.",
"Description": "Experience the new generation of games and entertainment with Xbox. Play Xbox games and stream video on all your devices.", "DisplayUrl": "www.xbox.com",
"DisplayUrl": "www.xbox.com", "Url": "http://www.xbox.com/"
"Url": "http://www.xbox.com/" }, {
}, "__metadata": {
{ "uri": "https://api.datamarket.azure.com/Data.ashx/Bing/Search/Web?Query='xbox'&$skip=1&$top=1",
"__metadata": { "type": "WebResult"
"uri": "https://api.datamarket.azure.com/Data.ashx/Bing/Search/Web?Query='xbox'&$skip=1&$top=1", },
"type": "WebResult" "ID": "ff23e110-31c2-44f9-be21-f213bcd4c654",
}, "Title": "Amazon.com: Xbox - More Systems: Video Games: Games ...",
"ID": "ff23e110-31c2-44f9-be21-f213bcd4c654", "Description": "Online shopping for Video Games from a great selection of Games, Hardware, Computer And Console Video Game Products & more at everyday low prices.",
"Title": "Amazon.com: Xbox - More Systems: Video Games: Games ...", "DisplayUrl": "www.amazon.com/Xbox-Games/b?ie=UTF8&node=537504",
"Description": "Online shopping for Video Games from a great selection of Games, Hardware, Computer And Console Video Game Products & more at everyday low prices.", "Url": "http://www.amazon.com/Xbox-Games/b?ie=UTF8&node=537504"
"DisplayUrl": "www.amazon.com/Xbox-Games/b?ie=UTF8&node=537504", }],
"Url": "http://www.amazon.com/Xbox-Games/b?ie=UTF8&node=537504" "__next": "https://api.datamarket.azure.com/Data.ashx/Bing/Search/Web?Query='xbox'&$skip=2"
} }
],
"__next": "https://api.datamarket.azure.com/Data.ashx/Bing/Search/Web?Query='xbox'&$skip=2"
}
}; };
var should = require('should'), var should = require('should'),
@ -34,7 +31,7 @@ var should = require('should'),
request = require('request'), request = require('request'),
bing = require('../lib/bing'); bing = require('../lib/bing');
describe('Bing', function() { describe('Bing', function () {
var server; var server;
var app; var app;
var port = 4321; var port = 4321;
@ -43,13 +40,14 @@ describe('Bing', function() {
app = express(); app = express();
server = http.createServer(app); server = http.createServer(app);
server.listen.apply(server, [port, server.listen.apply(server, [port,
function(err, result) { function (err, result) {
if (err) { if (err) {
done(err); done(err);
} else { } else {
done(); done();
} }
}]); }
]);
}); });
after(function (done) { after(function (done) {
@ -59,11 +57,14 @@ describe('Bing', function() {
done(); done();
}); });
it('should cope with valid responses', function(done) { it('should cope with valid responses', function (done) {
app.get('/hello/Web', function (req, res) { app.get('/hello/Web', function (req, res) {
res.status(200).send(JSON.stringify(validWebResponse)); res.status(200).send(JSON.stringify(validWebResponse));
}); });
var bingClient = bing({ rootUri: 'http://localhost:'+port+'/hello/', accKey: '123' }); var bingClient = bing({
rootUri: 'http://localhost:' + port + '/hello/',
accKey: '123'
});
bingClient.search('xbox', function (error, response, body) { bingClient.search('xbox', function (error, response, body) {
response.statusCode.should.eql(200); response.statusCode.should.eql(200);
body.should.eql(validWebResponse); body.should.eql(validWebResponse);
@ -71,13 +72,18 @@ describe('Bing', function() {
}); });
}); });
it('should cope with errors', function(done) { it('should cope with errors', function (done) {
// No actual data on what the failure looks like. // No actual data on what the failure looks like.
var failure = { message: 'Failed request' }; var failure = {
message: 'Failed request'
};
app.get('/hello/Image', function (req, res) { app.get('/hello/Image', function (req, res) {
res.status(500).send(failure); res.status(500).send(failure);
});
var bingClient = bing({
rootUri: 'http://localhost:' + port + '/hello/',
accKey: '123'
}); });
var bingClient = bing({ rootUri: 'http://localhost:'+port+'/hello/', accKey: '123' });
bingClient.images('xbox', function (error, response, body) { bingClient.images('xbox', function (error, response, body) {
response.statusCode.should.eql(500); response.statusCode.should.eql(500);
body.should.eql(JSON.stringify(failure)); body.should.eql(JSON.stringify(failure));

View file

@ -1,128 +1,133 @@
// Try to get an access key to run all this test. // Try to get an access key to run all this test.
// If the file doesn't exist or it doesn't contain an access key, it // If the file doesn't exist or it doesn't contain an access key, it
// should still run the basic tests; thus throwing an exception must // should still run the basic tests; thus throwing an exception must
// be avoided. // be avoided.
try{ try {
var accKey = require('./secrets').accKey; var accKey = require('./secrets').accKey;
} catch (e) {
console.log(e);
} }
catch(e){ console.log(e); }
if(!accKey){ if (!accKey) {
return console.error("Need to include an access key in your secrets.js"); return console.error("Need to include an access key in your secrets.js");
} }
var Bing = require('../')({ accKey: accKey}); var Bing = require('../')({
accKey: accKey
});
var should = require('should'); var should = require('should');
describe("Bing Search", function(){ describe("Bing Search", function () {
this.timeout(1000 * 10); this.timeout(1000 * 10);
it('works without options', function(done){ it('works without options', function (done) {
Bing.search('monkey vs frog', function(err, res, body){ Bing.search('monkey vs frog', function (err, res, body) {
should.not.exist(err); should.not.exist(err);
should.exist(res); should.exist(res);
should.exist(body); should.exist(body);
body.d.results.should.have.length(50); body.d.results.should.have.length(50);
//TODO check it contains the right fields //TODO check it contains the right fields
done(); done();
});
}); });
});
it('finds only 5 results', function(done){ it('finds only 5 results', function (done) {
Bing.search('monkey vs frog', { Bing.search('monkey vs frog', {
top: 5, top: 5,
market: 'en-US', market: 'en-US',
adult: 'Strict' adult: 'Strict'
}, function(err, res, body){ }, function (err, res, body) {
should.not.exist(err); should.not.exist(err);
should.exist(res); should.exist(res);
should.exist(body); should.exist(body);
body.d.results.should.have.length(5); body.d.results.should.have.length(5);
done(); done();
});
}); });
});
}); });
describe("Bing Images", function(){ describe("Bing Images", function () {
this.timeout(1000 * 10); this.timeout(1000 * 10);
it('finds images with specific options', function(done){ it('finds images with specific options', function (done) {
Bing.images('pizza', { Bing.images('pizza', {
top: 3, top: 3,
adult: 'Off', adult: 'Off',
imagefilters: { size: 'small', color: 'monochrome' } imagefilters: {
}, function(err, res, body){ size: 'small',
should.not.exist(err); color: 'monochrome'
should.exist(res); }
should.exist(body); }, function (err, res, body) {
should.not.exist(err);
should.exist(res);
should.exist(body);
body.d.results.should.have.length(3); body.d.results.should.have.length(3);
done(); done();
});
}); });
});
}); });
describe("Bing News", function(){ describe("Bing News", function () {
this.timeout(1000 * 10); this.timeout(1000 * 10);
it('finds news with specific options', function(done){ it('finds news with specific options', function (done) {
Bing.news('ps4', { Bing.news('ps4', {
top: 10, top: 10,
skip: 1, skip: 1,
newsortby: 'Date' newsortby: 'Date'
}, function(err, res, body){ }, function (err, res, body) {
//TODO try unaccepted options like imagefilters //TODO try unaccepted options like imagefilters
should.not.exist(err); should.not.exist(err);
should.exist(res); should.exist(res);
should.exist(body); should.exist(body);
body.d.results.should.have.length(10); body.d.results.should.have.length(10);
done(); done();
});
}); });
});
}); });
describe("Bing Video", function(){ describe("Bing Video", function () {
this.timeout(1000 * 10); this.timeout(1000 * 10);
it('finds videos with specific options', function(done){ it('finds videos with specific options', function (done) {
Bing.video('monkey vs frog', { Bing.video('monkey vs frog', {
top: 10 top: 10
}, function(err, res, body){ }, function (err, res, body) {
should.not.exist(err); should.not.exist(err);
should.exist(res); should.exist(res);
should.exist(body); should.exist(body);
body.d.results.should.have.length(10); body.d.results.should.have.length(10);
//TODO try here unaccepted options like imagefilters //TODO try here unaccepted options like imagefilters
done(); done();
});
}); });
});
}); });