Skip to content

Commit

Permalink
feat(wordnik): cached response won't decrement usage
Browse files Browse the repository at this point in the history
and some code golf
  • Loading branch information
drawnepicenter committed Aug 26, 2016
1 parent c42a782 commit 9783ea8
Show file tree
Hide file tree
Showing 14 changed files with 112 additions and 154 deletions.
19 changes: 8 additions & 11 deletions build/cmds/wordnik_cmds/define.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ exports.handler = function (argv) {
var conn = cstyle(theme.connector.str);
http({ url: url }, function (error, response) {
if (!error && response.statusCode === 200) {
if (response.headers['x-gg-state'] === 'cached') {
config.wordnik.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 @@ -127,17 +132,9 @@ exports.handler = function (argv) {
tofile[['source' + i]] = item.sourceDictionary;
}
if (argv.o) tools.outFile(argv.o, argv.f, tofile);
if (reset) {
console.log(config.wordnik.date.remain + '/' + config.wordnik.date.limit + ' requests remaining this hour.');
} else {
if (config.usage) console.log(config.wordnik.date.remain + '/' + config.wordnik.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, not decrementing usage.\n' + config.wordnik.date.remain + '/' + config.wordnik.date.limit + ' requests remaining this hour.') : console.log(config.wordnik.date.remain + '/' + config.wordnik.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.wordnik.date.limit + '.');
}
} else throw new Error('Reached this hour\'s usage limit of ' + config.wordnik.date.limit + '.');
};
19 changes: 8 additions & 11 deletions build/cmds/wordnik_cmds/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ exports.handler = function (argv) {
};
http({ url: url }, function (error, response) {
if (!error && response.statusCode === 200) {
if (response.headers['x-gg-state'] === 'cached') {
config.wordnik.date.remain++;
noon.save(CFILE, config);
if (config.usage) console.log('Cached response, not decrementing usage.');
}
var body = JSON.parse(response.body);
var list = body.examples;
for (var i = 0; i <= list.length - 1; i++) {
Expand All @@ -107,17 +112,9 @@ exports.handler = function (argv) {
tofile[['example' + i]] = item.text;
}
if (argv.o) tools.outFile(argv.o, argv.f, tofile);
if (reset) {
console.log(config.wordnik.date.remain + '/' + config.wordnik.date.limit + ' requests remaining this hour.');
} else {
if (config.usage) console.log(config.wordnik.date.remain + '/' + config.wordnik.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, not decrementing usage.\n' + config.wordnik.date.remain + '/' + config.wordnik.date.limit + ' requests remaining this hour.') : console.log(config.wordnik.date.remain + '/' + config.wordnik.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.wordnik.date.limit + '.');
}
} else throw new Error('Reached this hour\'s usage limit of ' + config.wordnik.date.limit + '.');
};
19 changes: 8 additions & 11 deletions build/cmds/wordnik_cmds/hyphen.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,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.wordnik.date.remain++;
noon.save(CFILE, config);
if (config.usage) console.log('Cached response, not decrementing usage.');
}
var list = JSON.parse(response.body);
var hcont = [];
for (var i = 0; i <= list.length - 1; i++) {
Expand All @@ -118,17 +123,9 @@ exports.handler = function (argv) {
}
themes.label(theme, 'right', 'Hyphenation', hcont.join(''));
if (argv.o) tools.outFile(argv.o, argv.f, tofile);
if (reset) {
console.log(config.wordnik.date.remain + '/' + config.wordnik.date.limit + ' requests remaining this hour.');
} else {
if (config.usage) console.log(config.wordnik.date.remain + '/' + config.wordnik.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, not decrementing usage.\n' + config.wordnik.date.remain + '/' + config.wordnik.date.limit + ' requests remaining this hour.') : console.log(config.wordnik.date.remain + '/' + config.wordnik.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.wordnik.date.limit + '.');
}
} else throw new Error('Reached this hour\'s usage limit of ' + config.wordnik.date.limit + '.');
};
19 changes: 8 additions & 11 deletions build/cmds/wordnik_cmds/origin.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,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.wordnik.date.remain++;
noon.save(CFILE, config);
if (config.usage) console.log('Cached response, not decrementing usage.');
}
var resp = JSON.parse(response.body);
var origin = resp[0];
parser.parseString(origin, function (err, result) {
Expand All @@ -98,17 +103,9 @@ exports.handler = function (argv) {
tofile.origin = ets;
});
if (argv.o) tools.outFile(argv.o, argv.f, tofile);
if (reset) {
console.log(config.wordnik.date.remain + '/' + config.wordnik.date.limit + ' requests remaining this hour.');
} else {
if (config.usage) console.log(config.wordnik.date.remain + '/' + config.wordnik.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, not decrementing usage.\n' + config.wordnik.date.remain + '/' + config.wordnik.date.limit + ' requests remaining this hour.') : console.log(config.wordnik.date.remain + '/' + config.wordnik.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.wordnik.date.limit + '.');
}
} else throw new Error('Reached this hour\'s usage limit of ' + config.wordnik.date.limit + '.');
};
19 changes: 8 additions & 11 deletions build/cmds/wordnik_cmds/phrase.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ exports.handler = function (argv) {
};
http({ url: url }, function (error, response) {
if (!error && response.statusCode === 200) {
if (response.headers['x-gg-state'] === 'cached') {
config.wordnik.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 @@ -107,17 +112,9 @@ exports.handler = function (argv) {
tofile[['bgram' + i]] = item.gram2;
}
if (argv.o) tools.outFile(argv.o, argv.f, tofile);
if (reset) {
console.log(config.wordnik.date.remain + '/' + config.wordnik.date.limit + ' requests remaining this hour.');
} else {
if (config.usage) console.log(config.wordnik.date.remain + '/' + config.wordnik.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, not decrementing usage.\n' + config.wordnik.date.remain + '/' + config.wordnik.date.limit + ' requests remaining this hour.') : console.log(config.wordnik.date.remain + '/' + config.wordnik.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.wordnik.date.limit + '.');
}
} else throw new Error('Reached this hour\'s usage limit of ' + config.wordnik.date.limit + '.');
};
19 changes: 8 additions & 11 deletions build/cmds/wordnik_cmds/pronounce.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ exports.handler = function (argv) {
tofile.word = word;
http({ url: url }, function (error, response) {
if (!error && response.statusCode === 200) {
if (response.headers['x-gg-state'] === 'cached') {
config.wordnik.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 @@ -116,17 +121,9 @@ exports.handler = function (argv) {
tofile[['type' + i]] = item.rawType;
}
if (argv.o) tools.outFile(argv.o, argv.f, tofile);
if (reset) {
console.log(config.wordnik.date.remain + '/' + config.wordnik.date.limit + ' requests remaining this hour.');
} else {
if (config.usage) console.log(config.wordnik.date.remain + '/' + config.wordnik.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, not decrementing usage.\n' + config.wordnik.date.remain + '/' + config.wordnik.date.limit + ' requests remaining this hour.') : console.log(config.wordnik.date.remain + '/' + config.wordnik.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.wordnik.date.limit + '.');
}
} else throw new Error('Reached this hour\'s usage limit of ' + config.wordnik.date.limit + '.');
};
19 changes: 8 additions & 11 deletions build/cmds/wordnik_cmds/relate.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ exports.handler = function (argv) {
tofile.word = word;
http({ url: url }, function (error, response) {
if (!error && response.statusCode === 200) {
if (response.headers['x-gg-state'] === 'cached') {
config.wordnik.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 @@ -108,17 +113,9 @@ exports.handler = function (argv) {
tofile[['words' + i]] = item.words.join(', ');
}
if (argv.o) tools.outFile(argv.o, argv.f, tofile);
if (reset) {
console.log(config.wordnik.date.remain + '/' + config.wordnik.date.limit + ' requests remaining this hour.');
} else {
if (config.usage) console.log(config.wordnik.date.remain + '/' + config.wordnik.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, not decrementing usage.\n' + config.wordnik.date.remain + '/' + config.wordnik.date.limit + ' requests remaining this hour.') : console.log(config.wordnik.date.remain + '/' + config.wordnik.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.wordnik.date.limit + '.');
}
} else throw new Error('Reached this hour\'s usage limit of ' + config.wordnik.date.limit + '.');
};
19 changes: 8 additions & 11 deletions src/cmds/wordnik_cmds/define.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ exports.handler = (argv) => {
const conn = cstyle(theme.connector.str)
http({ url }, (error, response) => {
if (!error && response.statusCode === 200) {
if (response.headers['x-gg-state'] === 'cached') {
config.wordnik.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 @@ -124,16 +129,8 @@ exports.handler = (argv) => {
tofile[[`source${i}`]] = item.sourceDictionary
}
if (argv.o) tools.outFile(argv.o, argv.f, tofile)
if (reset) {
console.log(`${config.wordnik.date.remain}/${config.wordnik.date.limit} requests remaining this hour.`)
} else {
if (config.usage) console.log(`${config.wordnik.date.remain}/${config.wordnik.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, not decrementing usage.\n${config.wordnik.date.remain}/${config.wordnik.date.limit} requests remaining this hour.`) : console.log(`${config.wordnik.date.remain}/${config.wordnik.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.wordnik.date.limit}.`)
}
} else throw new Error(`Reached this hour's usage limit of ${config.wordnik.date.limit}.`)
}
19 changes: 8 additions & 11 deletions src/cmds/wordnik_cmds/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ exports.handler = (argv) => {
}
http({ url }, (error, response) => {
if (!error && response.statusCode === 200) {
if (response.headers['x-gg-state'] === 'cached') {
config.wordnik.date.remain++
noon.save(CFILE, config)
if (config.usage) console.log('Cached response, not decrementing usage.')
}
const body = JSON.parse(response.body)
const list = body.examples
for (let i = 0; i <= list.length - 1; i++) {
Expand All @@ -104,16 +109,8 @@ exports.handler = (argv) => {
tofile[[`example${i}`]] = item.text
}
if (argv.o) tools.outFile(argv.o, argv.f, tofile)
if (reset) {
console.log(`${config.wordnik.date.remain}/${config.wordnik.date.limit} requests remaining this hour.`)
} else {
if (config.usage) console.log(`${config.wordnik.date.remain}/${config.wordnik.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, not decrementing usage.\n${config.wordnik.date.remain}/${config.wordnik.date.limit} requests remaining this hour.`) : console.log(`${config.wordnik.date.remain}/${config.wordnik.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.wordnik.date.limit}.`)
}
} else throw new Error(`Reached this hour's usage limit of ${config.wordnik.date.limit}.`)
}
19 changes: 8 additions & 11 deletions src/cmds/wordnik_cmds/hyphen.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,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.wordnik.date.remain++
noon.save(CFILE, config)
if (config.usage) console.log('Cached response, not decrementing usage.')
}
const list = JSON.parse(response.body)
const hcont = []
for (let i = 0; i <= list.length - 1; i++) {
Expand All @@ -115,16 +120,8 @@ exports.handler = (argv) => {
}
themes.label(theme, 'right', 'Hyphenation', hcont.join(''))
if (argv.o) tools.outFile(argv.o, argv.f, tofile)
if (reset) {
console.log(`${config.wordnik.date.remain}/${config.wordnik.date.limit} requests remaining this hour.`)
} else {
if (config.usage) console.log(`${config.wordnik.date.remain}/${config.wordnik.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, not decrementing usage.\n${config.wordnik.date.remain}/${config.wordnik.date.limit} requests remaining this hour.`) : console.log(`${config.wordnik.date.remain}/${config.wordnik.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.wordnik.date.limit}.`)
}
} else throw new Error(`Reached this hour's usage limit of ${config.wordnik.date.limit}.`)
}
Loading

0 comments on commit 9783ea8

Please sign in to comment.