Skip to content

Commit

Permalink
feat(rbrain): cached response won't decrement usage
Browse files Browse the repository at this point in the history
  • Loading branch information
drawnepicenter committed Aug 25, 2016
1 parent 73c5651 commit 613be73
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 178 deletions.
19 changes: 8 additions & 11 deletions build/cmds/rhymebrain_cmds/combine.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ exports.handler = function (argv) {
};
http({ url: url }, function (error, response) {
if (!error && response.statusCode === 200) {
if (response.headers['x-gg-state'] === 'cached') {
config.rbrain.date.remain++;
noon.save(CFILE, config);
if (config.usage) console.log('Cached response, not decrementing usage.');
}
var list = JSON.parse(response.body);
for (var i = 0; i <= list.length - 1; i++) {
var item = list[i];
Expand All @@ -97,17 +102,9 @@ exports.handler = function (argv) {
tofile[['portmanteau' + i]] = item.combined;
}
if (argv.o) tools.outFile(argv.o, argv.f, tofile);
if (reset) {
console.log(config.rbrain.date.remain + '/' + config.rbrain.date.limit + ' requests remaining this hour.');
} else {
if (config.usage) console.log(config.rbrain.date.remain + '/' + config.rbrain.date.limit + ' requests remaining this hour, will reset in ' + (59 - minutes) + ' minutes.');
}
} else {
throw new Error('HTTP ' + response.statusCode + ': ' + error);
}
if (config.usage) reset ? console.log('Timestamp expired, reset usage limits.\n' + config.rbrain.date.remain + '/' + config.rbrain.date.limit + ' requests remaining this hour.') : console.log(config.rbrain.date.remain + '/' + config.rbrain.date.limit + ' requests remaining this hour, will reset in ' + (59 - minutes) + ' minutes.');
} else throw new Error('HTTP ' + response.statusCode + ': ' + error);
});
})();
} else {
throw new Error('Reached this hour\'s usage limit of ' + config.rbrain.date.limit + '.');
}
} else throw new Error('Reached this hour\'s usage limit of ' + config.rbrain.date.limit + '.');
};
19 changes: 8 additions & 11 deletions build/cmds/rhymebrain_cmds/info.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ exports.handler = function (argv) {
var ctstyle = _.get(chalk, theme.content.style);
http({ url: url }, function (error, response) {
if (!error && response.statusCode === 200) {
if (response.headers['x-gg-state'] === 'cached') {
config.rbrain.date.remain++;
noon.save(CFILE, config);
if (config.usage) console.log('Cached response, not decrementing usage.');
}
var info = JSON.parse(response.body);
themes.label(theme, 'right', 'Arpabet', info.pron);
themes.label(theme, 'right', 'IPA', info.ipa);
Expand All @@ -101,17 +106,9 @@ exports.handler = function (argv) {
}
themes.label(theme, 'right', 'Word Flags', flags.join(''));
if (argv.o) tools.outFile(argv.o, argv.f, tofile);
if (reset) {
console.log(config.rbrain.date.remain + '/' + config.rbrain.date.limit + ' requests remaining this hour.');
} else {
if (config.usage) console.log(config.rbrain.date.remain + '/' + config.rbrain.date.limit + ' requests remaining this hour, will reset in ' + (59 - minutes) + ' minutes.');
}
} else {
throw new Error('HTTP ' + response.statusCode + ': ' + error);
}
reset ? console.log(config.rbrain.date.remain + '/' + config.rbrain.date.limit + ' requests remaining this hour.') : console.log(config.rbrain.date.remain + '/' + config.rbrain.date.limit + ' requests remaining this hour, will reset in ' + (59 - minutes) + ' minutes.');
} else throw new Error('HTTP ' + response.statusCode + ': ' + error);
});
})();
} else {
throw new Error('Reached this hour\'s usage limit of ' + config.rbrain.date.limit + '.');
}
} else throw new Error('Reached this hour\'s usage limit of ' + config.rbrain.date.limit + '.');
};
21 changes: 9 additions & 12 deletions build/cmds/rhymebrain_cmds/rhyme.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,15 @@ exports.handler = function (argv) {
http({ url: url }, function (error, response) {
if (!error && response.statusCode === 200) {
(function () {
if (response.headers['x-gg-state'] === 'cached') {
config.rbrain.date.remain++;
noon.save(CFILE, config);
if (config.usage) console.log('Cached response, not decrementing usage.');
}
var list = JSON.parse(response.body);
var lcont = [];
_.each(list, function (item) {
lcont.push(item.word);
return lcont.push(item.word);
});
lcont.sort(function (a, b) {
if (a < b) return -1;
Expand All @@ -110,18 +115,10 @@ exports.handler = function (argv) {
rcont.sort();
themes.label(theme, 'right', task, rcont.join(', '));
if (argv.o) tools.outFile(argv.o, argv.f, tofile);
if (reset) {
console.log(config.rbrain.date.remain + '/' + config.rbrain.date.limit + ' requests remaining this hour.');
} else {
if (config.usage) console.log(config.rbrain.date.remain + '/' + config.rbrain.date.limit + ' requests remaining this hour, will reset in ' + (59 - minutes) + ' minutes.');
}
if (config.usage) reset ? console.log(config.rbrain.date.remain + '/' + config.rbrain.date.limit + ' requests remaining this hour.') : console.log(config.rbrain.date.remain + '/' + config.rbrain.date.limit + ' requests remaining this hour, will reset in ' + (59 - minutes) + ' minutes.');
})();
} else {
throw new Error('HTTP ' + response.statusCode + ': ' + error);
}
} else throw new Error('HTTP ' + response.statusCode + ': ' + error);
});
})();
} else {
throw new Error('Reached this hour\'s usage limit of ' + config.rbrain.date.limit + '.');
}
} else throw new Error('Reached this hour\'s usage limit of ' + config.rbrain.date.limit + '.');
};
66 changes: 12 additions & 54 deletions build/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,14 @@ exports.limitOnelook = function (config) {
var hours = moment(new Date()).diff(stamp, 'hours');
if (hours < 24) {
c.onelook.date.remain--;
noon.save(CFILE, c);
} else if (hours >= 24) {
reset = true;
c.onelook.date.stamp = new Date().toJSON();
c.onelook.date.remain = c.onelook.date.limit;
c.onelook.date.remain--;
noon.save(CFILE, c);
}
if (c.onelook.date.remain <= 0) {
proceed = false;
c.onelook.date.remain = 0;
noon.save(CFILE, c);
} else {
proceed = true;
}
c.onelook.date.remain <= 0 ? c.onelook.date.remain = 0 : proceed = true;
noon.save(CFILE, c);
return [c, proceed, reset];
};

Expand All @@ -58,21 +51,14 @@ exports.limitDmuse = function (config) {
var hours = moment(new Date()).diff(stamp, 'hours');
if (hours < 24) {
c.dmuse.date.remain--;
noon.save(CFILE, c);
} else if (hours >= 24) {
reset = true;
c.dmuse.date.stamp = new Date().toJSON();
c.dmuse.date.remain = c.dmuse.date.limit;
c.dmuse.date.remain--;
noon.save(CFILE, c);
}
if (c.dmuse.date.remain <= 0) {
proceed = false;
c.dmuse.date.remain = 0;
noon.save(CFILE, c);
} else {
proceed = true;
}
c.dmuse.date.remain <= 0 ? c.dmuse.date.remain = 0 : proceed = true;
noon.save(CFILE, c);
return [c, proceed, reset];
};

Expand All @@ -89,24 +75,14 @@ exports.limitRbrain = function (config) {
var minutes = moment(new Date()).diff(stamp, 'minutes');
if (minutes < 60) {
c.rbrain.date.remain--;
noon.save(CFILE, c);
} else if (minutes >= 60) {
reset = true;
c.rbrain.date.stamp = new Date().toJSON();
c.rbrain.date.remain = c.rbrain.date.limit;
console.log(chalk.white('Reset API limit to ' + c.rbrain.date.limit + '/' + c.rbrain.date.interval + '.'));
c.rbrain.date.remain--;
noon.save(CFILE, c);
}
if (c.rbrain.date.remain === 0) {
proceed = false;
} else if (c.rbrain.date.remain < 0) {
proceed = false;
c.rbrain.date.remain = 0;
noon.save(CFILE, c);
} else {
proceed = true;
}
c.rbrain.date.remain <= 0 ? c.rbrain.date.remain = 0 : proceed = true;
noon.save(CFILE, c);
return [c, proceed, reset];
};

Expand All @@ -123,24 +99,14 @@ exports.limitWordnik = function (config) {
var minutes = moment(new Date()).diff(stamp, 'minutes');
if (minutes < 60) {
c.wordnik.date.remain--;
noon.save(CFILE, c);
} else if (minutes >= 60) {
reset = true;
c.wordnik.date.stamp = new Date().toJSON();
c.wordnik.date.remain = c.wordnik.date.limit;
console.log(chalk.white('Reset API limit to ' + c.wordnik.date.limit + '/' + c.wordnik.date.interval + '.'));
c.wordnik.date.remain--;
noon.save(CFILE, c);
}
if (c.wordnik.date.remain === 0) {
proceed = false;
} else if (c.wordnik.date.remain < 0) {
proceed = false;
c.wordnik.date.remain = 0;
noon.save(CFILE, c);
} else {
proceed = true;
}
c.wordnik.date.remain <= 0 ? c.wordnik.date.remain = 0 : proceed = true;
noon.save(CFILE, c);
return [c, proceed, reset];
};

Expand All @@ -156,9 +122,7 @@ function checkOutfile(path) {
fs.statSync(path);
fileExists = true;
} catch (e) {
if (e.code === 'ENOENT') {
fileExists = false;
}
if (e.code === 'ENOENT') fileExists = false;
}
return fileExists;
}
Expand All @@ -185,9 +149,7 @@ exports.checkConfig = function (file) {
try {
fs.statSync(file);
} catch (e) {
if (e.code === 'ENOENT') {
throw new Error('No config found at ' + file + ', run: \'leximaven config init\'');
}
if (e.code === 'ENOENT') throw new Error('No config found at ' + file + ', run: \'leximaven config init\'');
}
return true;
};
Expand Down Expand Up @@ -223,9 +185,7 @@ exports.outFile = function (path, force, tofile) {
fs.writeSync(fd, xml);
fs.closeSync(fd);
console.log(chalk.white('Overwrote ' + path + ' with data.'));
} else {
console.log(chalk.white(path + ' exists, use -f to force overwrite.'));
}
} else console.log(chalk.white(path + ' exists, use -f to force overwrite.'));
} else {
var _xml = builder.buildObject(tofile);
var _fd = fs.openSync(path, 'w+');
Expand All @@ -238,9 +198,7 @@ exports.outFile = function (path, force, tofile) {
if (force) {
noon.save(path, tofile);
console.log(chalk.white('Overwrote ' + path + ' with data.'));
} else {
console.log(chalk.white(path + ' exists, use -f to force overwrite.'));
}
} else console.log(chalk.white(path + ' exists, use -f to force overwrite.'));
} else {
noon.save(path, tofile);
console.log(chalk.white('Wrote data to ' + path + '.'));
Expand Down
19 changes: 8 additions & 11 deletions src/cmds/rhymebrain_cmds/combine.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ exports.handler = (argv) => {
}
http({ url }, (error, response) => {
if (!error && response.statusCode === 200) {
if (response.headers['x-gg-state'] === 'cached') {
config.rbrain.date.remain++
noon.save(CFILE, config)
if (config.usage) console.log('Cached response, not decrementing usage.')
}
const list = JSON.parse(response.body)
for (let i = 0; i <= list.length - 1; i++) {
const item = list[i]
Expand All @@ -94,16 +99,8 @@ exports.handler = (argv) => {
tofile[[`portmanteau${i}`]] = item.combined
}
if (argv.o) tools.outFile(argv.o, argv.f, tofile)
if (reset) {
console.log(`${config.rbrain.date.remain}/${config.rbrain.date.limit} requests remaining this hour.`)
} else {
if (config.usage) console.log(`${config.rbrain.date.remain}/${config.rbrain.date.limit} requests remaining this hour, will reset in ${59 - minutes} minutes.`)
}
} else {
throw new Error(`HTTP ${response.statusCode}: ${error}`)
}
if (config.usage) reset ? console.log(`Timestamp expired, reset usage limits.\n${config.rbrain.date.remain}/${config.rbrain.date.limit} requests remaining this hour.`) : console.log(`${config.rbrain.date.remain}/${config.rbrain.date.limit} requests remaining this hour, will reset in ${59 - minutes} minutes.`)
} else throw new Error(`HTTP ${response.statusCode}: ${error}`)
})
} else {
throw new Error(`Reached this hour's usage limit of ${config.rbrain.date.limit}.`)
}
} else throw new Error(`Reached this hour's usage limit of ${config.rbrain.date.limit}.`)
}
19 changes: 8 additions & 11 deletions src/cmds/rhymebrain_cmds/info.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ exports.handler = (argv) => {
const ctstyle = _.get(chalk, theme.content.style)
http({ url }, (error, response) => {
if (!error && response.statusCode === 200) {
if (response.headers['x-gg-state'] === 'cached') {
config.rbrain.date.remain++
noon.save(CFILE, config)
if (config.usage) console.log('Cached response, not decrementing usage.')
}
const info = JSON.parse(response.body)
themes.label(theme, 'right', 'Arpabet', info.pron)
themes.label(theme, 'right', 'IPA', info.ipa)
Expand All @@ -98,16 +103,8 @@ exports.handler = (argv) => {
}
themes.label(theme, 'right', 'Word Flags', flags.join(''))
if (argv.o) tools.outFile(argv.o, argv.f, tofile)
if (reset) {
console.log(`${config.rbrain.date.remain}/${config.rbrain.date.limit} requests remaining this hour.`)
} else {
if (config.usage) console.log(`${config.rbrain.date.remain}/${config.rbrain.date.limit} requests remaining this hour, will reset in ${59 - minutes} minutes.`)
}
} else {
throw new Error(`HTTP ${response.statusCode}: ${error}`)
}
reset ? console.log(`${config.rbrain.date.remain}/${config.rbrain.date.limit} requests remaining this hour.`) : console.log(`${config.rbrain.date.remain}/${config.rbrain.date.limit} requests remaining this hour, will reset in ${59 - minutes} minutes.`)
} else throw new Error(`HTTP ${response.statusCode}: ${error}`)
})
} else {
throw new Error(`Reached this hour's usage limit of ${config.rbrain.date.limit}.`)
}
} else throw new Error(`Reached this hour's usage limit of ${config.rbrain.date.limit}.`)
}
23 changes: 9 additions & 14 deletions src/cmds/rhymebrain_cmds/rhyme.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,14 @@ exports.handler = (argv) => {
const ctstyle = _.get(chalk, theme.content.style)
http({ url }, (error, response) => {
if (!error && response.statusCode === 200) {
if (response.headers['x-gg-state'] === 'cached') {
config.rbrain.date.remain++
noon.save(CFILE, config)
if (config.usage) console.log('Cached response, not decrementing usage.')
}
const list = JSON.parse(response.body)
const lcont = []
_.each(list, (item) => {
lcont.push(item.word)
})
_.each(list, (item) => lcont.push(item.word))
lcont.sort((a, b) => {
if (a < b) return -1
if (a > b) return 1
Expand All @@ -106,16 +109,8 @@ exports.handler = (argv) => {
rcont.sort()
themes.label(theme, 'right', task, rcont.join(', '))
if (argv.o) tools.outFile(argv.o, argv.f, tofile)
if (reset) {
console.log(`${config.rbrain.date.remain}/${config.rbrain.date.limit} requests remaining this hour.`)
} else {
if (config.usage) console.log(`${config.rbrain.date.remain}/${config.rbrain.date.limit} requests remaining this hour, will reset in ${59 - minutes} minutes.`)
}
} else {
throw new Error(`HTTP ${response.statusCode}: ${error}`)
}
if (config.usage) reset ? console.log(`${config.rbrain.date.remain}/${config.rbrain.date.limit} requests remaining this hour.`) : console.log(`${config.rbrain.date.remain}/${config.rbrain.date.limit} requests remaining this hour, will reset in ${59 - minutes} minutes.`)
} else throw new Error(`HTTP ${response.statusCode}: ${error}`)
})
} else {
throw new Error(`Reached this hour's usage limit of ${config.rbrain.date.limit}.`)
}
} else throw new Error(`Reached this hour's usage limit of ${config.rbrain.date.limit}.`)
}
Loading

0 comments on commit 613be73

Please sign in to comment.