-
Notifications
You must be signed in to change notification settings - Fork 12
Refactor #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ShaAllFar
wants to merge
2
commits into
codefellows-seattle-javascript-401d12:master
Choose a base branch
from
ShaAllFar:refactor
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Refactor #25
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| Created by https://www.gitignore.io/api/macos,node,vim,windows,linux | ||
|
|
||
| out.bmp | ||
| ### 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 | ||
|
|
||
|
|
||
| ### Node ### | ||
| node_modules | ||
| # 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 | ||
|
|
||
|
|
||
|
|
||
| ### Vim ### | ||
| # swap | ||
| [._]*.s[a-w][a-z] | ||
| [._]s[a-w][a-z] | ||
| # session | ||
| Session.vim | ||
| # temporary | ||
| .netrwhist | ||
| *~ | ||
| # auto-generated tag files | ||
| tags | ||
|
|
||
|
|
||
| ### 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* |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| # **Vanilla HTTP RESTful API** | ||
|
|
||
| ## **Overview** | ||
|
|
||
| This application is a HTTP RESTful API written in vanilla JavaScript. It utilizes the GET, POST, & DELETE methods to fetch, add, and remove files in a file system. | ||
|
|
||
| ## **How To Use API** | ||
| * Clone this repository | ||
| * Open a terminal and run `npm i` to install all the application dependencies | ||
|
|
||
| #### Dependencies | ||
| * bluebird 3.4.6 | ||
| * node-uuid 1.4.7 | ||
|
|
||
| #### Dev dependencies | ||
| * chai 3.5.0 | ||
| * gulp 3.9.1 | ||
| * gulp-eslint 3.0.1 | ||
| * gulp-mocha 3.0.1 | ||
| * mocha 3.2.0 | ||
| * superagent 3.3.0 | ||
|
|
||
|
|
||
| ### **Run your server** | ||
| `node server.js` | ||
|
|
||
| In a new terminal window/tab run your HTTP method commands | ||
|
|
||
| ### **POST Request** | ||
| `http POST localhost:[port number] name='[name]' gender='[gender]'` | ||
|
|
||
| ### **GET Request** | ||
| `http GET localhost:[port number]?id='[id]'` | ||
|
|
||
| ### **DELETE Request** | ||
| `http DELETE localhost:[port number]?id='[id]'` | ||
1 change: 1 addition & 0 deletions
1
lab-shawn/data/person/00b17950-c35b-11e6-91c0-2f447f841b80.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| {"id":"00b17950-c35b-11e6-91c0-2f447f841b80","name":"test name","gender":"male"} |
1 change: 1 addition & 0 deletions
1
lab-shawn/data/person/282e3b50-c34f-11e6-a8f1-49626432423e.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| {"id":"282e3b50-c34f-11e6-a8f1-49626432423e","name":"test name","gender":"male"} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| 'use strict'; | ||
|
|
||
| const gulp = require('gulp'); | ||
| const eslint = require('gulp-eslint'); | ||
| const mocha = require('gulp-mocha'); | ||
|
|
||
| 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'],['lint']); | ||
| }); | ||
|
|
||
| gulp.task('default',['dev']); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| 'use strict'; | ||
|
|
||
| module.exports = function(req){ | ||
| return new Promise((resolve,reject) => { | ||
| if(req.method === 'POST' || req.method === 'PUT'){ | ||
| var body = ''; | ||
|
|
||
| req.on('data',data => { | ||
| body += data.toString(); | ||
| }); | ||
|
|
||
| req.on('end', () =>{ | ||
| try{ | ||
| req.body = JSON.parse(body); | ||
| resolve(req); | ||
| }catch(err){ | ||
| console.error(err); | ||
| reject(err); | ||
| } | ||
| }); | ||
| return; | ||
| } | ||
| resolve(); | ||
| }); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| 'use strict'; | ||
|
|
||
| const parseUrl = require('url').parse; | ||
| const parseQuery = require('querystring').parse; | ||
|
|
||
| module.exports = function(req){ | ||
| req.url = parseUrl(req.url); | ||
| req.url.query = parseQuery(req.url.query); | ||
| return Promise.resolve(req); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| 'use strict'; | ||
|
|
||
| module.exports = exports = {}; | ||
|
|
||
| exports.sendJSON = function(res,status,data){ | ||
| res.writeHead(status,{ | ||
| 'Content-Type':'application/json' | ||
| }); | ||
| res.write(JSON.stringify(data)); | ||
| res.end(); | ||
| }; | ||
|
|
||
| exports.sendText = function(res,status,msg){ | ||
| res.writeHead(status,{ | ||
| 'Content-Type':'text/plain' | ||
| }); | ||
| res.write(msg); | ||
| res.end(); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| 'use strict'; | ||
|
|
||
| const parseUrl = require('./parse-url.js'); | ||
| const parseJSON = require('./parse-json.js'); | ||
|
|
||
| const Router = module.exports = function(){ | ||
| this.routes = { | ||
| GET: {}, | ||
| POST: {}, | ||
| PUT: {}, | ||
| DELETE: {} | ||
| }; | ||
| }; | ||
|
|
||
| Router.prototype.get = function(endpoint,callback){ | ||
| this.routes.GET[endpoint] = callback; | ||
| }; | ||
|
|
||
| Router.prototype.post = function(endpoint,callback){ | ||
| this.routes.POST[endpoint] = callback; | ||
| }; | ||
|
|
||
| Router.prototype.put = function(endpoint,callback){ | ||
| this.routes.PUT[endpoint] = callback; | ||
| }; | ||
|
|
||
| Router.prototype.delete = function(endpoint,callback){ | ||
| this.routes.DELETE[endpoint] = callback; | ||
| }; | ||
|
|
||
| Router.prototype.route = function(){ | ||
| return (req,res) => { | ||
| Promise.all([ | ||
| parseUrl(req), | ||
| parseJSON(req) | ||
| ]) | ||
| .then( () => { | ||
| if(typeof this.routes[req.method][req.url.pathname] === 'function'){ | ||
| this.routes[req.method][req.url.pathname](req,res); | ||
| return; | ||
| } | ||
|
|
||
| console.error('route not found'); | ||
| res.sendText(res,404,'route not found'); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since you are now sending the error in res.sendText you don't need to console.error the error. This will help get rid of the errors that are being logged in the terminal when you run your tests. |
||
| }) | ||
| .catch(err => { | ||
| console.error(err); | ||
| res.sendText(res,400,'bad request'); | ||
| }); | ||
| }; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| 'use strict'; | ||
|
|
||
| const Promise = require('bluebird'); | ||
| const fs = Promise.promisifyAll(require('fs'),{suffix: 'Prom'}); | ||
|
|
||
| module.exports = exports = {}; | ||
|
|
||
| exports.createPerson = function(schemaName, person){ | ||
| if(!schemaName) return Promise.reject(new Error('expected schema name')); | ||
| if(!person) return Promise.reject(new Error('expected a person')); | ||
|
|
||
| let json = JSON.stringify(person); | ||
| return fs.writeFileProm(`${__dirname}/../data/${schemaName}/${person.id}.json`,json) | ||
| .then( () => person ) | ||
| .catch( err => Promise.reject(err)); | ||
| }; | ||
|
|
||
| exports.fetchPerson = function(schemaName,id){ | ||
| if(!schemaName) return Promise.reject(new Error('expected schema name')); | ||
| if(!id) return Promise.reject(new Error('expected an id')); | ||
|
|
||
| return fs.readFileProm(`${__dirname}/../data/${schemaName}/${id}.json`) | ||
| .then( data => { | ||
| let person = JSON.parse(data.toString()); | ||
| return person; | ||
| }) | ||
| .catch(err => Promise.reject(err)); | ||
| }; | ||
|
|
||
| exports.deletePerson = function(schemaName,id){ | ||
| if(!schemaName) return Promise.reject(new Error('expected schema name')); | ||
| if(!id) return Promise.reject(new Error('expected id')); | ||
|
|
||
| return fs.unlinkProm(`${__dirname}/../data/${schemaName}/${id}.json`) | ||
| .catch(err => Promise.reject(err)); | ||
|
|
||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| 'use strict'; | ||
|
|
||
| const uuid = require('node-uuid'); | ||
|
|
||
| module.exports = function(name, gender){ | ||
| if(!name) throw new Error('expected name'); | ||
| if(!gender) throw new Error('expected gender'); | ||
|
|
||
| this.id = uuid.v1(); | ||
| this.name = name; | ||
| this.gender = gender; | ||
| }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not necessary to include dependencies in the README since they are accessible in your package.json :)