Skip to content

Commit

Permalink
fix(resources): recreate resources on constructor call (#44)
Browse files Browse the repository at this point in the history
* update specs

* reinstantiate resource on call

* update cc examples
  • Loading branch information
lucasassisrosa authored Sep 23, 2020
1 parent 405d7a8 commit 364be00
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
6 changes: 5 additions & 1 deletion examples/webhook-signing/call-control/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ const apiKey = process.env.TELNYX_API_KEY;

const telnyx = Telnyx(apiKey);

telnyx.calls.create({connection_id: 'uuid', to: '+1111111111111', from: '+1111111111111'});
try {
telnyx.calls.create({connection_id: 'uuid', to: '+1111111111111', from: '+1111111111111'});
} catch (e) {
console.error(e);
}
12 changes: 11 additions & 1 deletion examples/webhook-signing/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,17 @@ app.post('/webhooks', bodyParser.json(), function(req, res) {

const call = new telnyx.Call({call_control_id: event.data.payload.call_control_id});

call.gather_using_audio({audio_url: 'https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3'});
call.gather_using_audio({audio_url: 'https://file-examples-com.github.io/uploads/2017/11/file_example_MP3_700KB.mp3'});
}
if (event.data.event_type === 'call.gather.ended') {
console.log('Call Gathered with Audio. Hanging up call control id: ' + event.data.payload.call_control_id);

const call = new telnyx.Call({call_control_id: event.data.payload.call_control_id});

call.hangup();
}
if (event.data.event_type === 'call.hangup') {
console.log('Call Hangup. call control id: ' + event.data.payload.call_control_id);
}

// Event was 'constructed', so we can respond with a 200 OK
Expand Down
19 changes: 13 additions & 6 deletions lib/telnyx.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,18 +284,25 @@ Telnyx.prototype = {

_prepResources: function() {
for (var name in resources) {
var camelCaseName = utils.pascalToCamelCase(name);
this._instantiateResource(name, this);

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

_createEmptyConstructor: function(content) {
_instantiateResource: function(name, self) {
var camelCaseName = utils.pascalToCamelCase(name);

self[camelCaseName] = new resources[name](self);

return self[camelCaseName];
},

_createConstructor: function(resourceName, self) {
return function(args) {
return Object.assign(content, args || {});
return Object.assign(self._instantiateResource(resourceName, self), args || {});
}
}
},
};

module.exports = Telnyx;
Expand Down
2 changes: 1 addition & 1 deletion test/resources/Calls.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ describe('Calls Resource', function() {
expect(response.data).to.have.property('call_session_id');
expect(response.data).to.have.property('call_leg_id');
expect(response.data).to.have.property('call_control_id');
expect(response.data).to.include({record_type: 'call', is_alive: true});
expect(response.data).to.include({record_type: 'call', is_alive: false});
}

it('Sends the correct request', function() {
Expand Down

0 comments on commit 364be00

Please sign in to comment.