Class: Plivo::RestAPI

Inherits:
Object
  • Object
show all
Defined in:
lib/plivo.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(auth_id, auth_token, url = "https://api.plivo.com", version = "v1") ⇒ RestAPI

Returns a new instance of RestAPI


33
34
35
36
37
38
39
40
41
# File 'lib/plivo.rb', line 33

def initialize(auth_id, auth_token, url="https://api.plivo.com", version="v1")
    @auth_id = auth_id
    @auth_token = auth_token
    @url = url.chomp('/')
    @version = version
    @api = @url + '/' + @version + '/Account/' + @auth_id
    @headers = {"User-Agent" => "RubyPlivo"}
    @rest = RestClient::Resource.new(@api, @auth_id, @auth_token)
end

Instance Attribute Details

#apiObject

Returns the value of attribute api


31
32
33
# File 'lib/plivo.rb', line 31

def api
  @api
end

#auth_idObject

Returns the value of attribute auth_id


31
32
33
# File 'lib/plivo.rb', line 31

def auth_id
  @auth_id
end

#auth_tokenObject

Returns the value of attribute auth_token


31
32
33
# File 'lib/plivo.rb', line 31

def auth_token
  @auth_token
end

#headersObject

Returns the value of attribute headers


31
32
33
# File 'lib/plivo.rb', line 31

def headers
  @headers
end

#restObject

Returns the value of attribute rest


31
32
33
# File 'lib/plivo.rb', line 31

def rest
  @rest
end

#urlObject

Returns the value of attribute url


31
32
33
# File 'lib/plivo.rb', line 31

def url
  @url
end

#versionObject

Returns the value of attribute version


31
32
33
# File 'lib/plivo.rb', line 31

def version
  @version
end

Instance Method Details

#buy_phone_number(params = {}) ⇒ Object


166
167
168
169
# File 'lib/plivo.rb', line 166

def buy_phone_number(params={})
    number = params.delete("number")
    return request('POST', "/PhoneNumber/#{number}/", params)
end

#create_application(params = {}) ⇒ Object


142
143
144
# File 'lib/plivo.rb', line 142

def create_application(params={})
    return request('POST', "/Application/", params)
end

#create_endpoint(params = {}) ⇒ Object


415
416
417
# File 'lib/plivo.rb', line 415

def create_endpoint(params={})
    return request('POST', "/Endpoint/", params)
end

#create_incoming_carrier(params = {}) ⇒ Object


439
440
441
# File 'lib/plivo.rb', line 439

def create_incoming_carrier(params={})
    return request('POST', "/IncomingCarrier/", params)
end

#create_outgoing_carrier(params = {}) ⇒ Object


463
464
465
# File 'lib/plivo.rb', line 463

def create_outgoing_carrier(params={})
    return request('POST', "/OutgoingCarrier/", params)
end

#create_outgoing_carrier_routing(params = {}) ⇒ Object


487
488
489
# File 'lib/plivo.rb', line 487

def create_outgoing_carrier_routing(params={})
    return request('POST', "/OutgoingCarrierRouting/", params)
end

#create_subaccount(params = {}) ⇒ Object


118
119
120
# File 'lib/plivo.rb', line 118

def create_subaccount(params={})
    return request('POST', "/Subaccount/", params)
end

#deaf_member(params = {}) ⇒ Object


355
356
357
358
359
# File 'lib/plivo.rb', line 355

def deaf_member(params={})
    conference_name = params.delete('conference_name')
    member_id = params.delete('member_id')
    return request('POST', "/Conference/#{conference_name}/Member/#{member_id}/Deaf/", params)
end

#delete_application(params = {}) ⇒ Object


156
157
158
159
# File 'lib/plivo.rb', line 156

def delete_application(params={})
    app_id = params.delete("app_id")
    return request('DELETE', "/Application/#{app_id}/", params)
end

#delete_endpoint(params = {}) ⇒ Object


429
430
431
432
# File 'lib/plivo.rb', line 429

def delete_endpoint(params={})
    endpoint_id = params.delete('endpoint_id')
    return request('DELETE', "/Endpoint/#{endpoint_id}/", params)
end

#delete_incoming_carrier(params = {}) ⇒ Object


453
454
455
456
# File 'lib/plivo.rb', line 453

def delete_incoming_carrier(params={})
    carrier_id = params.delete('carrier_id')
    return request('DELETE', "/IncomingCarrier/#{carrier_id}/", params)
end

#delete_outgoing_carrier(params = {}) ⇒ Object


477
478
479
480
# File 'lib/plivo.rb', line 477

def delete_outgoing_carrier(params={})
    carrier_id = params.delete('carrier_id')
    return request('DELETE', "/OutgoingCarrier/#{carrier_id}/", params)
end

#delete_outgoing_carrier_routing(params = {}) ⇒ Object


501
502
503
504
# File 'lib/plivo.rb', line 501

def delete_outgoing_carrier_routing(params={})
    routing_id = params.delete('routing_id')
    return request('DELETE', "/OutgoingCarrierRouting/#{routing_id}/", params)
end

#delete_recording(params = {}) ⇒ Object


405
406
407
408
# File 'lib/plivo.rb', line 405

def delete_recording(params={})
    recording_id = params.delete('recording_id')
    return request('DELETE', "/Recording/#{recording_id}/", params)
end

#delete_subaccount(params = {}) ⇒ Object


132
133
134
135
# File 'lib/plivo.rb', line 132

def delete_subaccount(params={})
    subauth_id = params.delete("subauth_id") || params.delete(:subauth_id)
    return request('DELETE', "/Subaccount/#{subauth_id}/", params)
end

#get_account(params = {}) ⇒ Object

Accounts ##


106
107
108
# File 'lib/plivo.rb', line 106

def (params={})
    return request('GET', "/", params)
end

#get_application(params = {}) ⇒ Object


146
147
148
149
# File 'lib/plivo.rb', line 146

def get_application(params={})
    app_id = params.delete("app_id")
    return request('GET', "/Application/#{app_id}/", params)
end

#get_applications(params = {}) ⇒ Object

Applications ##


138
139
140
# File 'lib/plivo.rb', line 138

def get_applications(params={})
    return request('GET', "/Application/", params)
end

#get_cdr(params = {}) ⇒ Object


229
230
231
232
# File 'lib/plivo.rb', line 229

def get_cdr(params={})
    record_id = params.delete('record_id')
    return request('GET', "/Call/#{record_id}/", params)
end

#get_cdrs(params = {}) ⇒ Object

Calls ##


225
226
227
# File 'lib/plivo.rb', line 225

def get_cdrs(params={})
    return request('GET', "/Call/", params)
end

#get_endpoint(params = {}) ⇒ Object


419
420
421
422
# File 'lib/plivo.rb', line 419

def get_endpoint(params={})
    endpoint_id = params.delete('endpoint_id')
    return request('GET', "/Endpoint/#{endpoint_id}/", params)
end

#get_endpoints(params = {}) ⇒ Object

Endpoints ##


411
412
413
# File 'lib/plivo.rb', line 411

def get_endpoints(params={})
    return request('GET', "/Endpoint/", params)
end

#get_incoming_carrier(params = {}) ⇒ Object


443
444
445
446
# File 'lib/plivo.rb', line 443

def get_incoming_carrier(params={})
    carrier_id = params.delete('carrier_id')
    return request('GET', "/IncomingCarrier/#{carrier_id}/", params)
end

#get_incoming_carriers(params = {}) ⇒ Object

Incoming Carriers ##


435
436
437
# File 'lib/plivo.rb', line 435

def get_incoming_carriers(params={})
    return request('GET', "/IncomingCarrier/", params)
end

#get_live_call(params = {}) ⇒ Object


239
240
241
242
243
# File 'lib/plivo.rb', line 239

def get_live_call(params={})
    call_uuid = params.delete('call_uuid')
    params["status"] = "live"
    return request('GET', "/Call/#{call_uuid}/", params)
end

#get_live_calls(params = {}) ⇒ Object


234
235
236
237
# File 'lib/plivo.rb', line 234

def get_live_calls(params={})
    params["status"] = "live"
    return request('GET', "/Call/", params)
end

#get_live_conference(params = {}) ⇒ Object


314
315
316
317
# File 'lib/plivo.rb', line 314

def get_live_conference(params={})
    conference_name = params.delete('conference_name')
    return request('GET', "/Conference/#{conference_name}/", params)
end

#get_live_conferences(params = {}) ⇒ Object

Conferences ##


306
307
308
# File 'lib/plivo.rb', line 306

def get_live_conferences(params={})
    return request('GET', "/Conference/", params)
end

#get_message(params = {}) ⇒ Object


524
525
526
527
# File 'lib/plivo.rb', line 524

def get_message(params={})
    record_id = params.delete('record_id')
    return request('GET', "/Message/#{record_id}/", params)
end

#get_messages(params = {}) ⇒ Object


520
521
522
# File 'lib/plivo.rb', line 520

def get_messages(params={})
    return request('GET', "/Message/", params)
end

#get_number(params = {}) ⇒ Object


184
185
186
187
# File 'lib/plivo.rb', line 184

def get_number(params={})
    number = params.delete("number")
    return request('GET', "/Number/#{number}/", params)
end

#get_number_group(params = {}) ⇒ Object


210
211
212
# File 'lib/plivo.rb', line 210

def get_number_group(params={})
    return request('GET', "/AvailableNumberGroup/", params)
end

#get_number_group_details(params = {}) ⇒ Object


214
215
216
217
# File 'lib/plivo.rb', line 214

def get_number_group_details(params={})
    group_id = params.delete('group_id')
    return request('GET', "/AvailableNumberGroup/#{group_id}/", params)
end

#get_numbers(params = {}) ⇒ Object


171
172
173
# File 'lib/plivo.rb', line 171

def get_numbers(params={})
    return request('GET', "/Number/", params)
end

#get_outgoing_carrier(params = {}) ⇒ Object


467
468
469
470
# File 'lib/plivo.rb', line 467

def get_outgoing_carrier(params={})
    carrier_id = params.delete('carrier_id')
    return request('GET', "/OutgoingCarrier/#{carrier_id}/", params)
end

#get_outgoing_carrier_routing(params = {}) ⇒ Object


491
492
493
494
# File 'lib/plivo.rb', line 491

def get_outgoing_carrier_routing(params={})
    routing_id = params.delete('routing_id')
    return request('GET', "/OutgoingCarrierRouting/#{routing_id}/", params)
end

#get_outgoing_carrier_routings(params = {}) ⇒ Object

Outgoing Carrier Routings ##


483
484
485
# File 'lib/plivo.rb', line 483

def get_outgoing_carrier_routings(params={})
    return request('GET', "/OutgoingCarrierRouting/", params)
end

#get_outgoing_carriers(params = {}) ⇒ Object

Outgoing Carriers ##


459
460
461
# File 'lib/plivo.rb', line 459

def get_outgoing_carriers(params={})
    return request('GET', "/OutgoingCarrier/", params)
end

#get_recording(params = {}) ⇒ Object


400
401
402
403
# File 'lib/plivo.rb', line 400

def get_recording(params={})
    recording_id = params.delete('recording_id')
    return request('GET', "/Recording/#{recording_id}/", params)
end

#get_recordings(params = {}) ⇒ Object

Recordings ##


396
397
398
# File 'lib/plivo.rb', line 396

def get_recordings(params={})
    return request('GET', "/Recording/", params)
end

#get_subaccount(params = {}) ⇒ Object


122
123
124
125
# File 'lib/plivo.rb', line 122

def get_subaccount(params={})
    subauth_id = params.delete("subauth_id") || params.delete(:subauth_id)
    return request('GET', "/Subaccount/#{subauth_id}/", params)
end

#get_subaccounts(params = {}) ⇒ Object


114
115
116
# File 'lib/plivo.rb', line 114

def get_subaccounts(params={})
    return request('GET', "/Subaccount/", params)
end

#hangup_all_calls(params = {}) ⇒ Object


249
250
251
# File 'lib/plivo.rb', line 249

def hangup_all_calls(params={})
    return request('DELETE', "/Call/", params)
end

#hangup_all_conferences(params = {}) ⇒ Object


310
311
312
# File 'lib/plivo.rb', line 310

def hangup_all_conferences(params={})
    return request('DELETE', "/Conference/", params)
end

#hangup_call(params = {}) ⇒ Object


258
259
260
261
# File 'lib/plivo.rb', line 258

def hangup_call(params={})
    call_uuid = params.delete('call_uuid')
    return request('DELETE', "/Call/#{call_uuid}/", params)
end

#hangup_conference(params = {}) ⇒ Object


319
320
321
322
# File 'lib/plivo.rb', line 319

def hangup_conference(params={})
    conference_name = params.delete('conference_name')
    return request('DELETE', "/Conference/#{conference_name}/", params)
end

#hangup_member(params = {}) ⇒ Object


324
325
326
327
328
# File 'lib/plivo.rb', line 324

def hangup_member(params={})
    conference_name = params.delete('conference_name')
    member_id = params.delete('member_id')
    return request('DELETE', "/Conference/#{conference_name}/Member/#{member_id}/", params)
end

#hangup_request(params = {}) ⇒ Object

Calls requests ##


300
301
302
303
# File 'lib/plivo.rb', line 300

def hangup_request(params={})
    request_uuid = params.delete('request_uuid')
    return request('DELETE', "/Request/#{request_uuid}/", params)
end

#hash_to_params(myhash) ⇒ Object


43
44
45
# File 'lib/plivo.rb', line 43

def hash_to_params(myhash)
    return myhash.map { |k, v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}" }.join("&")
end

#kick_member(params = {}) ⇒ Object


379
380
381
382
383
# File 'lib/plivo.rb', line 379

def kick_member(params={})
    conference_name = params.delete('conference_name')
    member_id = params.delete('member_id')
    return request('POST', "/Conference/#{conference_name}/Member/#{member_id}/Kick/", params)
end

199
200
201
202
# File 'lib/plivo.rb', line 199

def link_application_number(params={})
    number = params.delete("number")
    return request('POST', "/Number/#{number}/", params)
end

#make_call(params = {}) ⇒ Object


245
246
247
# File 'lib/plivo.rb', line 245

def make_call(params={})
    return request('POST', "/Call/", params)
end

#modify_account(params = {}) ⇒ Object


110
111
112
# File 'lib/plivo.rb', line 110

def (params={})
    return request('POST', "/", params)
end

#modify_application(params = {}) ⇒ Object


151
152
153
154
# File 'lib/plivo.rb', line 151

def modify_application(params={})
    app_id = params.delete("app_id")
    return request('POST', "/Application/#{app_id}/", params)
end

#modify_endpoint(params = {}) ⇒ Object


424
425
426
427
# File 'lib/plivo.rb', line 424

def modify_endpoint(params={})
    endpoint_id = params.delete('endpoint_id')
    return request('POST', "/Endpoint/#{endpoint_id}/", params)
end

#modify_incoming_carrier(params = {}) ⇒ Object


448
449
450
451
# File 'lib/plivo.rb', line 448

def modify_incoming_carrier(params={})
    carrier_id = params.delete('carrier_id')
    return request('POST', "/IncomingCarrier/#{carrier_id}/", params)
end

#modify_number(params = {}) ⇒ Object


175
176
177
178
# File 'lib/plivo.rb', line 175

def modify_number(params={})
    number = params.delete("number")
    return request('POST', "/Number/#{number}/", params)
end

#modify_outgoing_carrier(params = {}) ⇒ Object


472
473
474
475
# File 'lib/plivo.rb', line 472

def modify_outgoing_carrier(params={})
    carrier_id = params.delete('carrier_id')
    return request('POST', "/OutgoingCarrier/#{carrier_id}/", params)
end

#modify_outgoing_carrier_routing(params = {}) ⇒ Object


496
497
498
499
# File 'lib/plivo.rb', line 496

def modify_outgoing_carrier_routing(params={})
    routing_id = params.delete('routing_id')
    return request('POST', "/OutgoingCarrierRouting/#{routing_id}/", params)
end

#modify_subaccount(params = {}) ⇒ Object


127
128
129
130
# File 'lib/plivo.rb', line 127

def modify_subaccount(params={})
    subauth_id = params.delete("subauth_id") || params.delete(:subauth_id)
    return request('POST', "/Subaccount/#{subauth_id}/", params)
end

#mute_member(params = {}) ⇒ Object


367
368
369
370
371
# File 'lib/plivo.rb', line 367

def mute_member(params={})
    conference_name = params.delete('conference_name')
    member_id = params.delete('member_id')
    return request('POST', "/Conference/#{conference_name}/Member/#{member_id}/Mute/", params)
end

#play(params = {}) ⇒ Object


273
274
275
276
# File 'lib/plivo.rb', line 273

def play(params={})
    call_uuid = params.delete('call_uuid')
    return request('POST', "/Call/#{call_uuid}/Play/", params)
end

#play_member(params = {}) ⇒ Object


330
331
332
333
334
# File 'lib/plivo.rb', line 330

def play_member(params={})
    conference_name = params.delete('conference_name')
    member_id = params.delete('member_id')
    return request('POST', "/Conference/#{conference_name}/Member/#{member_id}/Play/", params)
end

#pricing(params = {}) ⇒ Object

Pricing ##


507
508
509
# File 'lib/plivo.rb', line 507

def pricing(params={})
    return request('GET', "/Pricing/", params)
end

#record(params = {}) ⇒ Object


263
264
265
266
# File 'lib/plivo.rb', line 263

def record(params={})
    call_uuid = params.delete('call_uuid')
    return request('POST', "/Call/#{call_uuid}/Record/", params)
end

#record_conference(params = {}) ⇒ Object


385
386
387
388
# File 'lib/plivo.rb', line 385

def record_conference(params={})
    conference_name = params.delete('conference_name')
    return request('POST', "/Conference/#{conference_name}/Record/", params)
end

#rent_from_number_group(params = {}) ⇒ Object


219
220
221
222
# File 'lib/plivo.rb', line 219

def rent_from_number_group(params={})
    group_id = params.delete('group_id')
    return request('POST', "/AvailableNumberGroup/#{group_id}/", params)
end

#rent_number(params = {}) ⇒ Object


189
190
191
192
# File 'lib/plivo.rb', line 189

def rent_number(params={})
    number = params.delete("number")
    return request('POST', "/AvailableNumber/#{number}/", params)
end

#request(method, path, params = nil) ⇒ Object


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
# File 'lib/plivo.rb', line 47

def request(method, path, params=nil)
    if method == "POST"
        if not params
            params = {}
        end
        begin
            r = @rest[path].post params.to_json, :content_type => 'application/json'
        rescue => e
            response = e
        end
        if not response
            code = r.code
            raw = r.to_str
            response = JSON.parse(raw)
        else
            code = response.http_code
            response = JSON.parse(response.response.to_s)
        end
        return [code, response]
    elsif method == "GET"
        if params
            path = path + '?' + hash_to_params(params)
        end

        begin
            r = @rest[path].get
        rescue => e
            response = e
        end
        if not response
            code = r.code
            raw = r.to_str
            response = JSON.parse(raw)
        else
            code = response.http_code
            response = JSON.parse(response.response.to_s)
        end
        return [code, response]
    elsif method == "DELETE"
        if params
            path = path + '?' + hash_to_params(params)
        end
        begin
            r = @rest[path].delete
        rescue => e
            response = e
        end
        if not response
            code = r.code
        else
            code = response.http_code
            response = JSON.parse(response.response.to_s)
        end
        return [code, ""]
    end
    return [405, 'Method Not Supported']
end

#search_numbers(params = {}) ⇒ Object


180
181
182
# File 'lib/plivo.rb', line 180

def search_numbers(params={})
    return request('GET', "/AvailableNumber/", params)
end

#search_phone_number(params = {}) ⇒ Object

Numbers ##


162
163
164
# File 'lib/plivo.rb', line 162

def search_phone_number(params={})
    return request('GET', "/PhoneNumber/", params)
end

#send_digits(params = {}) ⇒ Object


294
295
296
297
# File 'lib/plivo.rb', line 294

def send_digits(params={})
    call_uuid = params.delete('call_uuid')
    return request('POST', "/Call/#{call_uuid}/DTMF/", params)
end

#send_message(params = {}) ⇒ Object

Message ##


516
517
518
# File 'lib/plivo.rb', line 516

def send_message(params={})
    return request('POST', "/Message/", params)
end

#speak(params = {}) ⇒ Object


283
284
285
286
287
# File 'lib/plivo.rb', line 283

def speak(params={})
    call_uuid = params.delete('call_uuid')
    params.update({"text" => HTMLEntities.new(:html4).encode(params['text'], :decimal)})
    return request('POST', "/Call/#{call_uuid}/Speak/", params)
end

#speak_member(params = {}) ⇒ Object


342
343
344
345
346
347
# File 'lib/plivo.rb', line 342

def speak_member(params={})
    conference_name = params.delete('conference_name')
    member_id = params.delete('member_id')
    params.update({"text" => HTMLEntities.new(:html4).encode(params['text'], :decimal)})
    return request('POST', "/Conference/#{conference_name}/Member/#{member_id}/Speak/", params)
end

#stop_play(params = {}) ⇒ Object


278
279
280
281
# File 'lib/plivo.rb', line 278

def stop_play(params={})
    call_uuid = params.delete('call_uuid')
    return request('DELETE', "/Call/#{call_uuid}/Play/", params)
end

#stop_play_member(params = {}) ⇒ Object


336
337
338
339
340
# File 'lib/plivo.rb', line 336

def stop_play_member(params={})
    conference_name = params.delete('conference_name')
    member_id = params.delete('member_id')
    return request('DELETE', "/Conference/#{conference_name}/Member/#{member_id}/Play/", params)
end

#stop_record(params = {}) ⇒ Object


268
269
270
271
# File 'lib/plivo.rb', line 268

def stop_record(params={})
    call_uuid = params.delete('call_uuid')
    return request('DELETE', "/Call/#{call_uuid}/Record/", params)
end

#stop_record_conference(params = {}) ⇒ Object


390
391
392
393
# File 'lib/plivo.rb', line 390

def stop_record_conference(params={})
    conference_name = params.delete('conference_name')
    return request('DELETE', "/Conference/#{conference_name}/Record/", params)
end

#stop_speak(params = {}) ⇒ Object


289
290
291
292
# File 'lib/plivo.rb', line 289

def stop_speak(params={})
    call_uuid = params.delete('call_uuid')
    return request('DELETE', "/Call/#{call_uuid}/Speak/", params)
end

#stop_speak_member(params = {}) ⇒ Object


349
350
351
352
353
# File 'lib/plivo.rb', line 349

def stop_speak_member(params={})
    conference_name = params.delete('conference_name')
    member_id = params.delete('member_id')
    return request('DELETE', "/Conference/#{conference_name}/Member/#{member_id}/Speak/", params)
end

#transfer_call(params = {}) ⇒ Object


253
254
255
256
# File 'lib/plivo.rb', line 253

def transfer_call(params={})
    call_uuid = params.delete('call_uuid')
    return request('POST', "/Call/#{call_uuid}/", params)
end

#undeaf_member(params = {}) ⇒ Object


361
362
363
364
365
# File 'lib/plivo.rb', line 361

def undeaf_member(params={})
    conference_name = params.delete('conference_name')
    member_id = params.delete('member_id')
    return request('DELETE', "/Conference/#{conference_name}/Member/#{member_id}/Deaf/", params)
end

204
205
206
207
208
# File 'lib/plivo.rb', line 204

def unlink_application_number(params={})
    number = params.delete("number")
    params = {"app_id" => ""}
    return request('POST', "/Number/#{number}/", params)
end

#unmute_member(params = {}) ⇒ Object


373
374
375
376
377
# File 'lib/plivo.rb', line 373

def unmute_member(params={})
    conference_name = params.delete('conference_name')
    member_id = params.delete('member_id')
    return request('DELETE', "/Conference/#{conference_name}/Member/#{member_id}/Mute/", params)
end

#unrent_number(params = {}) ⇒ Object


194
195
196
197
# File 'lib/plivo.rb', line 194

def unrent_number(params={})
    number = params.delete("number")
    return request('DELETE', "/Number/#{number}/", params)
end