Adding jsbeautifier grunt task
This commit is contained in:
parent
82cd4e5118
commit
9312aca8a1
5 changed files with 389 additions and 385 deletions
16
Gruntfile.js
16
Gruntfile.js
|
@ -8,7 +8,18 @@ module.exports = function(grunt) {
|
|||
ui: 'bdd',
|
||||
reporter: 'list'
|
||||
},
|
||||
all: { src: ['test/**/*.js'] }
|
||||
all: {
|
||||
src: ['test/**/*.js']
|
||||
}
|
||||
},
|
||||
|
||||
jsbeautifier: {
|
||||
files: ['Gruntfile.js', 'index.js', 'lib/**/*.js', 'test/**/*.js'],
|
||||
options: {
|
||||
js: {
|
||||
jslintHappy: true
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
jshint: {
|
||||
|
@ -27,10 +38,11 @@ module.exports = function(grunt) {
|
|||
});
|
||||
|
||||
grunt.loadNpmTasks('grunt-contrib-jshint');
|
||||
grunt.loadNpmTasks('grunt-jsbeautifier');
|
||||
grunt.loadNpmTasks('grunt-simple-mocha');
|
||||
grunt.loadNpmTasks('grunt-jsdoc');
|
||||
|
||||
grunt.registerTask('test', ['simplemocha:all']);
|
||||
grunt.registerTask('build', ['jshint', 'test']);
|
||||
grunt.registerTask('build', ['jsbeautifier', 'jshint', 'test']);
|
||||
grunt.registerTask('default', ['build', 'jsdoc']);
|
||||
};
|
||||
|
|
40
lib/bing.js
40
lib/bing.js
|
@ -1,4 +1,3 @@
|
|||
|
||||
/*********************************************************
|
||||
* Simple Node.js module for using the Bing Search API *
|
||||
*********************************************************/
|
||||
|
@ -59,24 +58,13 @@ var Bing = function( options ) {
|
|||
_.extend(opts, options);
|
||||
}
|
||||
|
||||
var reqUri = opts.rootUri
|
||||
+ vertical
|
||||
+ "?$format=json&"
|
||||
+ qs.stringify({ "Query": "'" + query + "'" })
|
||||
+ "&$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({ "ImageFilters": "'" + opts.imagefilters + "'" })
|
||||
: '')
|
||||
+ (opts.videofilters
|
||||
? '&' + qs.stringify({ "VideoFilters": "'" + opts.videofilters + "'" })
|
||||
: '');
|
||||
var reqUri = opts.rootUri + vertical + "?$format=json&" + qs.stringify({
|
||||
"Query": "'" + query + "'"
|
||||
}) + "&$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({
|
||||
"ImageFilters": "'" + opts.imagefilters + "'"
|
||||
}) : '') + (opts.videofilters ? '&' + qs.stringify({
|
||||
"VideoFilters": "'" + opts.videofilters + "'"
|
||||
}) : '');
|
||||
|
||||
request({
|
||||
uri: reqUri,
|
||||
|
@ -97,9 +85,7 @@ var Bing = function( options ) {
|
|||
} else {
|
||||
|
||||
// Parse body, if body
|
||||
body = typeof body === 'string'
|
||||
? JSON.parse(body)
|
||||
: body;
|
||||
body = typeof body === 'string' ? JSON.parse(body) : body;
|
||||
}
|
||||
|
||||
callback(err, res, body);
|
||||
|
@ -188,10 +174,7 @@ Bing.prototype.news = function(query, options, callback) {
|
|||
* @function
|
||||
*/
|
||||
Bing.prototype.video = function (query, options, callback) {
|
||||
if (options
|
||||
&& typeof options === 'object'
|
||||
&& options.videofilters
|
||||
&& typeof options.videofilters === 'object') {
|
||||
if (options && typeof options === 'object' && options.videofilters && typeof options.videofilters === 'object') {
|
||||
var filterQuery = '';
|
||||
var filters = Object.keys(options.videofilters);
|
||||
filters.map(function (key, i) {
|
||||
|
@ -222,10 +205,7 @@ Bing.prototype.video = function(query, options, callback) {
|
|||
* @function
|
||||
*/
|
||||
Bing.prototype.images = function (query, options, callback) {
|
||||
if (options
|
||||
&& typeof options === 'object'
|
||||
&& options.imagefilters
|
||||
&& typeof options.imagefilters === 'object') {
|
||||
if (options && typeof options === 'object' && options.imagefilters && typeof options.imagefilters === 'object') {
|
||||
var filterQuery = '';
|
||||
var filters = Object.keys(options.imagefilters);
|
||||
filters.map(function (key, i) {
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
"express": "^4.9.5",
|
||||
"grunt": "^0.4.5",
|
||||
"grunt-contrib-jshint": "^0.10.0",
|
||||
"grunt-jsbeautifier": "~0.2.10",
|
||||
"grunt-jsdoc": "^0.5.7",
|
||||
"grunt-simple-mocha": "^0.4.0",
|
||||
"mocha": "^1.21.4",
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
var validWebResponse = {
|
||||
"d": {
|
||||
"results": [
|
||||
{
|
||||
"results": [{
|
||||
"__metadata": {
|
||||
"uri": "https://api.datamarket.azure.com/Data.ashx/Bing/Search/Web?Query='xbox'&$skip=0&$top=1",
|
||||
"type": "WebResult"
|
||||
|
@ -11,8 +10,7 @@ var validWebResponse = {
|
|||
"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",
|
||||
"Url": "http://www.xbox.com/"
|
||||
},
|
||||
{
|
||||
}, {
|
||||
"__metadata": {
|
||||
"uri": "https://api.datamarket.azure.com/Data.ashx/Bing/Search/Web?Query='xbox'&$skip=1&$top=1",
|
||||
"type": "WebResult"
|
||||
|
@ -22,8 +20,7 @@ var validWebResponse = {
|
|||
"Description": "Online shopping for Video Games from a great selection of Games, Hardware, Computer And Console Video Game Products & more at everyday low prices.",
|
||||
"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"
|
||||
}
|
||||
};
|
||||
|
@ -49,7 +46,8 @@ describe('Bing', function() {
|
|||
} else {
|
||||
done();
|
||||
}
|
||||
}]);
|
||||
}
|
||||
]);
|
||||
});
|
||||
|
||||
after(function (done) {
|
||||
|
@ -63,7 +61,10 @@ describe('Bing', function() {
|
|||
app.get('/hello/Web', function (req, res) {
|
||||
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) {
|
||||
response.statusCode.should.eql(200);
|
||||
body.should.eql(validWebResponse);
|
||||
|
@ -73,11 +74,16 @@ describe('Bing', function() {
|
|||
|
||||
it('should cope with errors', function (done) {
|
||||
// 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) {
|
||||
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) {
|
||||
response.statusCode.should.eql(500);
|
||||
body.should.eql(JSON.stringify(failure));
|
||||
|
|
|
@ -1,19 +1,21 @@
|
|||
|
||||
// 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
|
||||
// should still run the basic tests; thus throwing an exception must
|
||||
// be avoided.
|
||||
try {
|
||||
var accKey = require('./secrets').accKey;
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
}
|
||||
catch(e){ console.log(e); }
|
||||
|
||||
if (!accKey) {
|
||||
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');
|
||||
|
||||
|
||||
|
@ -63,7 +65,10 @@ describe("Bing Images", function(){
|
|||
Bing.images('pizza', {
|
||||
top: 3,
|
||||
adult: 'Off',
|
||||
imagefilters: { size: 'small', color: 'monochrome' }
|
||||
imagefilters: {
|
||||
size: 'small',
|
||||
color: 'monochrome'
|
||||
}
|
||||
}, function (err, res, body) {
|
||||
should.not.exist(err);
|
||||
should.exist(res);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue