Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow creating generic twiml nodes #357

Merged
merged 4 commits into from
Jun 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 9 additions & 34 deletions lib/twiml/FaxResponse.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
/* jshint ignore:end */

var builder = require('xmlbuilder'); /* jshint ignore:line */
var TwiML = require('./TwiML'); /* jshint ignore:line */


/* jshint ignore:start */
Expand All @@ -18,9 +18,13 @@ var builder = require('xmlbuilder'); /* jshint ignore:line */
*/
/* jshint ignore:end */
function FaxResponse() {
this.response = builder.create('Response').dec('1.0', 'UTF-8');
TwiML.call(this);
this._propertyName = 'response';
}

FaxResponse.prototype = Object.create(TwiML.prototype);
FaxResponse.prototype.constructor = FaxResponse;

/* jshint ignore:start */
/**
* <Receive> TwiML Verb
Expand All @@ -36,28 +40,6 @@ FaxResponse.prototype.receive = function receive(attributes) {
return new Receive(this.response.ele('Receive', attributes));
};

/* jshint ignore:start */
/**
* Convert to TwiML
*
* @returns TwiML XML
*/
/* jshint ignore:end */
FaxResponse.prototype.toString = function toString() {
return this.response.end();
};

/* jshint ignore:start */
/**
* Add text under the TwiML node
*
* @param {string} content
*/
/* jshint ignore:end */
FaxResponse.prototype.addText = function addText(content) {
this.response.txt(content);
};


/* jshint ignore:start */
/**
Expand All @@ -68,17 +50,10 @@ FaxResponse.prototype.addText = function addText(content) {
/* jshint ignore:end */
function Receive(receive) {
this.receive = receive;
this._propertyName = 'receive';
}

/* jshint ignore:start */
/**
* Add text under the TwiML node
*
* @param {string} content
*/
/* jshint ignore:end */
Receive.prototype.addText = function addText(content) {
this.receive.txt(content);
};
Receive.prototype = Object.create(TwiML.prototype);
Receive.prototype.constructor = Receive;

module.exports = FaxResponse;
84 changes: 19 additions & 65 deletions lib/twiml/MessagingResponse.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
/* jshint ignore:end */

var builder = require('xmlbuilder'); /* jshint ignore:line */
var TwiML = require('./TwiML'); /* jshint ignore:line */


/* jshint ignore:start */
Expand All @@ -18,9 +18,13 @@ var builder = require('xmlbuilder'); /* jshint ignore:line */
*/
/* jshint ignore:end */
function MessagingResponse() {
this.response = builder.create('Response').dec('1.0', 'UTF-8');
TwiML.call(this);
this._propertyName = 'response';
}

MessagingResponse.prototype = Object.create(TwiML.prototype);
MessagingResponse.prototype.constructor = MessagingResponse;

/* jshint ignore:start */
/**
* <Message> TwiML Verb
Expand Down Expand Up @@ -55,28 +59,6 @@ MessagingResponse.prototype.redirect = function redirect(attributes, url) {
return new Redirect(this.response.ele('Redirect', attributes, url));
};

/* jshint ignore:start */
/**
* Convert to TwiML
*
* @returns TwiML XML
*/
/* jshint ignore:end */
MessagingResponse.prototype.toString = function toString() {
return this.response.end();
};

/* jshint ignore:start */
/**
* Add text under the TwiML node
*
* @param {string} content
*/
/* jshint ignore:end */
MessagingResponse.prototype.addText = function addText(content) {
this.response.txt(content);
};


/* jshint ignore:start */
/**
Expand All @@ -87,18 +69,11 @@ MessagingResponse.prototype.addText = function addText(content) {
/* jshint ignore:end */
function Redirect(redirect) {
this.redirect = redirect;
this._propertyName = 'redirect';
}

/* jshint ignore:start */
/**
* Add text under the TwiML node
*
* @param {string} content
*/
/* jshint ignore:end */
Redirect.prototype.addText = function addText(content) {
this.redirect.txt(content);
};
Redirect.prototype = Object.create(TwiML.prototype);
Redirect.prototype.constructor = Redirect;


/* jshint ignore:start */
Expand All @@ -110,8 +85,12 @@ Redirect.prototype.addText = function addText(content) {
/* jshint ignore:end */
function Message(message) {
this.message = message;
this._propertyName = 'message';
}

Message.prototype = Object.create(TwiML.prototype);
Message.prototype.constructor = Message;

/* jshint ignore:start */
/**
* <Body> TwiML Noun
Expand Down Expand Up @@ -140,17 +119,6 @@ Message.prototype.media = function media(attributes, url) {
return new Media(this.message.ele('Media', attributes, url));
};

/* jshint ignore:start */
/**
* Add text under the TwiML node
*
* @param {string} content
*/
/* jshint ignore:end */
Message.prototype.addText = function addText(content) {
this.message.txt(content);
};


/* jshint ignore:start */
/**
Expand All @@ -161,18 +129,11 @@ Message.prototype.addText = function addText(content) {
/* jshint ignore:end */
function Media(media) {
this.media = media;
this._propertyName = 'media';
}

/* jshint ignore:start */
/**
* Add text under the TwiML node
*
* @param {string} content
*/
/* jshint ignore:end */
Media.prototype.addText = function addText(content) {
this.media.txt(content);
};
Media.prototype = Object.create(TwiML.prototype);
Media.prototype.constructor = Media;


/* jshint ignore:start */
Expand All @@ -184,17 +145,10 @@ Media.prototype.addText = function addText(content) {
/* jshint ignore:end */
function Body(body) {
this.body = body;
this._propertyName = 'body';
}

/* jshint ignore:start */
/**
* Add text under the TwiML node
*
* @param {string} content
*/
/* jshint ignore:end */
Body.prototype.addText = function addText(content) {
this.body.txt(content);
};
Body.prototype = Object.create(TwiML.prototype);
Body.prototype.constructor = Body;

module.exports = MessagingResponse;
74 changes: 74 additions & 0 deletions lib/twiml/TwiML.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
'use strict';

var builder = require('xmlbuilder'); /* jshint ignore:line */

/* jshint ignore:start */
/**
* Parent TwiML object
*/
/* jshint ignore:end */
function TwiML() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so ... we could make this an ES6 class, right? I guess that would require some more significant changes to the generated classes, so if you want to punt then OK. But if we don't do it now, I'm not sure when we would ....

this.response = builder.create('Response').dec('1.0', 'UTF-8');
}

/* jshint ignore:start */
/**
* Because child elements have properties named after their class names, we need
* to translate that when we want to access that at the parent prototype level.
* So this will translate something like Say to 'say' and VoiceResponse to
* 'response'.
*/
/* jshint ignore:end */
TwiML.prototype._getXml = function _getPropertyName() {
return this[this._propertyName];
}

/* jshint ignore:start */
/**
* Convert to TwiML
*
* @returns TwiML XML
*/
/* jshint ignore:end */
TwiML.prototype.toString = function toString() {
return this._getXml().end();
}

/* jshint ignore:start */
/**
* Add text under the TwiML node
*
* @param {string} content
*/
/* jshint ignore:end */
TwiML.prototype.addText = function addText(content) {
this._getXml().txt(content);
}

/* jshint ignore:start */
/**
* Add an arbitrary tag as a child.
*
* @param {string} content
*/
/* jshint ignore:end */
TwiML.prototype.addChild = function addChild(tagName, attributes) {
return new GenericNode(this._getXml().ele(tagName, attributes));
}

/* jshint ignore:start */
/**
* Generic node
*/
/* jshint ignore:end */
function GenericNode(node) {
// must match variable name for _getPropertyName
this.node = node;
this._propertyName = 'node';
}

// "Inherit" from TwiML
GenericNode.prototype = Object.create(TwiML.prototype);
GenericNode.prototype.constructor = GenericNode;

module.exports = TwiML;
Loading