Skip to content

Instantly share code, notes, and snippets.

@gouegd
Forked from spennyf/get URLs
Last active August 29, 2015 14:08
Show Gist options
  • Save gouegd/aae61aa08b8295d52b08 to your computer and use it in GitHub Desktop.
Save gouegd/aae61aa08b8295d52b08 to your computer and use it in GitHub Desktop.
Parse.Cloud.job("saveCurrentDayItems", function(request, response) {
var utcDate = new Date();
var utcHour = utcDate.getHours();
utcDate.setHours(utcHour-4);
var currentDate = utcDate;
var days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
//var months = ['January','February','March','April','May','June','July','August','September','October','November','December'];
var dayNumber = currentDate.getDay();
if (dayNumber == 0) {
dayNumber = 6;
}
else dayNumber = dayNumber -1;
var xmlReader = require('cloud/xmlreader.js');
//First worker function, promisify xmlReader.read();
function readResponse_async(xlmString) {
var promise = new Parse.Promise();
xmlReader.read(xlmString, function (err, res) {
if(err) {
promise.reject(err);
} else {
promise.resolve(res);
}
});
return promise;
}
function saveMeals_async(meals, school, diningHallNumber, menuLocation) {
//Declare all vars up front
var i3, i4, i5, m,
TestItem = Parse.Object.extend("TestItem"),//can be reused within the loops?
promise = Parse.Promise.as();//resolved promise to start a long .then() chain
for (i3 = 0; i3 < meals.count(); i3++) {
m = meals.at(i3);
//get number of stations in day
for (i4 = 0; i4 < m.counter.count(); i4++) {
//get number of items at each station
for (i5 = 0; i5 < m.counter.at(i4).dish.count(); i5++) {
//Here a self-executing function is used to form a closure trapping `testItem`.
//Otherwise the `testItem` used in `promise.then(...)` would be the last
//remaining `testItem` created when all iterations are complete.
(function(testItem) {
testItem.set("item", m.counter.at(i4).dish.at(i5).name.text());
testItem.set("meal", m.attributes().name);
testItem.set("school", school);
testItem.set("diningHallNumber", diningHallNumber);
testItem.set("schoolMenu", menuLocation);
//build the .then() chain
promise = promise.then(function() {
return testItem.save();
});
})(new TestItem());
}
}
}
return promise;
}
var MenuURL = Parse.Object.extend("MenuURL");
var queryMenuURL = new Parse.Query(MenuURL);
//Master routine
queryMenuURL.find().then(function(resultsMenuURL) {
return resultsMenuURL.reduce(function(promise, menuItem) {
console.log(menuItem.get('URL'))
var url = menuItem.get('URL'),
school = menuItem.get("school"),
diningHallNumber = menuItem.get("diningHallNumber"),
menuLocation = menuItem.get("menuLocation");
return promise.then(function() {
console.log(url)
return Parse.Cloud.httpRequest({
url: url,
//data: ... //some properties of menuItem?
}).then(function(httpResponse) {
return readResponse_async(httpResponse.text).then(function(res) {
if (res.menu.day.at(dayNumber).meal) {
return saveMeals_async(res.menu.day.at(dayNumber).meal, school, diningHallNumber, menuLocation);
} else {
return Parse.Promise.as();//return resolved promise to keep the promise chain going.
}
});
});
}, function(err) {console.log('error 1'); return 'there was an error caught, this will result in a resolved promise because we return something in this error handler'});
}, Parse.Promise.as());
}).fail(function(err) {
console.error(err);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment