From 1a1a08768a389258cba2e3437af494f615aa0d66 Mon Sep 17 00:00:00 2001 From: reality Date: Sun, 14 Apr 2013 18:14:59 +0000 Subject: [PATCH] Map request functions to extMap. getQuoteCategory external API function [#361] --- modules/api/api.js | 16 +++++++++++----- modules/quotes/quotes.js | 13 +++++++++++++ modules/web/config.json | 2 +- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/modules/api/api.js b/modules/api/api.js index 1dcb58d..1c10b98 100644 --- a/modules/api/api.js +++ b/modules/api/api.js @@ -9,6 +9,7 @@ var api = function(dbot) { dbot.modules.web.app.get('/api/:module/:method', function(req, res) { var module = req.params.module, method = req.params.method, + reqArgs = req.query, body = { 'err': null, 'data': null }; if(!_.has(dbot.api, module)) { @@ -21,11 +22,16 @@ var api = function(dbot) { } if(!body.err) { - var func = dbot.api[module][method]; - var paramNames = func.extMap; - var args = []; + var func = dbot.api[module][method], + paramNames = func.extMap, + args = []; - // TODO: Use request params to map to extMap args + _.each(reqArgs, function(arg, name) { + var callbackIndex = paramNames.indexOf(name); + if(callbackIndex != -1) { + args[callbackIndex] = decodeURIComponent(arg); + } + }); var callbackIndex = paramNames.indexOf('callback'); if(callbackIndex != -1) { @@ -35,7 +41,7 @@ var api = function(dbot) { }; func.apply(null, args); } else { - body.data = func.call(null, args); + body.data = func.apply(null, args); res.json(body); } } else { diff --git a/modules/quotes/quotes.js b/modules/quotes/quotes.js index 269e252..f616a84 100644 --- a/modules/quotes/quotes.js +++ b/modules/quotes/quotes.js @@ -83,8 +83,21 @@ var quotes = function(dbot) { return false; } } + }, + + 'getQuoteCategory': function(name) { + console.log(name); + var key = name.trim().toLowerCase(); + if(_.has(this.quotes, key)) { + return this.quotes[key]; + } else { + return false; + } } }; + + this.api['getQuoteCategory'].external = true; + this.api['getQuoteCategory'].extMap = [ 'name' ]; this.listener = function(event) { if(event.action == 'PRIVMSG') { diff --git a/modules/web/config.json b/modules/web/config.json index f714ecc..8087b2e 100644 --- a/modules/web/config.json +++ b/modules/web/config.json @@ -1,6 +1,6 @@ { "webHost": "localhost", - "webPort": 8080, + "webPort": 9001, "externalPath": false, "help": "https://github.com/reality/depressionbot/blob/master/modules/web/README.md" }