-
Notifications
You must be signed in to change notification settings - Fork 0
/
oakland_service.js
40 lines (34 loc) · 1.22 KB
/
oakland_service.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
var http = require("http");
var oaklandPublicServiceRequestsJSON = "http://data.oaklandnet.com/resource/quth-gb8e.json";
function printServiceRequest(serviceRequestObject) {
console.log("Request: " + serviceRequestObject.description + " \n at " + serviceRequestObject.reqaddress.human_address);
}
function fetchServiceRequests(callback) {
var request = http.get(oaklandPublicServiceRequestsJSON, function(response) {
// read the data from the response
var body = "";
response.on("data", function(chunk) {
body += chunk;
});
response.on("end", function() {
if (response.statusCode === 200) {
try {
var responseArray = JSON.parse(body);
// print the responsearray
for (var i = 0; i < responseArray.length; i++) {
printServiceRequest(responseArray[i]);
}
} catch(error) {
printError(error);
}
} else {
printError({message: "There was an error getting the service request data. " + http.STATUS_CODES[response.statusCode]});
}
});
});
}
// print out error messages
function printError(error) {
console.log(error.message);
}
module.exports.fetchServiceRequests = fetchServiceRequests;