-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhoiio.js
152 lines (134 loc) · 4.59 KB
/
hoiio.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
if (typeof Object.create !== 'function') {
Object.create = function (o) {
var F = function (){};
F.prototype = o;
return new F();
};
}
var hoiio = {};
hoiio.util = {
serialize : function(obj, prefix) {
var p;
var str = [];
for (p in obj)
if (obj.hasOwnProperty(p)) {
var k = prefix ? prefix + "[" + p + "]" : p, v = obj[p];
str.push(typeof v == "object" ?
serialize(v, k) :
encodeURIComponent(k) + "=" + encodeURIComponent(v));
}
return str.join("&");
},
createFuncs : function(service, uses, obj) {
var use;
for (use in uses)
if (uses.hasOwnProperty(use)) {
obj[use] = function(use) {
return function(params, callback) {
this.client.execute(service, use, params, null);
};
}(use);
}
},
createObj : function(service, uses) {
var obj = {client: hoiio.clientObj};
this.createFuncs(service, uses, obj);
return obj;
}
};
hoiio.clientObj = {
app_id : '',
access_token : '',
base_uri : "https://secure.hoiio.com/open",
getMethodURI : function() {
var str = Array.prototype.slice.call(arguments);
str.unshift(this.base_uri);
document.writeln(str);
return str.join("/");
},
request: function(uri, params, callback) {
var req = new XMLHttpRequest();
params.app_id = this.app_id;
params.access_token = this.access_token;
req.open('POST', uri, true);
req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
if (callback) {
req.onreadystatechange = callback(req);
}
document.writeln(uri);
document.writeln(hoiio.util.serialize(params));
req.send(hoiio.util.serialize(params));
},
execute: function(service, use, params, callback) {
var methodUri = this.getMethodURI(service, use);
this.request(methodUri, params, callback);
}
};
hoiio.client = function(app_id, access_token) {
// Create other services constructor
var i;
var retCli;
var serv;
var services = {
account : {
getBalance : "get_balance",
getInfo : "get_info"
},
number : {
getCountries : "get_countries",
getChoices : "get_choices",
getRates : "get_rates",
subscribe : "subscribe",
updateForwarding : "update_forwarding",
getActive : "get_active"
},
sms : {
send : "send",
sendBulk : "bulk_send",
getHistory : "get_history",
getRate : "get_rate",
getStatus : "query_status"
},
voice : {
call : "call",
conference : "conference",
hangUp : "hangup",
getHistory : "get_history",
getRate : "get_rate",
getStatus : "query_status"
},
fax : {
send : "send",
getHistory : "get_history",
getRate : "get_rate",
getStatus : "query_status"
},
ivr : {
dial : "start/dial",
play : "middle/play",
gather : "middle/gather",
record : "middle/record",
transfer : "end/transfer",
hangUp : "end/hangup"
}
};
for (serv in services)
if (services.hasOwnProperty(serv)) {
this[serv + 'Obj'] = this.util.createObj(serv, services[serv]);
if (typeof this[serv] !== 'function') {
this[serv] = function(service) {
// save the name of the service in the scope for
// the inner function
return function (client) {
var obj = Object.create(this[service + 'Obj']);
obj.client = client;
return obj;
};
}(serv);
}
}
retCli = Object.create(this.clientObj);
retCli.app_id = app_id;
retCli.access_token = access_token;
return retCli;
};