From 364be00fb978da4d7048c5695b2924664321045b Mon Sep 17 00:00:00 2001 From: Lucas Rosa Date: Wed, 23 Sep 2020 18:52:34 -0300 Subject: [PATCH] fix(resources): recreate resources on constructor call (#44) * update specs * reinstantiate resource on call * update cc examples --- .../webhook-signing/call-control/index.js | 6 +++++- examples/webhook-signing/express.js | 12 +++++++++++- lib/telnyx.js | 19 +++++++++++++------ test/resources/Calls.spec.js | 2 +- 4 files changed, 30 insertions(+), 9 deletions(-) diff --git a/examples/webhook-signing/call-control/index.js b/examples/webhook-signing/call-control/index.js index 72c67de..0d6c4f0 100644 --- a/examples/webhook-signing/call-control/index.js +++ b/examples/webhook-signing/call-control/index.js @@ -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); +} diff --git a/examples/webhook-signing/express.js b/examples/webhook-signing/express.js index f9e8c5e..be530d4 100644 --- a/examples/webhook-signing/express.js +++ b/examples/webhook-signing/express.js @@ -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 diff --git a/lib/telnyx.js b/lib/telnyx.js index 86235e5..def96ed 100644 --- a/lib/telnyx.js +++ b/lib/telnyx.js @@ -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; diff --git a/test/resources/Calls.spec.js b/test/resources/Calls.spec.js index 83e9c58..6b882b3 100644 --- a/test/resources/Calls.spec.js +++ b/test/resources/Calls.spec.js @@ -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() {