Skip to content

Commit

Permalink
errorHandling: added a few useful console loggings
Browse files Browse the repository at this point in the history
  • Loading branch information
jmishra17 committed Jul 21, 2015
1 parent 01f5925 commit 69a5921
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
1 change: 1 addition & 0 deletions wolframBot.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ module.exports = function(req, res, next){
let url = host + path + "?" + queryString;
let wolframResponse = await prequest(url);
let jsonBody = await pParseString(wolframResponse[0].body);
console.log('jsonBody --->', jsonBody);
let attachmentsObj = await wolframParser(jsonBody);
return attachmentsObj;
} catch(err) {
Expand Down
23 changes: 13 additions & 10 deletions wolframParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,20 @@ import {parseString} from 'xml2js';
let prequest = Promise.promisify(request);
let pParseString = Promise.promisify(parseString);
module.exports = function(data){
let attachments = [];
if(data.queryresult.tips){
return getTipsData(data.queryresult.tips[0]);
}
let postFixTitle = data.queryresult.pod[0].subpod[0].plaintext[0];
return processPodData(data.queryresult.pod);

async function processPodData(pods){
let asyncPodPromises = [];
let asyncPodPromises = [], syncAttachments = [], asyncAttachments = [];
_.forEach(pods, pod => {
if(pod.$.async){
console.log('async ----->');
let asyncPodPromise = prequest(pod.$.async);
asyncPodPromises.push(asyncPodPromise);
} else {
addPodToAttachments(pod);
addAttachmentToArray(pod, syncAttachments);
}
});
try {
Expand All @@ -30,13 +28,17 @@ module.exports = function(data){
});
let parseToJsonResults = await* Promise.all(parseToJsonPromises);
parseToJsonResults.map(jsonResult => {
addPodToAttachments(jsonResult.pod);
addAttachmentToArray(jsonResult.pod, asyncAttachments);
});
let text = attachments.length > 1 ?
attachments.length + ' results ..' : attachments.length + ' result ..'
console.log('syncAttachments.length -->', syncAttachments.length);
console.log('asyncAttachments.length -->', asyncAttachments.length);

let allAtachments = _.merge(syncAttachments, asyncAttachments);
let text = allAtachments.length > 1 ?
allAtachments.length + ' results ..' : allAtachments.length + ' result ..';

return {
attachments:attachments,
attachments:allAtachments,
text:text
};
} catch(err){
Expand All @@ -45,6 +47,7 @@ module.exports = function(data){
}

function getTipsData(tipsObj){
let attachments = [];
_.forEach(tipsObj.tip, tipObj => {
let attachment = {};
attachment.text = tipObj.$.text;
Expand All @@ -58,10 +61,10 @@ module.exports = function(data){
};
}

function addPodToAttachments(pod){
function addAttachmentToArray(pod, attachmentsArray){
_.forEach(pod.subpod, subpod => {
let attachment = getAttachment(pod, subpod);
attachments.push(attachment);
attachmentsArray.push(attachment);
});
}

Expand Down

0 comments on commit 69a5921

Please sign in to comment.