Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions lab-hawa/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"rules": {
"no-console": "off",
"indent": [ "error", 2 ],
"quotes": [ "error", "single" ],
"semi": ["error", "always"],
"linebreak-style": [ "error", "unix" ]
},
"env": {
"es6": true,
"node": true,
"mocha": true,
"jasmine": true
},
"ecmaFeatures": {
"modules": true,
"experimentalObjectRestSpread": true,
"impliedStrict": true
},
"extends": "eslint:recommended"
}
117 changes: 117 additions & 0 deletions lab-hawa/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# Created by https://www.gitignore.io/api/ma,node,macos,windows,linux

#!! ERROR: ma is undefined. Use list command to see defined gitignore types !!#

node_modules/

### Node ###
# Logs
logs
*.log
npm-debug.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules
jspm_packages

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity



### macOS ###
*.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon
# Thumbnails
._*
# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk


### Windows ###
# Windows image file caches
Thumbs.db
ehthumbs.db

# Folder config file
Desktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msm
*.msp

# Windows shortcuts
*.lnk


### Linux ###
*~

# temporary files which can be created if a process still has a handle open of a deleted file
.fuse_hidden*

# KDE directory preferences
.directory

# Linux trash folder which might appear on any partition or disk
.Trash-*

# .nfs files are created when an open file is removed but is still being accessed
.nfs*
2 changes: 2 additions & 0 deletions lab-hawa/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
__Express Middleware__
======
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"1371e7a0-d111-11e6-bb8c-4d90b94618c3","name":"example name","spiritAnimal":"example spiritAnimal","spiritAnimalName":"example spiritAnimalName"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"5577dcb0-c7dc-11e6-93bd-a13b10be40d2","name":"example name","spiritAnimal":"example spiritAnimal","spiritAnimalName":"example spiritAnimalName"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"55815290-c7dc-11e6-93bd-a13b10be40d2","name":"example name","spiritAnimal":"example spiritAnimal","spiritAnimalName":"example spiritAnimalName"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"917279f0-c7dc-11e6-9c23-f9e3f0401c40","name":"example name","spiritAnimal":"example spiritAnimal","spiritAnimalName":"example spiritAnimalName"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"a2719c30-c88c-11e6-af99-5d1dde52a62c","name":"example name","spiritAnimal":"example spiritAnimal","spiritAnimalName":"example spiritAnimalName"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"ada78bb0-c7dc-11e6-af9c-d7a9c1c62256","name":"example name","spiritAnimal":"example spiritAnimal","spiritAnimalName":"example spiritAnimalName"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"e2d4fac0-c7dc-11e6-8d84-b9f4c59011fc","name":"example name","spiritAnimal":"example spiritAnimal","spiritAnimalName":"example spiritAnimalName"}
25 changes: 25 additions & 0 deletions lab-hawa/gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

const gulp = require('gulp');
const eslint = require('gulp-eslint');
const mocha = require('gulp-mocha');

//TODO:watch each individual file??

gulp.task('test', function(){
gulp.src('./test/*-test.js', {read: false})
.pipe(mocha({reporter: 'spec'}));
});

gulp.task('lint', function(){
return gulp.src(['**/*.js','!node_modules/**'])
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError());
});

gulp.task('dev', function(){
gulp.watch(['**/*.js','!node_modules/**'], ['test', 'lint']);
});

gulp.task('default', ['dev']);
7 changes: 7 additions & 0 deletions lab-hawa/lib/cors-middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

module.exports = function(req, res, next) {
res.append('Access-Control-Allow-Origin', '*');
res.append('Access-Control-Allow-Headers', '*');
next();
};
21 changes: 21 additions & 0 deletions lab-hawa/lib/error-middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

const createError = require('http-errors');
const debug = require('debug')('spiritAnimal:error-middleware');

module.exports = function (err, req, res, next) {
console.error(err.message);

if (err.status) {
debug('user error');

res.status(err.status).send(err.name);
next();
return;
}

debug('server error');
err = createError(500, err.message);
res.status(err.status).send(err.name);
next();
};
54 changes: 54 additions & 0 deletions lab-hawa/lib/storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
'use strict';

const Promise = require('bluebird');
const fs = Promise.promisifyAll(require('fs'), {suffix: 'Prom'});
const createError = require('http-errors');
const debug = require('debug')('spiritAnimal:storage');

module.exports = exports = {};

exports.createItem = function(schemaName, item){
debug('createItem');

if (!schemaName) return Promise.reject(createError(400, 'expected schema name'));
if (!item) return Promise.reject(createError(400, 'expected item'));

let json = JSON.stringify(item);
return fs.writeFileProm(`${__dirname}/../data/${schemaName}/${item.id}.json`, json)
.then( () => item)
.catch( err => Promise.reject(createError(500, err.message)));
};

exports.fetchItem = function(schemaName, id){
debug('fetchItem');

if (!schemaName) return Promise.reject(createError(400, 'expected schema name'));
if (!id) return Promise.reject(createError(400, 'expected id'));

return fs.readFileProm(`${__dirname}/../data/${schemaName}/${id}.json`)
.then(data => {
try {
let item = JSON.parse(data.toString());
return item;
} catch (err) {
return Promise.reject(createError(500, err.message));
}
})
.catch(err => Promise.reject(createError(404, err.message)));
};

exports.deleteItem = function(schemaName, id) {
debug('deleteItem');

if (!schemaName) return Promise.reject(createError(400, 'expected schema name'));
if (!id) return Promise.reject(createError(400, 'expected id'));

return fs.unlinkProm(`${__dirname}/../data/${schemaName}/${id}.json`)
.catch( err => Promise.reject(createError(404, err.message)));
};

exports.availIDs = function(schemaName) {
return fs.readdirProm(`${__dirname}/../data/${schemaName}`)
.then ( files => files.map(name => name.split('.json')[0]))
.catch( err => Promise.reject(createError(404, err.message)));
};
62 changes: 62 additions & 0 deletions lab-hawa/model/spiritAnimal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
'use strict';

const uuid = require('node-uuid');
const createError = require('http-errors');
const debug = require('debug')('spiritAnimal:spiritAnimal');
const storage = require('../lib/storage.js');

const SpiritAnimal = module.exports = function(name, spiritAnimal, spiritAnimalName) {
debug('spirit animal constructor');

if (!name) throw createError(400, 'expected name');
if (!spiritAnimal) throw createError(400, 'expected spirit animal');
if (!spiritAnimalName) throw createError(400, 'expected favorite animal');

this.id = uuid.v1();
this.name = name;
this.spiritAnimal = spiritAnimal;
this.spiritAnimalName = spiritAnimalName;
};

SpiritAnimal.createSpiritAnimal = function (_spiritAnimal) {
debug('createItem -- spiritAnimal.js');

try {
let spiritAnimal = new SpiritAnimal(_spiritAnimal.name, _spiritAnimal.spiritAnimal, _spiritAnimal.spiritAnimalName);
return storage.createItem('spiritAnimal', spiritAnimal);
} catch (err) {
return Promise.reject(createError(400, err.message));
}
};

SpiritAnimal.fetchSpiritAnimal = function(id) {
debug('fetchSpiritAnimal');

return storage.fetchItem('spiritAnimal', id);
};

SpiritAnimal.updateSpiritAnimal = function(id, _spiritAnimal) {
debug('updateSpirtAnimal');

return storage.fetchItem('spiritAnimal', id)
.catch( err => Promise.reject(createError(404, err.message)))
.then( spiritAnimal => {
for (var prop in spiritAnimal) {
if (prop === 'id') continue;
if(_spiritAnimal[prop]) spiritAnimal[prop] = _spiritAnimal[prop];
}
return storage.createItem('spiritAnimal', spiritAnimal);
});
};

SpiritAnimal.deleteSpiritAnimal = function(id) {
debug('deleteSpiritAnimal');

return storage.deleteItem('spiritAnimal', id);
};

SpiritAnimal.fetchIDs = function() {
debug('fetchIDs');

return storage.availIDs('spiritAnimal');
};
32 changes: 32 additions & 0 deletions lab-hawa/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "lab-hawa",
"version": "1.0.0",
"description": "",
"main": "server.js",
"directories": {
"test": "test"
},
"scripts": {
"start": "DEBUG='spiritAnimal*' node server.js",
"test": "DEBUG='spiritAnimal*' mocha"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"bluebird": "^3.4.6",
"body-parser": "^1.15.2",
"chai": "^3.5.0",
"debug": "^2.5.1",
"express": "^4.14.0",
"http-errors": "^1.5.1",
"mocha": "^3.2.0",
"morgan": "^1.7.0",
"node-uuid": "^1.4.7"
},
"devDependencies": {
"chai": "^3.5.0",
"mocha": "^3.2.0",
"superagent": "^3.3.1"
}
}
Loading