summaryrefslogtreecommitdiff
path: root/lib/fortune.js
diff options
context:
space:
mode:
authorJules Laplace <jules@okfoc.us>2015-09-03 18:31:54 -0400
committerJules Laplace <jules@okfoc.us>2015-09-03 18:31:54 -0400
commitdf4892fb2b7abdbdc59ae7f9a7048b3fee1f9a07 (patch)
tree02e61e3e40caa79de43e4d19ecf25c0c1c7fb304 /lib/fortune.js
parentc4f8d3508367de71f708825ccba48096f3490f1f (diff)
starting to write apis
Diffstat (limited to 'lib/fortune.js')
-rw-r--r--lib/fortune.js28
1 files changed, 28 insertions, 0 deletions
diff --git a/lib/fortune.js b/lib/fortune.js
new file mode 100644
index 0000000..7adba5a
--- /dev/null
+++ b/lib/fortune.js
@@ -0,0 +1,28 @@
+function choice (a){ return a[ Math.floor(Math.random()*a.length) ] }
+
+var fs = require("fs"), path = require("path")
+var fortunes = {}
+var dir = "fortune"
+
+fs.readdirSync(path.resolve(dir)).forEach(function(fn){
+
+ var file = dir + '/' + fn
+ var stat = fs.statSync(file)
+
+ if (stat && ! stat.isDirectory()) {
+ fortunes[fn] = fs.readFileSync(file)
+ .toString()
+ .split("\n")
+ .filter(function(s){ return !! s })
+ }
+
+})
+
+module.exports = function(tag){
+ if (tag in fortunes) {
+ return choice(fortunes[tag])
+ }
+ else {
+ return "bucky"
+ }
+} \ No newline at end of file