agi 0.0.2 → 0.0.666

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,13 +1,9 @@
1
1
  {
2
- "author": "Brian M. Carlson <brian@enginode.com> (http://enginode.com)",
2
+ "author": "Div Garg (http://divyanshgarg.com)",
3
3
  "name": "agi",
4
- "description": "AGI (Asterisk Gateway Interface) for writing dialplan scripts",
5
- "version": "0.0.2",
6
- "repository": {
7
- "type": "git",
8
- "url": "git://github.com/brianc/node-agi.git"
9
- },
10
- "main": "index.js",
4
+ "description": "AGI - Artificial General Intelligence",
5
+ "version": "0.0.666",
6
+ "main": "lib/",
11
7
  "scripts": {
12
8
  "test": "mocha -R tap"
13
9
  },
package/.npmignore DELETED
@@ -1 +0,0 @@
1
- node_modules/
package/.travis.yml DELETED
@@ -1,9 +0,0 @@
1
- language: node_js
2
- node_js:
3
- - 0.8
4
-
5
- script:
6
- - "npm test"
7
-
8
- notifications:
9
- email: false
package/index.js DELETED
@@ -1,89 +0,0 @@
1
- var Readable = require('readable-stream');
2
- var EventEmitter = require('events').EventEmitter;
3
- var state = {
4
- init: 0,
5
- waiting: 2
6
- };
7
-
8
- var Context = function(stream) {
9
- EventEmitter.call(this);
10
- this.stream = new Readable();
11
- this.stream.wrap(stream);
12
- this.state = state.init;
13
- this.msg = "";
14
- var self = this;
15
- this.stream.on('readable', function() {
16
- //always keep the 'leftover' part of the message
17
- self.msg = self.read();
18
- });
19
- this.msg = this.read();
20
- this.variables = {};
21
- this.pending = null;
22
- };
23
-
24
- require('util').inherits(Context, EventEmitter);
25
-
26
- Context.prototype.read = function() {
27
- var buffer = this.stream.read();
28
- if(!buffer) return this.msg;
29
- this.msg += buffer.toString('utf8');
30
- if(!~this.msg.indexOf('\n\n')) return this.msg; //we don't have whole message
31
- //TODO if more than one message comes in (unlikely) we need to split
32
- if(this.state === state.init) {
33
- this.readVariables(this.msg);
34
- } else if(this.state === state.waiting) {
35
- this.readResponse(this.msg);
36
- }
37
- return "";
38
- };
39
-
40
- Context.prototype.readVariables = function(msg) {
41
- var lines = msg.split('\n');
42
- for(var i = 0; i < lines.length; i++) {
43
- var line = lines[i];
44
- var split = line.split(':')
45
- var name = split[0];
46
- var value = split[1];
47
- this.variables[name] = (value||'').trim();
48
- }
49
- this.emit('variables', this.variables);
50
- this.setState(state.waiting);
51
- return "";
52
- };
53
-
54
- Context.prototype.readResponse = function(msg) {
55
- var parsed = /^(\d{3})(?: result=)(.*)/.exec(msg);
56
- var response = {
57
- code: parseInt(parsed[1]),
58
- result: parsed[2]
59
- };
60
- if(this.pending) {
61
- this.pending(null, response);
62
- this.pending = null;
63
- }
64
- this.emit('response', response);
65
- };
66
-
67
- Context.prototype.setState = function(state) {
68
- this.state = state;
69
- };
70
-
71
- Context.prototype.send = function(msg, cb) {
72
- this.pending = cb;
73
- this.stream.write(msg);
74
- };
75
-
76
- Context.prototype.exec = function() {
77
- var args = Array.prototype.slice.call(arguments, 0);
78
- var last = args.pop();
79
- if(typeof last !== 'function') {
80
- args.push(last);
81
- last = function() { }
82
- }
83
- this.send('EXEC ' + args.join(' ') + '\n', last);
84
- }
85
-
86
- module.exports = {
87
- Context: Context,
88
- state: state
89
- }
package/test/index.js DELETED
@@ -1,97 +0,0 @@
1
- var MemoryStream = require('memstream').MemoryStream;
2
- var expect = require('expect.js');
3
- var Context = require('./../').Context;
4
- var state = require('./../').state;
5
-
6
- //helpers
7
- var writeVars = function(stream) {
8
- stream.write('agi_network: yes\n');
9
- stream.write('agi_uniqueid: 13507138.14\n');
10
- stream.write('agi_arg_1: test\n');
11
- stream.write('\n\n');
12
- };
13
-
14
- var context = function(cb) {
15
- var stream = new MemoryStream();
16
- var ctx = new Context(stream);
17
- //TODO nasty
18
- ctx.send = function(msg, cb) {
19
- ctx.pending = cb;
20
- ctx.sent = ctx.sent || [];
21
- ctx.sent.push(msg);
22
- };
23
- ctx.once('variables', function(vars) {
24
- cb(ctx);
25
- });
26
- writeVars(stream);
27
- };
28
-
29
- describe('Context', function() {
30
- beforeEach(function(done) {
31
- var self = this;
32
- context(function(context) {
33
- self.context = context;
34
- done();
35
- });
36
- });
37
-
38
- describe('parsing variables', function() {
39
- it('works', function(done) {
40
- var vars = this.context.variables;
41
- expect(vars['agi_network']).ok();
42
- expect(vars['agi_network']).to.eql('yes');
43
- expect(vars['agi_uniqueid']).to.eql('13507138.14');
44
- expect(vars['agi_arg_1']).to.eql('test');
45
- done();
46
-
47
- });
48
-
49
- it('puts context into waiting state', function() {
50
- expect(this.context.state).to.eql(state.waiting);
51
- });
52
- });
53
-
54
- describe('sending command', function() {
55
- it('writes out', function() {
56
- this.context.send('EXEC test');
57
- expect(this.context.sent.length).to.eql(1);
58
- expect(this.context.sent.pop()).to.eql('EXEC test');
59
- });
60
- });
61
-
62
- describe('context.exec', function() {
63
- it('sends exec command', function() {
64
- this.context.exec('test', 'bang', 'another');
65
- expect(this.context.sent.pop()).to.eql('EXEC test bang another\n');
66
- });
67
- });
68
-
69
- describe('command flow', function() {
70
- describe('success', function() {
71
- it('emits proper repsonse', function(done) {
72
- var context = this.context;
73
-
74
- context.on('response', function(msg) {
75
- expect(msg.code).to.equal(200);
76
- expect(msg.result).to.eql('0');
77
- done();
78
- });
79
-
80
- process.nextTick(function() {
81
- context.exec('test', 'bang', 'another');
82
- context.stream.write('200');
83
- context.stream.write(' result=0\n\n');
84
- });
85
- });
86
-
87
- it('invokes callback with response', function(done) {
88
- var context = this.context;
89
- context.exec('test', 'boom', function(err, res) {
90
- done(err);
91
- });
92
- context.stream.write('200 result=0');
93
- context.stream.write('\n\n');
94
- });
95
- });
96
- });
97
- });
File without changes