Skip to content

Commit

Permalink
create object to send to client > relates #17 #18
Browse files Browse the repository at this point in the history
  • Loading branch information
VirtualDOMinic committed Aug 3, 2018
1 parent 82c1d76 commit c8a1f68
Showing 1 changed file with 71 additions and 38 deletions.
109 changes: 71 additions & 38 deletions src/handlers.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
const fs = require('fs');
const path = require('path');
const mime = require('mime-types');
const requestPkg = require('request');
const fs = require("fs");
const path = require("path");
const mime = require("mime-types");
const requestPkg = require("request");

const buildPath = function (myPath) {
return path.join(__dirname, '..', 'public', myPath);
const buildPath = function(myPath) {
return path.join(__dirname, "..", "public", myPath);
};

const handler = {
home(request, response) {
console.log('running handle.home');
fs.readFile(buildPath('index.html'), (error, file) => {
console.log("running handle.home");
fs.readFile(buildPath("index.html"), (error, file) => {
if (error) {
response.writeHead(500, { 'Content-type': 'text/plain' });
response.end('server error');
response.writeHead(500, { "Content-type": "text/plain" });
response.end("server error");
console.log(error);
return;
}
response.writeHead(200, { 'Content-Type': 'text/html' });
response.writeHead(200, { "Content-Type": "text/html" });
response.end(file);
});
},
publicHandler(request, response) {
fs.readFile(buildPath(request.url), (error, file) => {
if (error) {
response.writeHead(500, { 'Content-type': 'text/plain' });
response.end('server error');
response.writeHead(500, { "Content-type": "text/plain" });
response.end("server error");
console.log(error);
return;
}
response.writeHead(200, { 'Content-Type': mime.lookup(request.url) });
response.writeHead(200, { "Content-Type": mime.lookup(request.url) });
response.end(file);
});
},
coordsHandler(request, response) {
const latlong = request.url.split('coords/')[1];
const lat = latlong.split('&long=')[0].split('=')[1];
const long = latlong.split('&long=')[1];
const latlong = request.url.split("coords/")[1];
const lat = latlong.split("&long=")[0].split("=")[1];
const long = latlong.split("&long=")[1];
console.log(`lat ${lat} long ${long}`);
handler.policeRequest(lat, long);
response.writeHead(302, { 'Content-Type': 'text/plain' });
response.writeHead(302, { "Content-Type": "text/plain" });
response.end();

// NEXT STEPS:
Expand All @@ -50,26 +50,25 @@ const handler = {
},
postcodeHandler(request, response) {
const decoded = decodeURI(request.url);
const postcode = decoded.split('postcode/')[1].replace(/[^A-Za-z0-9]/, '');
const postcode = decoded.split("postcode/")[1].replace(/[^A-Za-z0-9]/, "");

// API request to get latitude and longitude from postcodes.io api
requestPkg(`https://api.postcodes.io/postcodes/${postcode}`, (
error,
response,
body,
) => {
console.log(`error: ${error}`);
const statusCode = response.statusCode;
body = JSON.parse(body);
console.log(
`lat: ${body.result.latitude}, long: ${body.result.longitude}`,
);
requestPkg(
`https://api.postcodes.io/postcodes/${postcode}`,
(error, response, body) => {
console.log(`error: ${error}`);
const statusCode = response.statusCode;
body = JSON.parse(body);
console.log(
`lat: ${body.result.latitude}, long: ${body.result.longitude}`
);
handler.policeRequest(body.result.latitude, body.result.longitude);
});
}
);

// PLACEHOLDER
console.log(`postcode is ${postcode}`);
response.writeHead(302, { 'Content-Type': 'text/plain' });
response.writeHead(302, { "Content-Type": "text/plain" });
response.end();

// NEXT STEPS:
Expand All @@ -79,12 +78,46 @@ const handler = {
// send object of some sorts to client
},
policeRequest(latitude, longitude) {
requestPkg(`https://data.police.uk/api/crimes-street/all-crime?lat=${latitude}&lng=${longitude}`, function(error, response, body) {
console.log(`your error is: ${error}`);
const policeData = JSON.parse(body);
console.log(policeData);
})
},
requestPkg(
`https://data.police.uk/api/crimes-street/all-crime?lat=${latitude}&lng=${longitude}`,
function(error, response, body) {
console.log(`your error is: ${error}`);
const policeData = JSON.parse(body);
handler.responseCreator(policeData);
}
);
},
responseCreator(testArr) {
// Object to send back to client
var responseObj = {
crimes: {},
streets: {}
};

// Extract crimes and streets from the large JSON response. Make street descriptions more concise with regex
var listOfCrimes = testArr.map(i => i.category);
var listOfStreets = testArr.map(i =>
i.location.street.name.replace("On or near ", "")
);

// Use callback to filter unique values from array
function getUniqueValues(val, i, self) {
return self.indexOf(val) === i;
}

var uniqueCrimes = listOfCrimes.filter(getUniqueValues);
var uniqueStreets = listOfStreets.filter(getUniqueValues);

// Initialise values in object (to avoid NaN issue)
uniqueCrimes.forEach(i => (responseObj.crimes[i] = 0));
uniqueStreets.forEach(i => (responseObj.streets[i] = 0));

// Increase count (value) of each crime (key) by one each time it appears in the array
listOfCrimes.forEach(i => (responseObj.crimes[i] += 1));
listOfStreets.forEach(i => (responseObj.streets[i] += 1));

console.log(responseObj);
}
};

module.exports = handler;

0 comments on commit c8a1f68

Please sign in to comment.