Skip to content

Commit

Permalink
feat(onelook): cached response won't decrement usage
Browse files Browse the repository at this point in the history
also fix usage messages
  • Loading branch information
drawnepicenter committed Aug 23, 2016
1 parent 2aba656 commit 01bd9f0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 26 deletions.
28 changes: 15 additions & 13 deletions build/cmds/onelook.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ exports.handler = function (argv) {
var config = noon.load(CFILE);
var proceed = false;
var reset = false;
var stamp = new Date(config.onelook.date.stamp);
var hours = moment(new Date()).diff(stamp, 'hours');
var minutes = moment(new Date()).diff(stamp, 'minutes');
var checkStamp = tools.limitOnelook(config);
config = checkStamp[0];
proceed = checkStamp[1];
reset = checkStamp[2];
var stamp = new Date(config.onelook.date.stamp);
var hours = moment(new Date()).diff(stamp, 'hours');
var minutes = moment(new Date()).diff(stamp, 'minutes');
if (proceed) {
(function () {
var userConfig = {
Expand All @@ -75,6 +75,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.onelook.date.remain++;
noon.save(CFILE, config);
if (config.usage) console.log('Cached response, not decrementing usage.');
}
var body = response.body;
var parser = new xml2js.Parser();
parser.parseString(body, function (err, result) {
Expand Down Expand Up @@ -121,18 +126,15 @@ exports.handler = function (argv) {
}
}
if (argv.o) tools.outFile(argv.o, argv.f, tofile);
if (reset) {
console.log(config.onelook.date.remain + '/' + config.onelook.date.limit + ' requests remaining today.');
} else {
if (config.usage) console.log(config.onelook.date.remain + '/' + config.onelook.date.limit + ' requests remaining today, will reset in ' + (23 - hours) + ' hours, ' + (59 - minutes) + ' minutes.');
if (config.usage) {
if (reset) {
console.log('Timestamp expired, reset usage limits.');
console.log(config.onelook.date.remain + '/' + config.onelook.date.limit + ' requests remaining today.');
} else console.log(config.onelook.date.remain + '/' + config.onelook.date.limit + ' requests remaining today, will reset in ' + (23 - hours) + ' hours, ' + (59 - minutes) + ' minutes.');
}
});
} else {
throw new Error('HTTP ' + response.statusCode + ' ' + error);
}
} else throw new Error('HTTP ' + response.statusCode + ': ' + error);
});
})();
} else {
throw new Error('Reached today\'s usage limit of ' + config.onelook.date.limit + '.');
}
} else throw new Error('Reached today\'s usage limit of ' + config.onelook.date.limit + '.');
};
28 changes: 15 additions & 13 deletions src/cmds/onelook.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ exports.handler = (argv) => {
let config = noon.load(CFILE)
let proceed = false
let reset = false
const stamp = new Date(config.onelook.date.stamp)
const hours = moment(new Date).diff(stamp, 'hours')
const minutes = moment(new Date).diff(stamp, 'minutes')
const checkStamp = tools.limitOnelook(config)
config = checkStamp[0]
proceed = checkStamp[1]
reset = checkStamp[2]
const stamp = new Date(config.onelook.date.stamp)
const hours = moment(new Date).diff(stamp, 'hours')
const minutes = moment(new Date).diff(stamp, 'minutes')
if (proceed) {
const userConfig = {
onelook: {
Expand All @@ -72,6 +72,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.onelook.date.remain++
noon.save(CFILE, config)
if (config.usage) console.log('Cached response, not decrementing usage.')
}
const body = response.body
const parser = new xml2js.Parser()
parser.parseString(body, (err, result) => {
Expand Down Expand Up @@ -118,17 +123,14 @@ exports.handler = (argv) => {
}
}
if (argv.o) tools.outFile(argv.o, argv.f, tofile)
if (reset) {
console.log(`${config.onelook.date.remain}/${config.onelook.date.limit} requests remaining today.`)
} else {
if (config.usage) console.log(`${config.onelook.date.remain}/${config.onelook.date.limit} requests remaining today, will reset in ${23 - hours} hours, ${59 - minutes} minutes.`)
if (config.usage) {
if (reset) {
console.log('Timestamp expired, reset usage limits.')
console.log(`${config.onelook.date.remain}/${config.onelook.date.limit} requests remaining today.`)
} else console.log(`${config.onelook.date.remain}/${config.onelook.date.limit} requests remaining today, will reset in ${23 - hours} hours, ${59 - minutes} minutes.`)
}
})
} else {
throw new Error(`HTTP ${response.statusCode} ${error}`)
}
} else throw new Error(`HTTP ${response.statusCode}: ${error}`)
})
} else {
throw new Error(`Reached today's usage limit of ${config.onelook.date.limit}.`)
}
} else throw new Error(`Reached today's usage limit of ${config.onelook.date.limit}.`)
}

0 comments on commit 01bd9f0

Please sign in to comment.