-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
124 changed files
with
7,588 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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,8 @@ | ||
line_length: 140 | ||
type_body_length: 400 | ||
|
||
disabled_rules: | ||
- identifier_name | ||
- unused_closure_parameter | ||
- force_cast | ||
- file_length |
This file contains 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,2 @@ | ||
/node_modules | ||
.DS_Store |
This file contains 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,58 @@ | ||
var express = require("express"); | ||
var app = express(); | ||
var bodyParser = require("body-parser"); | ||
var compression = require("compression"); | ||
var morgan = require("morgan"); | ||
var counters = require("./lib/counters"); | ||
|
||
app.use(morgan("combined")); | ||
app.use(bodyParser.json()); | ||
app.use(bodyParser.urlencoded({extended: true})); | ||
|
||
// [json] GET /api/v1/counters | ||
// => [ | ||
// => {id: "asdf", title: "boop", count: 4}, | ||
// => {id: "zxcv", title: "steve", count: 3} | ||
// => ] | ||
app.get("/api/v1/counters", function(req, res) { | ||
res.json(counters.all()) | ||
}); | ||
|
||
// [json] POST {title: "bob"} /api/v1/counters | ||
// => [ | ||
// => {id: "asdf", title: "boop", count: 4}, | ||
// => {id: "zxcv", title: "steve", count: 3}, | ||
// => {id: "qwer", title: "bob", count: 0} | ||
// => ] | ||
app.post("/api/v1/counter", function(req, res) { | ||
res.json(counters.create(req.body.title)); | ||
}) | ||
|
||
// [json] DELETE {id: "asdf"} /api/v1/counter | ||
// => [ | ||
// => {id: "zxcv", title: "steve", count: 3}, | ||
// => {id: "qwer", title: "bob", count: 0} | ||
// => ] | ||
app.delete("/api/v1/counter", function(req, res) { | ||
res.json(counters.delete(req.body.id)); | ||
}); | ||
|
||
// [json] POST {id: "qwer"} /api/v1/counter/inc | ||
// => [ | ||
// => {id: "zxcv", title: "steve", count: 3}, | ||
// => {id: "qwer", title: "bob", count: 1} | ||
// => ] | ||
app.post("/api/v1/counter/inc", function(req, res) { | ||
res.json(counters.inc(req.body.id)); | ||
}); | ||
|
||
// [json] POST {id: "zxcv"} /api/v1/counter/dec | ||
// => [ | ||
// => {id: "zxcv", title: "steve", count: 2}, | ||
// => {id: "qwer", title: "bob", count: 1} | ||
// => ] | ||
app.post("/api/v1/counter/dec", function(req, res) { | ||
res.json(counters.dec(req.body.id)); | ||
}); | ||
|
||
app.listen(3000, console.log.bind(null, "Listening on TCP port " + 3000)); |
This file contains 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 @@ | ||
var _ = require("lodash"); | ||
var __Counters = {}; | ||
|
||
module.exports = { | ||
all : getAll, | ||
create : create, | ||
inc : applyTo("count", inc), | ||
dec : applyTo("count", dec), | ||
delete : del | ||
}; | ||
|
||
function genId() { | ||
return (+new Date() + ~~(Math.random * 999999)).toString(36); | ||
} | ||
|
||
function getAll() { return _.map(__Counters, _.identity); } | ||
|
||
function create(title) { | ||
var id = genId(); | ||
__Counters[id] = {id: id, title: title, count: 0}; | ||
return getAll(); | ||
} | ||
|
||
function del(id) { | ||
delete __Counters[id]; | ||
return getAll(); | ||
} | ||
|
||
function applyTo(key, fn) { | ||
return function(id) { | ||
__Counters[id][key] = fn(__Counters[id][key]); | ||
return getAll(); | ||
} | ||
} | ||
|
||
function inc(n) { return n + 1; } | ||
function dec(n) { return n - 1; } |
Oops, something went wrong.