forked from illiatdesdindes/twilio-gas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TwimlResponse.js
136 lines (110 loc) · 4.26 KB
/
TwimlResponse.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//Public interface is a Response node with the initial set of TwiML child nodes available
/**
Create a TwiML response, which can later be serialized to XML.
@return {object} response TwiML Node
*/
var TwimlResponse = function() {
// Escape XML entites in a given string
function esc(str) {
return String(str).replace(/&/g, '&')
.replace(/\"/g, '"')
.replace(/\'/g, ''')
.replace(/</g, '<')
.replace(/>/g, '>');
}
//helper to generate a helper function which returns XML node for a given parent
function addTwimlFunction(node, twimlName) {
//Generate a function on the parent node
node[twimlName.charAt(0).toLowerCase() + twimlName.slice(1)] = function() {
var text, attributes, builder, legalNodes = [];
//Get XML components from the caller
for (var i = 0, l = arguments.length; i < l; i++) {
var arg = arguments[i];
if (typeof arg === 'string') {
text = arg;
}
else if (typeof arg === 'function') {
builder = arg;
}
else {
attributes = arg;
}
}
//determine legal sub-nodes based on the node name
switch(twimlName) {
case 'Gather': legalNodes = ['Say','Play','Pause']; break;
case 'Dial': legalNodes = ['Number','Client','Conference','Queue','Sip']; break;
default: break;
}
//create new node object
var newNode = new Node({
name:twimlName,
attributes:attributes,
text:text,
legalNodes:legalNodes
});
//create node's API for subnodes and call builder function, if need be
if (!text && legalNodes.length > 0 && builder) {
legalNodes.forEach(function(legalNodeName) {
addTwimlFunction(newNode, legalNodeName);
});
builder.call(newNode, newNode);
}
//Assemble the proper XML node and add to parent node
node.children.push(newNode);
//return the node, to allow for chaining
return node;
};
}
/**
A TwiML response node - nestable with other TwiML nodes
@param {object} config - options for HTTP request
- name {string}: name of this node
- attributes {object}: key-value pairs for XML attributes for this node
- text {string}: text content, if any, for this node
- topLevel {boolean}: indicates a top level node which should also print an XML instruction
- legalNodes {array<string>}: a list of child functions which should be allowable for this node
*/
function Node(config) {
_.extend(this,config);
this.children = [];
//create child adder functions based on legal nodes
var that = this;
this.legalNodes.forEach(function(val) {
addTwimlFunction(that,val);
});
}
//Output the contents of this XML node as a string
Node.prototype.toString = function() {
var buffer = [];
if (this.topLevel) {
buffer.push('<?xml version="1.0" encoding="UTF-8"?>');
}
//Start node
buffer.push('<'+this.name);
//handle attributes
for (var attr in this.attributes) {
buffer.push(' ' + attr + '="' + esc(this.attributes[attr]) + '"');
}
//Close start tag
buffer.push('>');
//process contents of tag
if (this.text) {
buffer.push(esc(this.text));
}
else {
//process child tags
for (var i = 0, l = this.children.length; i < l; i++) {
buffer.push(this.children[i]);
}
}
//close tag
buffer.push('</'+this.name+'>');
return buffer.join('');
};
return new Node({
topLevel:true,
name:'Response',
legalNodes:['Say', 'Play', 'Gather', 'Record', 'Sms', 'Dial', 'Enqueue', 'Leave', 'Hangup', 'Redirect', 'Reject', 'Pause']
});
};