Skip to content

Commit

Permalink
Merge c227174 into c526ad4
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasassisrosa committed Aug 8, 2019
2 parents c526ad4 + c227174 commit 7ef9aba
Show file tree
Hide file tree
Showing 8 changed files with 200 additions and 85 deletions.
25 changes: 22 additions & 3 deletions lib/TelnyxMethod.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ var makeAutoPaginationMethods = require('./autoPagination').makeAutoPaginationMe
* must be passed by the consumer of the API. Subsequent optional arguments are
* optionally passed through a hash (Object) as the penultimate argument
* (preceding the also-optional callback argument
* @param [spec.urlParamsValues=[]] Array of required arguments in the order that they
* @param [spec.paramsNames=[]] Array of required arguments in the order that they
* are to be used instead of being passed by the consumer of the API. Useful for nested resources
* in a manner that consumer doesn't need to provide path arguments
* @param [spec.paramsValues=[]] Array of substitute require arguments in `paramsNames` values
* @param [spec.encode] Function for mutating input parameters to a method.
* Usefully for applying transforms to data on a per-method basis.
* @param [spec.host] Hostname for the request.
Expand All @@ -27,8 +28,8 @@ function telnyxMethod(spec) {
var self = this;
var args = [].slice.call(arguments);

if (spec.urlParamsValues) {
args.unshift(spec.urlParamsValues);
if (spec.paramsValues) {
populateUrlParamsWithResource(self, args, spec);
}

var callback = typeof args[args.length - 1] == 'function' && args.pop();
Expand All @@ -44,4 +45,22 @@ function telnyxMethod(spec) {
};
}

/**
* Populate nested method URL params with resource object attributes that match the param name.
* This allows you to do things like setting the `call_control_id` attribute from an existing call object on a new instance of `telnyx.calls`.
*/
function populateUrlParamsWithResource(self, args, spec) {
// if url params is not in resource response data.
if (!spec.paramsValues[0]) {
spec.paramsValues = spec.paramsNames.reduce(function(result, name) {
if (self[name]) {
result.push(self[name]);
}
return result;
}, []);
}

args.unshift(spec.paramsValues);
}

module.exports = telnyxMethod;
55 changes: 27 additions & 28 deletions lib/resources/Calls.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,35 @@ var TelnyxResource = require('../TelnyxResource');
var utils = require('../utils');
var telnyxMethod = TelnyxResource.method;

function nestedMethods(callControlId) {
var commands = {};
var CALL_COMMANDS = [
'answer',
'reject',
'hangup',
'bridge',
'speak',
'fork_start',
'fork_stop',
'gather_using_audio',
'gather_using_speak',
'playback_start',
'playback_stop',
'record_start',
'record_stop',
'send_dtmf',
'transfer'
];

[
'answer',
'reject',
'hangup',
'bridge',
'speak',
'fork_start',
'fork_stop',
'gather_using_audio',
'gather_using_speak',
'playback_start',
'playback_stop',
'record_start',
'record_stop',
'send_dtmf',
'transfer'
].forEach(function(name) {
commands[name] = telnyxMethod({
function getSpec(callControlId) {
return function(methodName) {
return {
method: 'POST',
path: `/{callControlId}/actions/${name}`,
urlParams: ['callControlId'],
urlParamsValues: [callControlId],
path: `/{call_control_id}/actions/${methodName}`,
urlParams: ['call_control_id'],
paramsValues: [callControlId],
paramsNames: ['call_control_id'],
methodType: 'create',
})
});

return commands;
}
}
}

module.exports = require('../TelnyxResource').extend({
Expand All @@ -48,7 +47,7 @@ module.exports = require('../TelnyxResource').extend({
response,
telnyx,
'calls',
nestedMethods(response.data.call_control_id)
utils.createNestedMethods(telnyxMethod, CALL_COMMANDS, getSpec(response.data.call_control_id))
);
},
}),
Expand Down
33 changes: 16 additions & 17 deletions lib/resources/Conferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,25 @@ var TelnyxResource = require('../TelnyxResource');
var utils = require('../utils');
var telnyxMethod = TelnyxResource.method;

function nestedMethods(conferenceId) {
var conferences = {};
var CONFERENCES = [
'join',
'mute',
'unmute',
'hold',
'unhold',
];

[
'join',
'mute',
'unmute',
'hold',
'unhold',
].forEach(function(name) {
conferences[name] = telnyxMethod({
function getSpec(conferenceId) {
return function(methodName) {
return {
method: 'POST',
path: `/{conferenceId}/actions/${name}`,
path: `/{conferenceId}/actions/${methodName}`,
urlParams: ['conferenceId'],
urlParamsValues: [conferenceId],
paramsValues: [conferenceId],
paramsNames: ['id'],
methodType: 'create',
})
});

return conferences;
}
}
}

module.exports = require('../TelnyxResource').extend({
Expand All @@ -38,7 +37,7 @@ module.exports = require('../TelnyxResource').extend({
response,
telnyx,
'conferences',
nestedMethods(response.data.id)
utils.createNestedMethods(telnyxMethod, CONFERENCES, getSpec(response.data.id))
);
},
}),
Expand Down
65 changes: 42 additions & 23 deletions lib/resources/MessagingProfiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,60 @@ var TelnyxResource = require('../TelnyxResource');
var utils = require('../utils');
var telnyxMethod = TelnyxResource.method;

function nestedMethods(messagingProfileId) {
var methods = {};

[
'phone_numbers',
'sender_ids',
'short_codes',
].forEach(function(name) {
methods[name] = telnyxMethod({
var ACTIONS = [
'phone_numbers',
'sender_ids',
'short_codes',
];

function getSpec(messagingProfileId) {
return function(methodName) {
return {
method: 'GET',
path: `/{messagingProfileId}/${name}`,
path: `/{messagingProfileId}/${methodName}`,
urlParams: ['messagingProfileId'],
urlParamsValues: [messagingProfileId],
methodType: 'create',
})
paramsValues: [messagingProfileId],
paramsNames: ['id'],
methodType: 'list',
}
}
}

function transformResponseData(response, telnyx) {
const methods = utils.createNestedMethods(telnyxMethod, ACTIONS, getSpec(response.data.id));

methods.del = telnyxMethod({
method: 'DELETE',
path: '/{messagingProfileId}',
urlParams: ['messagingProfileId'],
paramsValues: [response.data.id],
paramsNames: ['id'],
});

return methods;
return utils.addResourceToResponseData(
response,
telnyx,
'messagingProfiles',
methods
);
}

module.exports = require('../TelnyxResource').extend({
path: 'messaging_profiles',
includeBasic: ['list', 'retrieve', 'del', 'update'],
includeBasic: ['list', 'del', 'update'],

create: telnyxMethod({
method: 'POST',

transformResponseData: function(response, telnyx) {
return utils.addResourceToResponseData(
response,
telnyx,
'messagingProfiles',
nestedMethods(response.data.id)
);
},
transformResponseData: transformResponseData,
}),

retrieve: telnyxMethod({
method: 'GET',
path: '/{id}',
urlParams: ['id'],

transformResponseData: transformResponseData,
}),

listPhoneNumbers: telnyxMethod({
Expand Down
3 changes: 2 additions & 1 deletion lib/resources/NumberReservations.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ module.exports = require('../TelnyxResource').extend({
method: 'POST',
path: '/{numberReservationId}/actions/extend',
urlParams: ['numberReservationId'],
urlParamsValues: [response.data.id],
paramsValues: [response.data.id],
paramsNames: ['id'],
methodType: 'create',
}),
}
Expand Down
28 changes: 28 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,17 @@ var utils = module.exports = {
return name[0].toLowerCase() + name.substring(1);
},

/**
* snake_case to camelCase
*/
snakeToCamelCase: function(name) {
const words = name.split('_');

return words.reduce(function(acc, nextWord) {
return acc + nextWord.charAt(0).toUpperCase() + nextWord.slice(1);
});
},

/**
* Add TelnyxResource to API response data
*
Expand All @@ -243,6 +254,23 @@ var utils = module.exports = {
return response;
},

/**
* Create multiple nested methods, in camelCase and snakeCase, using spec and method names
*
* @param [telnyxMethod] TelnyxResource Method telnyxMethod creator
* @param [names=[]] Array of method names
* @param [spec] telnyxMethod spec creator by method name
*/
createNestedMethods: function(telnyxMethod, names, spec) {
var methods = {};

names.forEach(function(name) {
methods[name] = methods[utils.snakeToCamelCase(name)] = telnyxMethod(spec(name));
})

return methods;
},

emitWarning: emitWarning,
};

Expand Down
33 changes: 32 additions & 1 deletion test/resources/Calls.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var telnyx = require('../../testUtils').getTelnyxMock();
var utils = require('../../lib/utils');
var expect = require('chai').expect;

var TEST_AUTH_KEY = 'KEY187557EC22404DB39975C43ACE661A58_9QdDI7XD5bvyahtaWx1YQo';
Expand All @@ -23,7 +24,6 @@ var COMMANDS = [
'transfer',
];


describe('Calls Resource', function() {
describe('Call Information', function() {
describe('retrieve', function() {
Expand Down Expand Up @@ -136,10 +136,15 @@ describe('Calls Resource', function() {

COMMANDS.forEach(function(command) {
describe(command, function() {
const camelCaseCommand = utils.snakeToCamelCase(command);

it('Sends the correct request', function() {
return telnyx.calls.create(callCreateData)
.then(function(response) {
const call = response.data;
call[utils.snakeToCamelCase(command)](callCommandsData[command] || {})
.then(responseFn);

return call[command](callCommandsData[command] || {})
.then(responseFn);
})
Expand All @@ -148,10 +153,36 @@ describe('Calls Resource', function() {
return telnyx.calls.create(callCreateData)
.then(function(response) {
const call = response.data;
call[utils.snakeToCamelCase(command)](callCommandsData[command] || {}, TEST_AUTH_KEY)
.then(responseFn);

return call[command](callCommandsData[command] || {}, TEST_AUTH_KEY)
.then(responseFn);
})
});

if (camelCaseCommand !== command) {
describe(camelCaseCommand, function() {
it('Sends the correct request', function() {
return telnyx.calls.create(callCreateData)
.then(function(response) {
const call = response.data;

return call[camelCaseCommand](callCommandsData[command] || {})
.then(responseFn);
})
});
it('Sends the correct request [with specified auth]', function() {
return telnyx.calls.create(callCreateData)
.then(function(response) {
const call = response.data;

return call[camelCaseCommand](callCommandsData[command] || {}, TEST_AUTH_KEY)
.then(responseFn);
})
});
})
}
});
});
});
Expand Down
Loading

0 comments on commit 7ef9aba

Please sign in to comment.