Comparing version 0.5.0 to 0.6.0
@@ -11,4 +11,3 @@ /** | ||
var request = require('request'), | ||
moduleinfo = {exports:{}}, //TODO: just parse the f***ing JSON ourselves to avoid this hack | ||
pkginfo = require('pkginfo')(moduleinfo); | ||
moduleinfo = require('../package.json'); | ||
@@ -90,3 +89,3 @@ //REST API Config Defaults | ||
'Accept':'application/json', | ||
'User-Agent':'twilio-node/' + moduleinfo.exports.version | ||
'User-Agent':'twilio-node/' + moduleinfo.version | ||
}; | ||
@@ -93,0 +92,0 @@ |
@@ -1,5 +0,117 @@ | ||
function TwimlResponse() { | ||
var _ = require('underscore'); | ||
//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']; 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; | ||
}; | ||
} | ||
module.exports = TwimlResponse; | ||
/** | ||
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 + '="' + this.attributes[attr] + '"'); | ||
} | ||
//Close start tag | ||
buffer.push('>'); | ||
//process contents of tag | ||
if (this.text) { | ||
buffer.push(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(''); | ||
}; | ||
//Public interface is a Response node with the initial set of TwiML child nodes available | ||
module.exports = function() { | ||
return new Node({ | ||
topLevel:true, | ||
name:'Response', | ||
legalNodes:['Say', 'Play', 'Gather', 'Record', 'Sms', 'Dial', 'Enqueue', 'Leave', 'Hangup', 'Redirect', 'Reject', 'Pause'] | ||
}); | ||
}; |
{ | ||
"name":"twilio", | ||
"description":"A Twilio helper library", | ||
"version":"0.5.0", | ||
"version":"0.6.0", | ||
"author":"Kevin Whinnery <kevin.whinnery@gmail.com>", | ||
@@ -18,3 +18,2 @@ "contributors":[ | ||
"request":"2.x", | ||
"pkginfo":"0.2.x", | ||
"underscore":"1.x" | ||
@@ -21,0 +20,0 @@ }, |
# node-twilio | ||
A Node.js Twilio helper library. This client library is under active development and is not yet fully supported by Twilio. Until we move to the 0.5.x release, please check out one of the other modules that have been updated more recently: | ||
A node.js Twilio helper library. For usage infomation and API docs, head out here: | ||
* [https://npmjs.org/package/twilio-js](https://npmjs.org/package/twilio-js) | ||
* [https://npmjs.org/package/twilio-api](https://npmjs.org/package/twilio-api) | ||
[http://kwhinnery.github.com/twilio-node/](http://kwhinnery.github.com/twilio-node/) | ||
@@ -8,0 +7,0 @@ ## Roadmap |
@@ -1,6 +0,150 @@ | ||
var config = require('../config'), | ||
twilio = require('../index'); | ||
var twilio = require('../index'); | ||
describe('The TwiML Response Object', function () { | ||
//lowercase the first character of a string | ||
function lowercaseFirst(str) { | ||
return str.charAt(0).toLowerCase() + str.slice(1); | ||
} | ||
it('should generate an empty response when just the constructor is used', function() { | ||
var response = new twilio.TwimlResponse(), | ||
xml = response.toString(); | ||
expect(xml).toBe('<?xml version="1.0" encoding="UTF-8"?><Response></Response>'); | ||
}); | ||
it('should support a flat document with all supported top-level nodes', function() { | ||
['Say', 'Play', 'Gather', 'Record', 'Sms', 'Dial', 'Enqueue', 'Leave', 'Hangup', 'Redirect', 'Reject', 'Pause'].forEach(function(verb) { | ||
var response = new twilio.TwimlResponse(); | ||
response[lowercaseFirst(verb)]('some text'); | ||
var xml = response.toString(); | ||
expect(xml).toBe('<?xml version="1.0" encoding="UTF-8"?><Response><'+verb+'>some text</'+verb+'></Response>'); | ||
}); | ||
}); | ||
it('should support a flat document with say nodes, with attributes', function() { | ||
var response = new twilio.TwimlResponse(); | ||
response.say('hello world', { | ||
voice:'woman', | ||
language:'en-gb' | ||
}); | ||
response.play('foobar'); | ||
var xml = response.toString(), | ||
test = [ | ||
'<?xml version="1.0" encoding="UTF-8"?>', | ||
'<Response>', | ||
'<Say voice="woman" language="en-gb">hello world</Say>', | ||
'<Play>foobar</Play>', | ||
'</Response>' | ||
].join(''); | ||
expect(xml).toBe(test); | ||
}); | ||
it('should support chaining syntax with supported verbs', function() { | ||
var resp = new twilio.TwimlResponse(); | ||
resp.say('hola mundo', { voice:'woman', language:'es' }) | ||
.play('foobar'); | ||
var xml = resp.toString(), | ||
test = [ | ||
'<?xml version="1.0" encoding="UTF-8"?>', | ||
'<Response>', | ||
'<Say voice="woman" language="es">hola mundo</Say>', | ||
'<Play>foobar</Play>', | ||
'</Response>' | ||
].join(''); | ||
expect(xml).toBe(test); | ||
}); | ||
it('should allow for nested XML building with the Gather verb', function() { | ||
var resp = new twilio.TwimlResponse(); | ||
resp.say('hola mundo', { voice:'woman', language:'es' }) | ||
.gather({ | ||
timeout:15, | ||
finishOnKey:'#' | ||
}, function() { | ||
this.play('foobar') | ||
.say({ | ||
voice:'woman' | ||
}, 'this is some hardcore nesting action'); | ||
}) | ||
.play('foobar'); | ||
var xml = resp.toString(), | ||
test = [ | ||
'<?xml version="1.0" encoding="UTF-8"?>', | ||
'<Response>', | ||
'<Say voice="woman" language="es">hola mundo</Say>', | ||
'<Gather timeout="15" finishOnKey="#">', | ||
'<Play>foobar</Play>', | ||
'<Say voice="woman">this is some hardcore nesting action</Say>', | ||
'</Gather>', | ||
'<Play>foobar</Play>', | ||
'</Response>' | ||
].join(''); | ||
expect(xml).toBe(test); | ||
}); | ||
it('should ignore attempts at nesting for verbs that dont explicitly allow it', function() { | ||
var resp = new twilio.TwimlResponse(); | ||
resp.say(function() { | ||
this.say('this should be totally ignored.'); | ||
}); | ||
var xml = resp.toString(), | ||
test = [ | ||
'<?xml version="1.0" encoding="UTF-8"?>', | ||
'<Response>', | ||
'<Say></Say>', | ||
'</Response>' | ||
].join(''); | ||
expect(xml).toBe(test); | ||
}); | ||
it('TwiML nodes that are not supported by the parent should be undefined', function() { | ||
var resp = new twilio.TwimlResponse(); | ||
expect(resp.conference).toBeUndefined(); | ||
resp.dial({ | ||
timeout:30 | ||
}, function() { | ||
expect(this.say).toBeUndefined(); | ||
}); | ||
}); | ||
it('should allow for the parent node to be made explicit in the parent builder', function() { | ||
var resp = new twilio.TwimlResponse(); | ||
resp.say('Your conference call is starting.', { voice:'woman', language:'en-gb' }) | ||
.dial({ | ||
action:'http://example.com/something.php' | ||
}, function(node) { | ||
node.conference('waitingRoom', { | ||
beep:'false' | ||
}); | ||
}); | ||
var xml = resp.toString(), | ||
test = [ | ||
'<?xml version="1.0" encoding="UTF-8"?>', | ||
'<Response>', | ||
'<Say voice="woman" language="en-gb">Your conference call is starting.</Say>', | ||
'<Dial action="http://example.com/something.php">', | ||
'<Conference beep="false">waitingRoom</Conference>', | ||
'</Dial>', | ||
'</Response>' | ||
].join(''); | ||
expect(xml).toBe(test); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
58406
2
34
1165
0
20
- Removedpkginfo@0.2.x
- Removedpkginfo@0.2.3(transitive)