Skip to content

Commit

Permalink
create empty resource constructors based on resource names
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasassisrosa committed Aug 9, 2019
1 parent c65d9c1 commit a669aab
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/TelnyxResource.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ function TelnyxResource(telnyx, urlData) {
}
}

if (this.instanceMethods) {
Object.assign(this, this.instanceMethods);
}

this.initialize.apply(this, arguments);
}

Expand Down
11 changes: 10 additions & 1 deletion lib/telnyx.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,18 @@ Telnyx.prototype = {

_prepResources: function() {
for (var name in resources) {
this[utils.pascalToCamelCase(name)] = new resources[name](this);
var camelCaseName = utils.pascalToCamelCase(name);

this[camelCaseName] = new resources[name](this);
this[utils.toSingular(name)] = this._createEmptyConstructor(this[camelCaseName]);
}
},

_createEmptyConstructor: function(content) {
return function() {
return content;
}
}
};

module.exports = Telnyx;
Expand Down
9 changes: 9 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,15 @@ var utils = module.exports = {
});
},

/**
* remove final `s` from names
*/
toSingular: function(name) {
if (name.endsWith('s')) {
return name.slice(0, -1);
}
},

/**
* Add TelnyxResource to API response data
*
Expand Down

0 comments on commit a669aab

Please sign in to comment.