-
Notifications
You must be signed in to change notification settings - Fork 9
/
angular-twilio.js
127 lines (106 loc) · 3.06 KB
/
angular-twilio.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* Part of mcwebb/angular-twilio
* Coypyright 2015 Matthew Webb <matthewowebb@gmail.com>
* MIT License
*/
angular.module('mcwebb.twilio', [])
.provider('Twilio', function () {
var apiEndpoint, credentials, accounts;
apiEndpoint = 'https://api.twilio.com/2010-04-01/';
credentials = {
accountSid: '',
authToken: ''
};
accounts = {
_default: ''
};
this.setCredentials = function (o) {
credentials.accountSid = o.accountSid;
accounts._default = o.accountSid;
credentials.authToken = o.authToken;
};
this.setAccounts = function (o) {
for (var key in o) {
if (key === '_default')
throw 'cannot add "_default" account, it\'s used internally';
else accounts[key] = o[key];
}
};
this.$get = function ($window, $http) {
var credentialsB64,
internal = {};
credentialsB64 = $window.btoa(
credentials.accountSid + ':' + credentials.authToken
);
internal.transformResourceUrl = function (url) {
if (url.substr(-1) === '/')
url = url.substr(0, url.length - 1);
return url + '.json';
};
internal.transformRequest = function (data, getHeaders) {
// If this is not an object, defer to native stringification.
if (!angular.isObject(data)) {
return (data === null) ? '' : data.toString();
}
var buffer = [];
// Serialize each key in the object.
for (var name in data) {
if (!data.hasOwnProperty(name)) continue;
var value = data[name];
buffer.push(
encodeURIComponent(name) +
'=' +
encodeURIComponent((value === null) ? '' : value )
);
}
// Serialize the buffer and clean it up for transportation.
var source = buffer
.join('&')
.replace(/%20/g, '+')
;
return source;
};
internal.generateRequest = function (method, resource, data, account) {
method = method.toUpperCase();
if (!angular.isString(account) || account.length < 1)
account = '_default';
resource = 'Accounts/' +
accounts[account] + '/' +
internal.transformResourceUrl(resource)
;
var request = {
method: method,
url: apiEndpoint + resource,
headers: {
'Authorization': 'Basic ' + credentialsB64
}
};
if (method === 'POST' || method === 'PUT') {
if (data) request.data = data;
request.transformRequest = internal.transformRequest;
request.headers['content-type'] = 'application/x-www-form-urlencoded; charset=utf-8';
} else if (data) {
request.params = data;
}
return $http(request);
};
internal.create = function (resource, data, account) {
return internal.generateRequest('POST', resource, data, account);
};
internal.read = function (resource, data, account) {
return internal.generateRequest('GET', resource, data, account);
};
internal.update = function (resource, data, account) {
return internal.generateRequest('PUT', resource, data, account);
};
internal.remove = function (resource, account) {
return internal.generateRequest('DELETE', resource, null, account);
};
return {
create: internal.create,
read: internal.read,
update: internal.update,
remove: internal.remove
};
};
});