From 65a8036c871f6d079586b1fb3a54a32a612d452d Mon Sep 17 00:00:00 2001 From: antirek Date: Mon, 15 Jun 2015 15:42:35 +0700 Subject: [PATCH] use ding-dong and promises --- examples/config.js | 5 +- index.js | 7 ++- lib/handler.js | 77 ++++++----------------------- lib/logger.js | 6 --- package.json | 9 ++-- spec/HandlerSpec.js | 117 ++++++++++++-------------------------------- 6 files changed, 54 insertions(+), 167 deletions(-) diff --git a/examples/config.js b/examples/config.js index a21e243..c399a30 100644 --- a/examples/config.js +++ b/examples/config.js @@ -1,5 +1,5 @@ module.exports = { - port: 3010, + port: 3000, mongo: { connectionString: 'mongodb://localhost/regions', collection: 'regions' @@ -14,9 +14,6 @@ module.exports = { console: { colorize: true }, - syslog: { - host: 'localhost' - }, file: { filename: '/var/log/agi-number-archer.log', json: false diff --git a/index.js b/index.js index 7c1856d..2436fdf 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,4 @@ -var dingDong = require('ding-dong'); +var AGIServer = require('ding-dong'); var mongoose = require('mongoose'); var ResourceSchema = require('./lib/resource'); @@ -19,9 +19,8 @@ var Server = function (config) { mongoose.connect(config.mongo.connectionString); - dingDong - .createServer(handler.handle) - .listen(config.port); + var agiServer = AGIServer(handler.handle); + agiServer.start(config.port); logger.info("server started"); }; diff --git a/lib/handler.js b/lib/handler.js index e9ea004..bb673e7 100644 --- a/lib/handler.js +++ b/lib/handler.js @@ -2,56 +2,6 @@ var Q = require('q'); -function AgiContextPromiseWrapper(context) { - this.on = function (eventName) { - var defer = Q.defer(); - context.on(eventName, function (result) { - defer.resolve(result); - }); - return defer.promise; - }; - this.answer = function () { - var defer = Q.defer(); - context.answer(function (err) { - if (err) { - defer.reject(err); - } else { - defer.resolve(); - } - }); - return defer.promise; - }; - this.streamFile = function (filename, acceptDigits) { - var defer = Q.defer(); - context.streamFile(filename, acceptDigits, function (err) { - if (err) { - defer.reject(err); - } else { - defer.resolve(); - } - }); - return defer.promise; - }; - this.setVariable = function (varName, value) { - var defer = Q.defer(); - context.setVariable(varName, value, function (err) { - if (err) { - defer.reject(err); - } else { - defer.resolve(); - } - }); - return defer.promise; - }; - this.end = function () { - var defer = Q.defer(); - context.end(function () { - defer.resolve(); - }); - return defer.promise; - } -} - function Handler(finder, logger, config) { var counter = 1; @@ -69,12 +19,6 @@ function Handler(finder, logger, config) { var number; var callId = getCallId(); - var contextWrapper = new AgiContextPromiseWrapper(context); - - function extractAgiResultVarName(agiVariables) { - number = agiVariables[config.agiParamName]; - log('number from dialplan', number); - }; function log(message, object) { var module = 'handler'; @@ -84,12 +28,19 @@ function Handler(finder, logger, config) { logger.info(module, callId, message); } }; + + function extractAgiResultVarName(agiVariables) { + + number = agiVariables[config.agiParamName]; + log('number from dialplan', number); + return Q.resolve(); + }; function answer() { - return contextWrapper.answer() + return context.answer() .then(function () { if (config.beep) { - return contextWrapper.streamFile('beep', '#'); + return context.streamFile('beep', '#'); } }); }; @@ -100,25 +51,25 @@ function Handler(finder, logger, config) { function setAgiResultVariable(codes) { log('codes', codes); - return contextWrapper + return context .setVariable(config.resultDialPlanVarName1, codes[0]) .then(function () { - contextWrapper.setVariable(config.resultDialPlanVarName2, codes[1]) + context.setVariable(config.resultDialPlanVarName2, codes[1]) }); }; function end() { log('end'); - return contextWrapper.end(); + return context.end(); }; function processFail(err) { log('fail', err); - return contextWrapper.streamFile('invalid', '#') + return context.streamFile('invalid', '#') .then(end); }; - contextWrapper.on('variables') + return context.onEvent('variables') .then(extractAgiResultVarName) .then(answer) .then(findCode) diff --git a/lib/logger.js b/lib/logger.js index 18810e7..9d72f53 100644 --- a/lib/logger.js +++ b/lib/logger.js @@ -1,7 +1,6 @@ 'use strict'; var winston = require('winston'); -require('winston-syslog').Syslog; function Logger (config) { if(!config) config = {}; @@ -17,11 +16,6 @@ function Logger (config) { logger.add(winston.transports.Console, config.console); }; - if (config.syslog) { - config.syslog['timestamp'] = config.syslog['timestamp'] || timeformat; - logger.add(winston.transports.Syslog, config.syslog); - }; - if (config.file) { config.file['timestamp'] = config.file['timestamp'] || timeformat; logger.add(winston.transports.File, config.file); diff --git a/package.json b/package.json index 7569c7b..bb157df 100644 --- a/package.json +++ b/package.json @@ -1,14 +1,13 @@ { "name": "agi-number-archer", - "version": "0.0.5", + "version": "0.0.6", "description": "AGI server for find concordance of number and region code", "main": "index.js", "dependencies": { - "ding-dong": "^0.0.8", - "mongoose": "^3.8.23", + "ding-dong": "0.1.1", + "mongoose": "^4.0.3", "q": "^1.1.2", - "winston": "^0.9.0", - "winston-syslog": "^1.0.0" + "winston": "^0.9.0" }, "devDependencies": { "jasmine": "^2.2.1" diff --git a/spec/HandlerSpec.js b/spec/HandlerSpec.js index 4e327c4..41c8fbe 100644 --- a/spec/HandlerSpec.js +++ b/spec/HandlerSpec.js @@ -29,94 +29,41 @@ describe('Handler', function () { } }; - beforeEach(function () { - context = { - on: function (eventName, callback) { - callback({agiParam: expectedNumber}); - }, - answer: function (callback) { - callback(); - }, - streamFile: function (filename, digits, callback) { - callback(); - }, - setVariable: function (variableName, value, callback) { - callback(); - }, - end: function (callback) { - callback(); - } - }; - handler = new Handler(finder, logger, config); - }); - it('should use context "variables" event', function (done) { - context.on = function (eventName) { - expect(eventName).toBe('variables'); - done(); - }; - handler.handle(context); - }); - it('should use context answer method', function (done) { - context.answer = function () { - done(); + var Context = function() { + + var onEvent = function (event) { + expect(event).toBe('variables'); + return Q.resolve({agiParam: expectedNumber}); }; - handler.handle(context); - }); - it('should call context streamFile if beep option is set', function (done) { - config.beep = true; - context.streamFile = function (filename, acceptDigits) { - expect(filename).toBe('beep'); - expect(acceptDigits).toBe('#'); - done(); + + var answer = function () { + return Q.resolve(); }; - handler.handle(context); - }); - it("shouldn't call context streamFile if beep option is not set", function (done) { - config.beep = false; - context.streamFile = function (filename, digits, callback) { - callback(); - fail(); + + var setVariable = function (variable, value) { + expect([24, 5]).toEqual(jasmine.arrayContaining([value])); + return Q.resolve(); }; - handler.handle(context).then(function () { - done(); - }); - }); - it('should call context setVariable with expected region code', function (done) { - var expectedAgiVarName = 'testAgiVar'; - config.resultDialPlanVarName1 = expectedAgiVarName; - context.setVariable = function (varName, value) { - expect(varName).toBe(expectedAgiVarName); - expect(value).toBe(expectedRegionCode); - done(); + + var streamFile = function (filename, digits) { + return Q.resolve(); }; - handler.handle(context); - }); - it('should call context end', function (done) { - context.end = function () { - done(); + + return { + answer: answer, + onEvent: onEvent, + setVariable: setVariable, + streamFile: streamFile }; - handler.handle(context); - }); - describe('on fails', function () { - beforeEach(function () { - context.answer = function (callback) { - callback(new Error('test error')); - } - }); - it('should call context streamFile', function (done) { - context.streamFile = function (filename, digits) { - expect(filename).toBe('invalid'); - expect(digits).toBe('#'); - done(); - }; - handler.handle(context); - }); - it('should call context end', function (done) { - context.end = function () { - done(); - }; - handler.handle(context); - }); + }; + + it('standard flow', function (done) { + + context = new Context(); + handler = new Handler(finder, logger, config); + + handler.handle(context) + .finally(done); }); -}) -; \ No newline at end of file + +}); \ No newline at end of file