Skip to content

Commit

Permalink
added simple JSON Restaurant Writer
Browse files Browse the repository at this point in the history
  • Loading branch information
bnorthan committed Dec 20, 2013
1 parent fbf1964 commit 064b30d
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 18 deletions.
41 changes: 27 additions & 14 deletions nodejs/HealthDataNYProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ makeCallback: function(postProcess, dataOut)
return callback;
},

makeArrayCallback: function(postProcess, dataOut, n)
makeArrayCallback: function(postProcess, dataOut, n, processArrayData)
{
console.log('make callback');
// callback to build data
Expand All @@ -61,31 +61,44 @@ makeArrayCallback: function(postProcess, dataOut, n)
}

JSONArray.push(jsonstr);
console.log(jsonstr.length);
console.log(JSONArray.length);
console.log(n);

if (JSONArray.length == n)
{
for (var j=0;j<JSONArray[0].length;j++)
if (processArrayData)
{
for (var i=0;i<JSONArray.length;i++)
{
console.log('test '+j+" "+i);
console.log(JSONArray[i][j].percentage_rate);
}
console.log('');
processArrayData(JSONArray);
}

}

});


}

return callback;
},

processArrayData: function(JSONArray)
{
var averaged=[];

for (var j=0;j<JSONArray[0].length;j++)
{
var avg=0.0;
for (var i=0;i<JSONArray.length;i++)
{
avg=avg+parseFloat(JSONArray[i][j].percentage_rate);
console.log('test '+j+" "+i);
console.log(JSONArray[i][j].percentage_rate);
}
avg=avg/JSONArray.length;

averaged.push({county_code: j+1,
percentage_rate: avg});
console.log('');
}

console.log(averaged);
},

// process data by county
processCountyData: function(jsonstr)
{
Expand Down
7 changes: 3 additions & 4 deletions nodejs/HealthDataNYTester.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var http = require('http');

var healthDataNYProcessor = require('HealthDataNYProcessor');
var healthDataNYProcessor = require('./HealthDataNYProcessor');

// declare a couple of options structures representing SODA requests
// declare some of options structures representing SODA requests
var options = {
host: 'health.data.ny.gov',
// Diabetes death rate 100000
Expand All @@ -19,7 +19,6 @@ var dontExercise ={
path: '/resource/vy66-whxp.json'
};


callback = healthDataNYProcessor.makeCallback(healthDataNYProcessor.processCountyData, healthDataNYProcessor.dataToConsole);

console.log('testconsolelog');
Expand All @@ -42,6 +41,6 @@ var optionsArray=[options, adultsOverweight, dontExercise];
for (i=0;i<optionsArray.length;i++)
{
console.log("creating callback");
callback = healthDataNYProcessor.makeArrayCallback(healthDataNYProcessor.processCountyData, null, optionsArray.length);
callback = healthDataNYProcessor.makeArrayCallback(healthDataNYProcessor.processCountyData, null, optionsArray.length, healthDataNYProcessor.processArrayData);
http.request(optionsArray[i], callback).end();
}
12 changes: 12 additions & 0 deletions nodejs/RestaurantDataTester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var restaurantWriter = require('./RestaurantDataWriter');

var json=[];

var test = {
rname: "subway",
price: "5.00"
}

restaurantWriter.writeData(test,json);

console.log(json);
11 changes: 11 additions & 0 deletions nodejs/RestaurantDataWriter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Writes restaurant data to JSON format

module.exports = {

writeData: function(params, json)
{
json.push({name: params.rname,
price: params.price});
}

}

0 comments on commit 064b30d

Please sign in to comment.