Skip to content

Commit

Permalink
Merge pull request #69 from rithmschool/routes-refactor
Browse files Browse the repository at this point in the history
Routes Refactor - Issue #54
  • Loading branch information
Nalipp authored May 5, 2021
2 parents 66de7cd + 09708c9 commit 4ef5767
Show file tree
Hide file tree
Showing 8 changed files with 10,441 additions and 292 deletions.
13 changes: 0 additions & 13 deletions __tests__/integration/app.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const app = require("../../app");
const request = require("supertest");
const fact = require("../../models/fact");
const api = require("../../routes/api");

describe("Integration/unit testing app.js", function () {
describe("GET /", function () {
Expand Down Expand Up @@ -31,17 +30,5 @@ describe("Integration/unit testing app.js", function () {
const { body } = response;
expect(body).toEqual(data);
});

test("POST /submit request should call appendToFile method in our router", async () => {
const appendToFile = jest.spyOn(api, "appendToFile");
let data = {
trivia: true,
number: 5,
fact: "5 is an odd number",
};

const response = await request(app).post("/submit").send(data);
expect(appendToFile).toHaveBeenCalled();
});
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { appendToFile } = require("../../routes/api");
const { appendToFile } = require("../../routes/numbers");
const fs = require("fs");
const app = require("../../app");
const request = require("supertest");
Expand All @@ -9,7 +9,8 @@ describe("appendToFile()", () => {
let testFile = fs.writeFile("./facts-dump/test.txt", "", (err) => {
if (err) throw err;
});
let writeToTestFile = appendToFile("./facts-dump/test.txt", "hello world");

appendToFile("./facts-dump/test.txt", "hello world");
fs.readFile("./facts-dump/test.txt", "utf8", (err, data) => {
if (err) throw err;

Expand Down
13 changes: 7 additions & 6 deletions __tests__/unit/data.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const {
reader_norm,
reader_manual,
normalize_common,
normalizeElement,
} = require("../../models/data");

jest.mock("fs");
Expand Down Expand Up @@ -106,16 +106,17 @@ describe("Unit testing functions in `models/data.js`", function () {
});
});

describe("Testing normalize_common function", function () {
describe("Testing normalizeElement function", function () {
test("function sets first letter in text to lowercase character when 'DET' tag is passed in", function () {
let element = {
date: "August 4",
text:
"A newly passed U.S. tariff act creates the system of cutters for revenue enforcement (later named the United States Revenue Cutter Service), the forerunner of the Coast Guard.",
"In response to the German invasion of Belgium, Great Britain entered World War I, declaring war on Germany.",
self: false,
pos: "DET",
};
element = normalize_common(element);
element = normalizeElement(element);
console.log("ELEMENT!", element);
expect(element.text[0]).toEqual(element.text[0].toLowerCase());
});

Expand All @@ -126,7 +127,7 @@ describe("Unit testing functions in `models/data.js`", function () {
self: true,
pos: "DET",
};
expect(normalize_common(element)).toBeUndefined();
expect(normalizeElement(element)).toBeUndefined();
});

test("function returns undefined if invalid character is passed in", function () {
Expand All @@ -137,7 +138,7 @@ describe("Unit testing functions in `models/data.js`", function () {
pos: "NP",
};

expect(normalize_common(element)).toBeUndefined();
expect(normalizeElement(element)).toBeUndefined();
});
});
});
37 changes: 12 additions & 25 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
console.log("\n\n\n=== ##### STARTING SERVER ##### ===\nat", new Date(), "\n");
// Module dependencies.

// Module dependencies.
const fs = require("fs");
const express = require("express");
const https = require("https");
const marked = require("marked");
const _ = require("underscore");
const cors = require("cors");
const favicon = require("serve-favicon");
const errorhandler = require("errorhandler");
const nunjucks = require("nunjucks");
const mousewheel = require("jquery-mousewheel");
const marked = require("marked");
const apiDocsHtml = marked(fs.readFileSync("README.md", "utf8"));
const numShares = 15;

const fact = require("./models/fact.js");
const router = require("./routes/api.js");
const { numRoutes } = require("./routes/numbers.js");
// const highcharts = require("./logs_highcharts.js");
const utils = require("./public/js/shared_utils.js");
require("dotenv").config();

const nodeEnv = process.env.NODE_ENV || "development";
const app = new express();

// fake number of viistors
// var BASE_VISITOR_TIME = new Date(1330560000000);
// var VISITOR_RATE = 1000 * 60 * 60 * 3; // 3 hours/visitor
Expand All @@ -36,7 +41,6 @@ require("dotenv").config();
// "&pubid=" +
// process.env.ADD_THIS_PUBID;
// var GET_NUM_SHARES_INTERVAL_MS = 1000 * 30;
var numShares = 15;
// var arguments = process.argv.splice(2);

// Dump all facts data to a directory
Expand Down Expand Up @@ -91,9 +95,6 @@ if (_.contains(arguments, "--dump")) {
// }
// setInterval(updateNumShares, GET_NUM_SHARES_INTERVAL_MS);

const nodeEnv = process.env.NODE_ENV || "development";
const app = express();

// Configuration and middleware

nunjucks.configure("views/", {
Expand All @@ -120,14 +121,11 @@ if (nodeEnv === "development") {
}

// Routes

router.route(app, fact);

var apiDocsHtml = marked(fs.readFileSync("README.md", "utf8"));

app.use("/js", express.static(__dirname + "/node_modules/jquery-mousewheel"));
app.use("/", numRoutes);

// TODO: Precompile this template.
// Route that renders the home page html
// source is ./README.md
app.get("/", function (req, res) {
var currDate = new Date();
res.render("index.html", {
Expand All @@ -150,17 +148,6 @@ app.get("/", function (req, res) {
});
});

// app.get("/type-time-highcharts", function (req, res) {
// res.json(highcharts.getTypeTimeHist());
// });

// app.get("/type-number-highcharts", function (req, res) {
// res.json(highcharts.getTypeNumberHist());
// });

app.post("/submit", function (req, res) {
router.appendToFile("./suggestions.json", JSON.stringify(req.body) + "\n");
res.send(req.body);
});
app.use("/js", express.static(__dirname + "/node_modules/jquery-mousewheel"));

module.exports = app;
132 changes: 81 additions & 51 deletions models/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@
const _ = require("underscore");
const fs = require("fs");
const path = require("path");
const [MIN_LENGTH, MAX_LENGTH] = [20, 150];

function reader_norm(out, pathname, callback) {
// TODO: more reliable checking if file is data file
// TODO: more reliable checking if file is data file DONE
let files = fs.readdirSync(pathname);

files.forEach((file) => {
let data;
let numbers;
if (!file.includes(".txt")) {
console.error(`Not a data file: ${pathname + file}`);
}
try {
data = fs.readFileSync(pathname + file, {
encoding: "utf8",
Expand All @@ -50,51 +54,46 @@ function reader_norm(out, pathname, callback) {
try {
_.each(numbers, function (number_data, number_key) {
let float_key = parseFloat(number_key, 10);
if (isNaN(float_key)) {
console.warn(
`Skipping invalid number_key, ${number_key} in file ${
pathname + file
}`
);
return;
}

// TODO: handle this during normalization
if (!number_data || number_data.length === 0) {
// console.warn('Skipping empty number_data for float_key', float_key, 'in file', pathname + file);
return;
}
if (!(float_key in out)) {
out[float_key] = [];
}
let o = out[float_key];
number_data.forEach((element) => {
if (!element.text || !element.text.length) {
console.warn(
`Skipping empty file (element.text is falsey) ${pathname + file}`
);
return;
}
if (callback) {
element = callback(element);
// if data parsed from numbers is valid proceed to normalizing
// the elements of each value from numbers
if (normalizeNumberData(float_key, number_data)) {
// if key doesn't currently exist in out object, create it.
if (!(float_key in out)) {
out[float_key] = [];
}
if (!element) {
return;
}
const [MIN_LENGTH, MAX_LENGTH] = [20, 150];
if (!element.manual) {
if (
element.text.length < MIN_LENGTH ||
element.text.length > MAX_LENGTH
) {

let o = out[float_key];
number_data.forEach((element) => {
if (!element.text || !element.text.length) {
console.warn(
`Skipping empty file (element.text is falsey) ${
pathname + file
}`
);
return;
}
if (callback) {
element = callback(element);
}
if (!element) {
return;
}
// const [MIN_LENGTH, MAX_LENGTH] = [20, 150];
// if (!element.manual) {
// if (
// element.text.length < MIN_LENGTH ||
// element.text.length > MAX_LENGTH
// ) {
// return;
// }
// }
o.push(element);
});
// TODO: should probably be performing this deletion also for early returns
if (o.length === 0) {
delete out[float_key];
}
o.push(element);
});
// TODO: should probably be performing this deletion also for early returns
if (o.length === 0) {
delete out[float_key];
}
});
} catch (e) {
Expand Down Expand Up @@ -181,7 +180,31 @@ function reader_manual(outs, pathname, callbacks) {
}

let countBad = 0;
function normalize_common(element) {

function normalizeNumberData(key, value) {
const numberKey = parseFloat(key, 10);
// is provided key a valid number
if (isNaN(numberKey)) {
console.warn(
`Skipping invalid number_key, ${key} in file ${pathname + file}`
);
return false;
}

// is provided value an array of fact objects with values present
if (!value || value.length === 0) {
console.warn(
`Skipping empty number_data for numberKey ${numberKey} in file ${
pathname + file
}`
);
return false;
}

return true;
}

function normalizeElement(element) {
// do not return results that contain the number itself
if (element.self) {
return undefined;
Expand All @@ -208,34 +231,41 @@ function normalize_common(element) {
// likely complex grammar that we do not support
return undefined;
}

if (!element.manual) {
if (element.text.length < MIN_LENGTH || element.text.length > MAX_LENGTH) {
return;
}
}

element.text = text;
return element;
}

let date = {};
reader_norm(date, "models/date/norm/", function (element) {
return normalize_common(element);
return normalizeElement(element);
});

let year = {};
reader_norm(year, "models/year/norm/", function (element) {
return normalize_common(element);
return normalizeElement(element);
});

let trivia = {};
let trivia_pathname = "models/trivia/";
reader_norm(trivia, "models/trivia/norm/", function (element) {
// TODO: include back non-manual results
if (element.manual) {
return normalize_common(element);
return normalizeElement(element);
} else {
return undefined;
}
});

let math = {};
reader_norm(math, "models/math/norm/", function (element) {
return normalize_common(element);
return normalizeElement(element);
});

let outs = {
Expand All @@ -245,10 +275,10 @@ let outs = {
t: trivia,
};
let callbacks = {
d: normalize_common,
y: normalize_common,
m: normalize_common,
t: normalize_common,
d: normalizeElement,
y: normalizeElement,
m: normalizeElement,
t: normalizeElement,
};
reader_manual(outs, "models/manual/", callbacks);

Expand All @@ -273,7 +303,7 @@ reader_manual(outs, "models/manual/", callbacks);
module.exports = {
reader_norm,
reader_manual,
normalize_common,
normalizeElement,
math,
trivia,
date,
Expand Down
Loading

0 comments on commit 4ef5767

Please sign in to comment.