Skip to content

Commit

Permalink
Merge pull request #5 from holidayextras/makeup-and-expect
Browse files Browse the repository at this point in the history
Makeup 6 and expect
  • Loading branch information
philmeehan committed Nov 26, 2015
2 parents 8fd17f6 + 8d26dc2 commit daf7e99
Show file tree
Hide file tree
Showing 18 changed files with 900 additions and 956 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,7 @@ build/Release

# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules
node_modules

# make-up
.eslintrc
30 changes: 0 additions & 30 deletions Gruntfile.js

This file was deleted.

3 changes: 1 addition & 2 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ test:
- mkdir -p $CIRCLE_TEST_REPORTS/unit
- mkdir -p $CIRCLE_TEST_REPORTS/coverage
override:
- grunt jshint
- grunt jscs
- node_modules/.bin/make-up lib test
- istanbul cover _mocha -- --timeout 15000 --recursive test/**/*Test.js -R xunit > $CIRCLE_TEST_REPORTS/unit/results.xml
post:
- cp -r ./coverage/ $CIRCLE_ARTIFACTS/coverage
Expand Down
3 changes: 1 addition & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* jslint node: true */
'use strict';

module.exports = require( './lib/pluginHarvest.js' );
module.exports = require( './lib/pluginHarvest.js' );
19 changes: 7 additions & 12 deletions lib/buildReject.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
/**
* Re-usable block of code for our promise rejections
* @param {Error} error An error object
* @param {Object} context The data passed into the function where the promise rejection occurs.
* @return {Object} Our promise rejection schema.
*/
'use strict';
module.exports = function( error, context ) {
return {
error: error,
origin: 'pluginHarvest',
data: context
};
};
return {
error: error,
origin: 'pluginHarvest',
data: context
};
};
79 changes: 39 additions & 40 deletions lib/getBasket.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* jslint node: true */
'use strict';

var _ = require( 'lodash' );
Expand All @@ -7,42 +6,42 @@ var buildReject = require( './buildReject' );

module.exports = function GetBasket( harvest, harvestDb, options ) {

var deferred = Q.defer();

try {

if( !options ) {
throw new TypeError( 'invalid options' );
}

if( !options.id || !_.isString( options.id ) ) {
throw new TypeError( 'invalid options.id' );
}

if( !options.tag || !_.isString( options.tag ) ) {
throw new TypeError( 'invalid options.tag' );
}

// *****************************************************************************************
// Interaction Flow
// 1. Get a stored basket out of the harvest database with the requested id
// 2. Convert to a shared basket structure based on the requested tag
// ******************************************************************************************
harvestDb.get( options.id, function( error, storedBasket ) {
if( error ) {
deferred.reject( buildReject( error, options ) );
} else {
try {
deferred.resolve( harvest.getSharedBasket( storedBasket, options.tag ) );
} catch ( error ) {
deferred.reject( buildReject( error, options ) );
}
}
} );

} catch ( error ) {
deferred.reject( buildReject( error, options ) );
}

return deferred.promise;
};
var deferred = Q.defer();

try {

if ( !options ) {
throw new TypeError( 'invalid options' );
}

if ( !options.id || !_.isString( options.id ) ) {
throw new TypeError( 'invalid options.id' );
}

if ( !options.tag || !_.isString( options.tag ) ) {
throw new TypeError( 'invalid options.tag' );
}

// *****************************************************************************************
// Interaction Flow
// 1. Get a stored basket out of the harvest database with the requested id
// 2. Convert to a shared basket structure based on the requested tag
// ******************************************************************************************
harvestDb.get( options.id, function( error, storedBasket ) {
if ( error ) {
deferred.reject( buildReject( error, options ) );
} else {
try {
deferred.resolve( harvest.getSharedBasket( storedBasket, options.tag ) );
} catch ( harvestError ) {
deferred.reject( buildReject( harvestError, options ) );
}
}
} );

} catch ( error ) {
deferred.reject( buildReject( error, options ) );
}

return deferred.promise;
};
123 changes: 61 additions & 62 deletions lib/getBaskets.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* jslint node: true */
'use strict';

var _ = require( 'lodash' );
Expand All @@ -7,71 +6,71 @@ var buildReject = require( './buildReject' );

module.exports = function GetBaskets( harvest, harvestDb, options ) {

var deferred = Q.defer();
var designDocument;
var view;
var params = {};
var deferred = Q.defer();
var designDocument;
var view;
var params = {};

// Used if we don't pass in timestamps for design documents that need them.
var currentTime = ( new Date() ).toISOString();
// Used if we don't pass in timestamps for design documents that need them.
var currentTime = ( new Date() ).toISOString();

try {
try {

if( !options ) {
throw new TypeError( 'invalid options' );
}
if ( !options ) {
throw new TypeError( 'invalid options' );
}

// Can't think of a better way of doing this - we need to examine different keys to call separate design documents on CouchDB
if( options.pin ) {
designDocument = 'all_baskets';
view = 'by_pin';
params.key = options.pin;
} else if( options.email ) {
designDocument = 'all_baskets';
view = 'by_email';
params.key = options.email;
} else if( options.telephone ) {
designDocument = 'all_baskets';
view = 'by_telephone';
params.key = options.telephone;
} else if( options.familyName ) {
designDocument = 'all_baskets';
view = 'by_family_name';
params.key = options.familyName;
} else if( options.type === 'phone' ) {
designDocument = 'phone_baskets';
view = 'by_created_at';
params.startkey = options.since || currentTime;
params.endkey = options.until || currentTime;
} else if( options.type === 'email' ) {
designDocument = 'email_baskets';
view = 'by_created_at';
params.startkey = options.since || currentTime;
params.endkey = options.until || currentTime;
} else {
throw new TypeError( 'invalid keys within options' );
}
// Can't think of a better way of doing this - we need to examine different keys to call separate design documents on CouchDB
if ( options.pin ) {
designDocument = 'all_baskets';
view = 'by_pin';
params.key = options.pin;
} else if ( options.email ) {
designDocument = 'all_baskets';
view = 'by_email';
params.key = options.email;
} else if ( options.telephone ) {
designDocument = 'all_baskets';
view = 'by_telephone';
params.key = options.telephone;
} else if ( options.familyName ) {
designDocument = 'all_baskets';
view = 'by_family_name';
params.key = options.familyName;
} else if ( options.type === 'phone' ) {
designDocument = 'phone_baskets';
view = 'by_created_at';
params.startkey = options.since || currentTime;
params.endkey = options.until || currentTime;
} else if ( options.type === 'email' ) {
designDocument = 'email_baskets';
view = 'by_created_at';
params.startkey = options.since || currentTime;
params.endkey = options.until || currentTime;
} else {
throw new TypeError( 'invalid keys within options' );
}

// *****************************************************************************************
// Interaction Flow
// 1. Get stored baskets from the relevant design document using passed in options
// 2. Convert each to a shared basket structure based on the requested tag
// ******************************************************************************************
harvestDb.view( designDocument, view, params, function( error, document ) {
var sharedBaskets = [];
if( error ) {
deferred.reject( buildReject( error, options ) );
} else {
_.forEach( document.rows, function( storedBasket ) {
sharedBaskets.push( harvest.getSharedBasket( storedBasket.value ) );
} );
deferred.resolve( sharedBaskets );
}
} );
// *****************************************************************************************
// Interaction Flow
// 1. Get stored baskets from the relevant design document using passed in options
// 2. Convert each to a shared basket structure based on the requested tag
// ******************************************************************************************
harvestDb.view( designDocument, view, params, function( error, document ) {
var sharedBaskets = [];
if ( error ) {
deferred.reject( buildReject( error, options ) );
} else {
_.forEach( document.rows, function( storedBasket ) {
sharedBaskets.push( harvest.getSharedBasket( storedBasket.value ) );
} );
deferred.resolve( sharedBaskets );
}
} );

} catch ( error ) {
deferred.reject( buildReject( error, options ) );
}
} catch ( error ) {
deferred.reject( buildReject( error, options ) );
}

return deferred.promise;
};
return deferred.promise;
};
53 changes: 26 additions & 27 deletions lib/pluginHarvest.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* jslint node: true */
'use strict';

var getBasket = require( './getBasket' );
Expand All @@ -10,42 +9,42 @@ var url = require( 'url' );

var Harvest = require( 'harvest' );

exports.register = function( server, options, next ) {
exports.register = function( server, pluginOptions, next ) {

var config = server.methods.getService( 'harvest' );
var config = server.methods.getService( 'harvest' );

// If we have authentication credentials then lets do some http basic auth
if( config.authentication ) {
config.auth = config.authentication.username + ':' + config.authentication.password;
}
// If we have authentication credentials then lets do some http basic auth
if ( config.authentication ) {
config.auth = config.authentication.username + ':' + config.authentication.password;
}

// Register the Harvest Database using url format to set up the connection details
var Nano = require( 'nano' )( url.format( config ) );
var harvestDb = Nano.db.use( 'harvest' );
// Register the Harvest Database using url format to set up the connection details
var Nano = require( 'nano' )( url.format( config ) );
var harvestDb = Nano.db.use( 'harvest' );

server.expose( 'getBasket', function( options ) {
return getBasket( Harvest, harvestDb, options );
} );
server.expose( 'getBasket', function( options ) {
return getBasket( Harvest, harvestDb, options );
} );

server.expose( 'getBaskets', function( options ) {
return getBaskets( Harvest, harvestDb, options );
} );
server.expose( 'getBaskets', function( options ) {
return getBaskets( Harvest, harvestDb, options );
} );

server.expose( 'postBasket', function( options ) {
return postBasket( Harvest, harvestDb, options );
} );
server.expose( 'postBasket', function( options ) {
return postBasket( Harvest, harvestDb, options );
} );

server.expose( 'putBasket', function( options ) {
return putBasket( Harvest, harvestDb, options );
} );
server.expose( 'putBasket', function( options ) {
return putBasket( Harvest, harvestDb, options );
} );

server.expose( 'postBasketVersion', function( options ) {
return postBasketVersion( Harvest, harvestDb, options );
} );
server.expose( 'postBasketVersion', function( options ) {
return postBasketVersion( Harvest, harvestDb, options );
} );

next();
next();
};

exports.register.attributes = {
pkg: require( '../package.json' )
pkg: require( '../package.json' )
};
Loading

0 comments on commit daf7e99

Please sign in to comment.