plivohelper 0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/LICENSE +22 -0
- data/README.rst +69 -0
- data/Rakefile +27 -0
- data/lib/plivohelper.rb +604 -0
- metadata +58 -0
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2008 Plivo, Inc.
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person
|
4
|
+
obtaining a copy of this software and associated documentation
|
5
|
+
files (the "Software"), to deal in the Software without
|
6
|
+
restriction, including without limitation the rights to use,
|
7
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
8
|
+
copies of the Software, and to permit persons to whom the
|
9
|
+
Software is furnished to do so, subject to the following
|
10
|
+
conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be
|
13
|
+
included in all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
17
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
19
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
20
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
21
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rst
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
Plivo Ruby Helper Library
|
2
|
+
---------------------------
|
3
|
+
|
4
|
+
Description
|
5
|
+
~~~~~~~~~~~
|
6
|
+
|
7
|
+
The Plivo Ruby helper simplifies the process of making REST calls and generating RESTXML.
|
8
|
+
|
9
|
+
See `Plivo Documentation <http://www.plivo.org/docs/>`_ for more information.
|
10
|
+
|
11
|
+
|
12
|
+
Installation
|
13
|
+
~~~~~~~~~~~~~
|
14
|
+
|
15
|
+
**Gemcutter:**
|
16
|
+
$ sudo gem install plivohelper
|
17
|
+
|
18
|
+
|
19
|
+
Manual Installation
|
20
|
+
~~~~~~~~~~~~~~~~~~~~
|
21
|
+
|
22
|
+
To use the rake command to build the gem and
|
23
|
+
|
24
|
+
**Download the source and run:**
|
25
|
+
$ sudo gem install /path/to/plivohelper/gem
|
26
|
+
|
27
|
+
to finish the installation
|
28
|
+
|
29
|
+
|
30
|
+
Usage
|
31
|
+
~~~~~
|
32
|
+
|
33
|
+
To use the Plivo helper library, As shown in example-call.rb,
|
34
|
+
you will need to specify the ACCOUNT_ID and ACCOUNT_TOKEN, before you can make REST requests.
|
35
|
+
|
36
|
+
Before you run the examples, you should have Plivo Running along with FreeSWITCH Running and a user 1000 logged in.
|
37
|
+
|
38
|
+
See `Plivo Documentation <http://www.plivo.org/docs/>`_ for more information.
|
39
|
+
|
40
|
+
|
41
|
+
|
42
|
+
Files
|
43
|
+
~~~~~
|
44
|
+
|
45
|
+
**lib/plivohelper.rb:** include this library in your code
|
46
|
+
|
47
|
+
**examples/example-call.rb:** example usage of REST Call
|
48
|
+
|
49
|
+
**examples/example-bulkcalls.rb:** example usage of REST Bulk Calls
|
50
|
+
|
51
|
+
**examples/example-transfercall.rb:** example usage of REST Transfer Live Call
|
52
|
+
|
53
|
+
**examples/example-hangupcall.rb:** example usage of REST Hangup Live Call
|
54
|
+
|
55
|
+
**examples/example-xml.rb:** example usage of the RESTXML generator
|
56
|
+
|
57
|
+
**examples/example-utils.rb:** example usage of utilities
|
58
|
+
|
59
|
+
|
60
|
+
Credits
|
61
|
+
-------
|
62
|
+
|
63
|
+
Plivo Ruby Helper Library is derived from `Twilio Ruby Helper <https://github.com/twilio/twilio-ruby>`_
|
64
|
+
|
65
|
+
|
66
|
+
License
|
67
|
+
-------
|
68
|
+
|
69
|
+
The Plivo Ruby Helper Library is distributed under the MIT License
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake/gempackagetask'
|
3
|
+
|
4
|
+
spec = Gem::Specification.new do |s|
|
5
|
+
s.name = "plivohelper"
|
6
|
+
s.version = "0.1"
|
7
|
+
s.author = "Team Plivo"
|
8
|
+
s.email = "hello@plivo.org"
|
9
|
+
s.homepage = "http://www.plivo.org"
|
10
|
+
s.description = "A Ruby gem for communicating with the Plivo API and generating RESTXML"
|
11
|
+
s.platform = Gem::Platform::RUBY
|
12
|
+
s.summary = "A Ruby gem for communicating with the Plivo API and generating RESTXML"
|
13
|
+
s.files = FileList["{lib}/*"].to_a
|
14
|
+
s.require_path = "lib"
|
15
|
+
s.test_files = FileList["{test}/response_spec.rb"].to_a
|
16
|
+
s.has_rdoc = true
|
17
|
+
s.extra_rdoc_files = ["README.rst"]
|
18
|
+
s.add_dependency("builder", ">= 2.1.2")
|
19
|
+
end
|
20
|
+
|
21
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
22
|
+
pkg.need_tar = true
|
23
|
+
end
|
24
|
+
|
25
|
+
task :default => "pkg/#{spec.name}-#{spec.version}.gem" do
|
26
|
+
puts "generated latest version"
|
27
|
+
end
|
data/lib/plivohelper.rb
ADDED
@@ -0,0 +1,604 @@
|
|
1
|
+
|
2
|
+
# @author Plivo
|
3
|
+
module Plivo
|
4
|
+
require 'net/http'
|
5
|
+
require 'net/https'
|
6
|
+
require 'uri'
|
7
|
+
require 'cgi'
|
8
|
+
require 'rubygems'
|
9
|
+
require 'builder'
|
10
|
+
require 'openssl'
|
11
|
+
require 'base64'
|
12
|
+
|
13
|
+
|
14
|
+
# Plivo REST Helpers
|
15
|
+
class Rest
|
16
|
+
|
17
|
+
#@param [String, String] Your Plivo SID/ID and Auth Token
|
18
|
+
#@return [Object] Rest object
|
19
|
+
def initialize(url, id, token)
|
20
|
+
@id = id
|
21
|
+
@token = token
|
22
|
+
@url = url
|
23
|
+
end
|
24
|
+
|
25
|
+
#sends a request and gets a response from the Plivo REST API
|
26
|
+
#
|
27
|
+
#@param [String, String, Hash]
|
28
|
+
#path, the URL (relative to the endpoint URL, after the /v1
|
29
|
+
#method, the HTTP method to use, defaults to POST
|
30
|
+
#vars, for POST or PUT, a dict of data to send
|
31
|
+
#
|
32
|
+
#@return Plivo response XML
|
33
|
+
#@raises [ArgumentError] Invalid path parameter
|
34
|
+
#@raises [NotImplementedError] Method given is not implemented
|
35
|
+
def request(path, method=nil, vars={})
|
36
|
+
if !path || path.length < 1
|
37
|
+
raise ArgumentError, 'Invalid path parameter'
|
38
|
+
end
|
39
|
+
if method && !['GET', 'POST'].include?(method)
|
40
|
+
raise NotImplementedError, 'HTTP %s not implemented' % method
|
41
|
+
end
|
42
|
+
|
43
|
+
if path[0, 1] == '/'
|
44
|
+
uri = @url + path
|
45
|
+
else
|
46
|
+
uri = @url + '/' + path
|
47
|
+
end
|
48
|
+
|
49
|
+
return fetch(uri, vars, method)
|
50
|
+
end
|
51
|
+
|
52
|
+
# REST Reload Plivo Config Helper
|
53
|
+
def reload_config(call_params)
|
54
|
+
path = '/v0.1/ReloadConfig/'
|
55
|
+
method = 'POST'
|
56
|
+
return request(path, method, call_params)
|
57
|
+
end
|
58
|
+
|
59
|
+
# REST Reload Plivo Cache Config Helper
|
60
|
+
def reload_cache_config(call_params)
|
61
|
+
path = '/v0.1/ReloadCacheConfig/'
|
62
|
+
method = 'POST'
|
63
|
+
return request(path, method, call_params)
|
64
|
+
end
|
65
|
+
|
66
|
+
# REST Call Helper
|
67
|
+
def call(call_params)
|
68
|
+
path = '/v0.1/Call/'
|
69
|
+
method = 'POST'
|
70
|
+
return request(path, method, call_params)
|
71
|
+
end
|
72
|
+
|
73
|
+
# REST Bulk Call Helper
|
74
|
+
def bulk_call(call_params)
|
75
|
+
path = '/v0.1/BulkCall/'
|
76
|
+
method = 'POST'
|
77
|
+
return request(path, method, call_params)
|
78
|
+
end
|
79
|
+
|
80
|
+
# REST Group Call Helper
|
81
|
+
def group_call(call_params)
|
82
|
+
path = '/v0.1/GroupCall/'
|
83
|
+
method = 'POST'
|
84
|
+
return request(path, method, call_params)
|
85
|
+
end
|
86
|
+
|
87
|
+
# REST Transfer Live Call Helper
|
88
|
+
def transfer_call(call_params)
|
89
|
+
path = '/v0.1/TransferCall/'
|
90
|
+
method = 'POST'
|
91
|
+
return request(path, method, call_params)
|
92
|
+
end
|
93
|
+
|
94
|
+
# REST Hangup All Live Calls Helper
|
95
|
+
def hangup_all_calls()
|
96
|
+
path = '/v0.1/HangupAllCalls/'
|
97
|
+
method = 'POST'
|
98
|
+
return request(path, method)
|
99
|
+
end
|
100
|
+
|
101
|
+
# REST Hangup Live Call Helper
|
102
|
+
def hangup_call(call_params)
|
103
|
+
path = '/v0.1/HangupCall/'
|
104
|
+
method = 'POST'
|
105
|
+
return request(path, method, call_params)
|
106
|
+
end
|
107
|
+
|
108
|
+
# REST Schedule Hangup Helper
|
109
|
+
def schedule_hangup(call_params)
|
110
|
+
path = '/v0.1/ScheduleHangup/'
|
111
|
+
method = 'POST'
|
112
|
+
return request(path, method, call_params)
|
113
|
+
end
|
114
|
+
|
115
|
+
# REST Cancel a Scheduled Hangup Helper
|
116
|
+
def cancel_scheduled_hangup(call_params)
|
117
|
+
path = '/v0.1/CancelScheduledHangup/'
|
118
|
+
method = 'POST'
|
119
|
+
return request(path, method, call_params)
|
120
|
+
end
|
121
|
+
|
122
|
+
# REST RecordStart helper
|
123
|
+
def record_start(call_params)
|
124
|
+
path = '/v0.1/RecordStart/'
|
125
|
+
method = 'POST'
|
126
|
+
return request(path, method, call_params)
|
127
|
+
end
|
128
|
+
|
129
|
+
# REST RecordStop
|
130
|
+
def record_stop(call_params)
|
131
|
+
path = '/v0.1/RecordStop/'
|
132
|
+
method = 'POST'
|
133
|
+
return request(path, method, call_params)
|
134
|
+
end
|
135
|
+
|
136
|
+
# REST Play something on a Call Helper
|
137
|
+
def play(call_params)
|
138
|
+
path = '/v0.1/Play/'
|
139
|
+
method = 'POST'
|
140
|
+
return request(path, method, call_params)
|
141
|
+
end
|
142
|
+
|
143
|
+
# REST PlayStop on a Call Helper
|
144
|
+
def play_stop(call_params)
|
145
|
+
path = '/v0.1/PlayStop/'
|
146
|
+
method = 'POST'
|
147
|
+
return request(path, method, call_params)
|
148
|
+
end
|
149
|
+
|
150
|
+
# REST Schedule Play Helper
|
151
|
+
def schedule_play(call_params)
|
152
|
+
path = '/v0.1/SchedulePlay/'
|
153
|
+
method = 'POST'
|
154
|
+
return request(path, method, call_params)
|
155
|
+
end
|
156
|
+
|
157
|
+
# REST Cancel a Scheduled Play Helper
|
158
|
+
def cancel_scheduled_play(call_params)
|
159
|
+
path = '/v0.1/CancelScheduledPlay/'
|
160
|
+
method = 'POST'
|
161
|
+
return request(path, method, call_params)
|
162
|
+
end
|
163
|
+
|
164
|
+
# REST Add soundtouch audio effects to a Call Helper
|
165
|
+
def sound_touch(call_params)
|
166
|
+
path = '/v0.1/SoundTouch/'
|
167
|
+
method = 'POST'
|
168
|
+
return request(path, method, call_params)
|
169
|
+
end
|
170
|
+
|
171
|
+
# REST Remove soundtouch audio effects on a Call Helper
|
172
|
+
def sound_touch_stop(call_params)
|
173
|
+
path = '/v0.1/SoundTouchStop/'
|
174
|
+
method = 'POST'
|
175
|
+
return request(path, method, call_params)
|
176
|
+
end
|
177
|
+
|
178
|
+
# REST Send digits to a Call Helper
|
179
|
+
def send_digits(call_params)
|
180
|
+
path = '/v0.1/SendDigits/'
|
181
|
+
method = 'POST'
|
182
|
+
return request(path, method, call_params)
|
183
|
+
end
|
184
|
+
|
185
|
+
# REST Conference Mute helper
|
186
|
+
def conference_mute(call_params)
|
187
|
+
path = '/v0.1/ConferenceMute/'
|
188
|
+
method = 'POST'
|
189
|
+
return request(path, method, call_params)
|
190
|
+
end
|
191
|
+
|
192
|
+
# REST Conference Unmute helper
|
193
|
+
def conference_unmute(call_params)
|
194
|
+
path = '/v0.1/ConferenceUnmute/'
|
195
|
+
method = 'POST'
|
196
|
+
return request(path, method, call_params)
|
197
|
+
end
|
198
|
+
|
199
|
+
# REST Conference Kick helper
|
200
|
+
def conference_kick(call_params)
|
201
|
+
path = '/v0.1/ConferenceKick/'
|
202
|
+
method = 'POST'
|
203
|
+
return request(path, method, call_params)
|
204
|
+
end
|
205
|
+
|
206
|
+
# REST Conference Hangup helper
|
207
|
+
def conference_hangup(call_params)
|
208
|
+
path = '/v0.1/ConferenceHangup/'
|
209
|
+
method = 'POST'
|
210
|
+
return request(path, method, call_params)
|
211
|
+
end
|
212
|
+
|
213
|
+
# REST Conference Deaf helper
|
214
|
+
def conference_deaf(call_params)
|
215
|
+
path = '/v0.1/ConferenceDeaf/'
|
216
|
+
method = 'POST'
|
217
|
+
return request(path, method, call_params)
|
218
|
+
end
|
219
|
+
|
220
|
+
# REST Conference Undeaf helper
|
221
|
+
def conference_undeaf(call_params)
|
222
|
+
path = '/v0.1/ConferenceUndeaf/'
|
223
|
+
method = 'POST'
|
224
|
+
return request(path, method, call_params)
|
225
|
+
end
|
226
|
+
|
227
|
+
# REST Conference RecordStart helper
|
228
|
+
def conference_record_start(call_params)
|
229
|
+
path = '/v0.1/ConferenceRecordStart/'
|
230
|
+
method = 'POST'
|
231
|
+
return request(path, method, call_params)
|
232
|
+
end
|
233
|
+
|
234
|
+
# REST Conference RecordStop
|
235
|
+
def conference_record_stop(call_params)
|
236
|
+
path = '/v0.1/ConferenceRecordStop/'
|
237
|
+
method = 'POST'
|
238
|
+
return request(path, method, call_params)
|
239
|
+
end
|
240
|
+
|
241
|
+
# REST Conference Play helper
|
242
|
+
def conference_play(call_params)
|
243
|
+
path = '/v0.1/ConferencePlay/'
|
244
|
+
method = 'POST'
|
245
|
+
return request(path, method, call_params)
|
246
|
+
end
|
247
|
+
|
248
|
+
# REST Conference Speak helper
|
249
|
+
def conference_speak(call_params)
|
250
|
+
path = '/v0.1/ConferenceSpeak/'
|
251
|
+
method = 'POST'
|
252
|
+
return request(path, method, call_params)
|
253
|
+
end
|
254
|
+
|
255
|
+
# REST Conference List Helper
|
256
|
+
def conference_list(call_params)
|
257
|
+
path = '/v0.1/ConferenceList/'
|
258
|
+
method = 'POST'
|
259
|
+
return request(path, method, call_params)
|
260
|
+
end
|
261
|
+
|
262
|
+
# REST Conference List Members Helper
|
263
|
+
def conference_list_members(call_params)
|
264
|
+
path = '/v0.1/ConferenceListMembers/'
|
265
|
+
method = 'POST'
|
266
|
+
return request(path, method, call_params)
|
267
|
+
end
|
268
|
+
|
269
|
+
|
270
|
+
#encode the parameters into a URL friendly string
|
271
|
+
#
|
272
|
+
#@param [Hash] URL key / values
|
273
|
+
#@return [String] Encoded URL
|
274
|
+
protected
|
275
|
+
def urlencode(params)
|
276
|
+
params.to_a.collect! \
|
277
|
+
{ |k, v| "#{k}=#{CGI.escape(v.to_s)}" }.join("&")
|
278
|
+
end
|
279
|
+
|
280
|
+
# Create the uri for the REST call
|
281
|
+
#
|
282
|
+
#@param [String, Hash] Base URL and URL parameters
|
283
|
+
#@return [String] URI for the REST call
|
284
|
+
def build_get_uri(uri, params)
|
285
|
+
if params && params.length > 0
|
286
|
+
if uri.include?('?')
|
287
|
+
if uri[-1, 1] != '&'
|
288
|
+
uri += '&'
|
289
|
+
end
|
290
|
+
uri += urlencode(params)
|
291
|
+
else
|
292
|
+
uri += '?' + urlencode(params)
|
293
|
+
end
|
294
|
+
end
|
295
|
+
return uri
|
296
|
+
end
|
297
|
+
|
298
|
+
# Returns a http request for the given url and parameters
|
299
|
+
#
|
300
|
+
#@param [String, Hash, String] Base URL, URL parameters, optional METHOD
|
301
|
+
#@return [String] URI for the REST call
|
302
|
+
def fetch(url, params, method=nil)
|
303
|
+
if method && method == 'GET'
|
304
|
+
url = build_get_uri(url, params)
|
305
|
+
end
|
306
|
+
uri = URI.parse(url)
|
307
|
+
|
308
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
309
|
+
#http.use_ssl = true
|
310
|
+
|
311
|
+
if method && method == 'GET'
|
312
|
+
req = Net::HTTP::Get.new(uri.request_uri)
|
313
|
+
elsif method && method == 'DELETE'
|
314
|
+
req = Net::HTTP::Delete.new(uri.request_uri)
|
315
|
+
elsif method && method == 'PUT'
|
316
|
+
req = Net::HTTP::Put.new(uri.request_uri)
|
317
|
+
req.set_form_data(params)
|
318
|
+
else
|
319
|
+
req = Net::HTTP::Post.new(uri.request_uri)
|
320
|
+
req.set_form_data(params)
|
321
|
+
end
|
322
|
+
req.basic_auth(@id, @token)
|
323
|
+
|
324
|
+
return http.request(req)
|
325
|
+
end
|
326
|
+
end
|
327
|
+
|
328
|
+
# RESTXML Response Helpers
|
329
|
+
module Element
|
330
|
+
module ClassMethods
|
331
|
+
@attributes = []
|
332
|
+
@allowed_element = []
|
333
|
+
attr_accessor :attributes
|
334
|
+
|
335
|
+
def allowed_element(*element)
|
336
|
+
return @allowed_element if element == []
|
337
|
+
@allowed_element = [] if @allowed_element.nil?
|
338
|
+
element.each do |element_element|
|
339
|
+
cleaned_element_element = element_element.to_s.slice(0,1).capitalize + element_element.to_s.slice(1..-1)
|
340
|
+
@allowed_element << cleaned_element_element
|
341
|
+
end
|
342
|
+
@allowed_element = @allowed_element.uniq
|
343
|
+
end
|
344
|
+
|
345
|
+
def attributes(*attrs)
|
346
|
+
return @attributes if attrs == []
|
347
|
+
@attributes = [] if @attributes.nil?
|
348
|
+
@attributes = (@attributes + attrs).uniq
|
349
|
+
attr_accessor(*@attributes)
|
350
|
+
@attributes
|
351
|
+
end
|
352
|
+
end
|
353
|
+
|
354
|
+
def attributes
|
355
|
+
self.class.attributes
|
356
|
+
end
|
357
|
+
|
358
|
+
#test if a given element element is allowed to be nested
|
359
|
+
#
|
360
|
+
#@param [Object] Element to be appended
|
361
|
+
#@return [true, false]
|
362
|
+
def allowed?(element_element)
|
363
|
+
self.class.allowed_element.nil? ? false : self.class.allowed_element.include?(element_element.class.name.split('::')[1])
|
364
|
+
end
|
365
|
+
|
366
|
+
#initialize a plivo response object
|
367
|
+
#
|
368
|
+
#@param [String, Hash] Body of the element, and a hash of the attributes
|
369
|
+
#@return [Object] Plivo element object
|
370
|
+
#
|
371
|
+
#@raises [ArgumentError] Invalid Argument
|
372
|
+
def initialize(body = nil, params = {})
|
373
|
+
@children = []
|
374
|
+
if body.class == String
|
375
|
+
@body = body
|
376
|
+
else
|
377
|
+
@body = nil
|
378
|
+
params = body || {}
|
379
|
+
end
|
380
|
+
params.each do |k,v|
|
381
|
+
if !self.class.attributes.nil? && self.class.attributes.include?(k)
|
382
|
+
send(k.to_s+"=",v)
|
383
|
+
else
|
384
|
+
raise ArgumentError, "Attribute Not Supported"
|
385
|
+
end
|
386
|
+
end
|
387
|
+
end
|
388
|
+
|
389
|
+
#set an attribute key / value
|
390
|
+
#no error checking
|
391
|
+
#
|
392
|
+
#@param [Hash] Hash of options
|
393
|
+
#@return void
|
394
|
+
def set(params = {})
|
395
|
+
params.each do |k,v|
|
396
|
+
self.class.attributes k.to_s
|
397
|
+
send(k.to_s+"=",v)
|
398
|
+
end
|
399
|
+
end
|
400
|
+
|
401
|
+
#output valid Plivo markup
|
402
|
+
#
|
403
|
+
#@param [Hash] Hash of options
|
404
|
+
#@return [String] Plivo Markup (in XML)
|
405
|
+
def respond(opts = {})
|
406
|
+
opts[:builder] ||= Builder::XmlMarkup.new(:indent => opts[:indent])
|
407
|
+
b = opts[:builder]
|
408
|
+
attrs = {}
|
409
|
+
attributes.each {|a| attrs[a] = send(a) unless send(a).nil? } unless attributes.nil?
|
410
|
+
|
411
|
+
if @children and @body.nil?
|
412
|
+
b.__send__(self.class.to_s.split(/::/)[-1], attrs) do
|
413
|
+
@children.each {|e|e.respond( opts.merge(:skip_instruct => true) )}
|
414
|
+
end
|
415
|
+
elsif @body and @children == []
|
416
|
+
b.__send__(self.class.to_s.split(/::/)[-1], @body, attrs)
|
417
|
+
else
|
418
|
+
raise ArgumentError, "Cannot have children and a body at the same time"
|
419
|
+
end
|
420
|
+
end
|
421
|
+
|
422
|
+
#output valid Plivo markup encoded for inclusion in a URL
|
423
|
+
#
|
424
|
+
#@param []
|
425
|
+
#@return [String] URL encoded Plivo Markup (XML)
|
426
|
+
def asURL()
|
427
|
+
CGI::escape(self.respond)
|
428
|
+
end
|
429
|
+
|
430
|
+
def append(element_element)
|
431
|
+
if(allowed?(element_element))
|
432
|
+
@children << element_element
|
433
|
+
@children[-1]
|
434
|
+
else
|
435
|
+
raise ArgumentError, "Element Not Supported"
|
436
|
+
end
|
437
|
+
end
|
438
|
+
|
439
|
+
# Element Convenience Methods
|
440
|
+
def addSpeak(string_to_speak = nil, opts = {})
|
441
|
+
append Plivo::Speak.new(string_to_speak, opts)
|
442
|
+
end
|
443
|
+
|
444
|
+
def addPlay(file_to_play = nil, opts = {})
|
445
|
+
append Plivo::Play.new(file_to_play, opts)
|
446
|
+
end
|
447
|
+
|
448
|
+
def addGetDigits(opts = {})
|
449
|
+
append Plivo::GetDigits.new(opts)
|
450
|
+
end
|
451
|
+
|
452
|
+
def addGetSpeech(opts = {})
|
453
|
+
append Plivo::GetSpeech.new(opts)
|
454
|
+
end
|
455
|
+
|
456
|
+
def addRecord(opts = {})
|
457
|
+
append Plivo::Record.new(opts)
|
458
|
+
end
|
459
|
+
|
460
|
+
def addDial(number = nil, opts = {})
|
461
|
+
append Plivo::Dial.new(number, opts)
|
462
|
+
end
|
463
|
+
|
464
|
+
def addRedirect(url = nil, opts = {})
|
465
|
+
append Plivo::Redirect.new(url, opts)
|
466
|
+
end
|
467
|
+
|
468
|
+
def addSIPTransfer(url = nil, opts = {})
|
469
|
+
append Plivo::SIPTransfer.new(url, opts)
|
470
|
+
end
|
471
|
+
|
472
|
+
def addWait(opts = {})
|
473
|
+
append Plivo::Wait.new(opts)
|
474
|
+
end
|
475
|
+
|
476
|
+
def addHangup(opts = {})
|
477
|
+
append Plivo::Hangup.new(opts)
|
478
|
+
end
|
479
|
+
|
480
|
+
def addNumber(number, opts = {})
|
481
|
+
append Plivo::Number.new(number, opts)
|
482
|
+
end
|
483
|
+
|
484
|
+
def addConference(room, opts = {})
|
485
|
+
append Plivo::Conference.new(room, opts)
|
486
|
+
end
|
487
|
+
|
488
|
+
def addPreAnswer(opts = {})
|
489
|
+
append Plivo::PreAnswer.new(opts)
|
490
|
+
end
|
491
|
+
|
492
|
+
end
|
493
|
+
|
494
|
+
class Speak
|
495
|
+
extend Plivo::Element::ClassMethods
|
496
|
+
include Plivo::Element
|
497
|
+
attributes :voice, :language, :loop, :engine, :type, :method
|
498
|
+
end
|
499
|
+
|
500
|
+
class Play
|
501
|
+
extend Plivo::Element::ClassMethods
|
502
|
+
include Plivo::Element
|
503
|
+
attributes :loop
|
504
|
+
end
|
505
|
+
|
506
|
+
class GetDigits
|
507
|
+
extend Plivo::Element::ClassMethods
|
508
|
+
include Plivo::Element
|
509
|
+
attributes :action, :method, :timeout, :finishOnKey, :numDigits, :retries, :playBeep, :validDigits, :invalidDigitsSound
|
510
|
+
allowed_element :play, :speak, :wait
|
511
|
+
end
|
512
|
+
|
513
|
+
class GetSpeech
|
514
|
+
extend Plivo::Element::ClassMethods
|
515
|
+
include Plivo::Element
|
516
|
+
attributes :action, :method, :timeout, :playBeep, :engine, :grammar, :grammarPath
|
517
|
+
allowed_element :play, :speak, :wait
|
518
|
+
end
|
519
|
+
|
520
|
+
class Record
|
521
|
+
extend Plivo::Element::ClassMethods
|
522
|
+
include Plivo::Element
|
523
|
+
attributes :action, :method, :timeout, :finishOnKey, :maxLength, :playBeep, :fileFormat, :fileName, :filePath, :bothLegs, :record
|
524
|
+
end
|
525
|
+
|
526
|
+
class Dial
|
527
|
+
extend Plivo::Element::ClassMethods
|
528
|
+
include Plivo::Element
|
529
|
+
attributes :action, :method, :timeout, :hangupOnStar, :timeLimit, :callerId, :callerName, :confirmSound, :confirmKey, :dialMusic, :redirect, :callbackUrl, :callbackMethod, :digitsMatch
|
530
|
+
allowed_element :number
|
531
|
+
end
|
532
|
+
|
533
|
+
class Redirect
|
534
|
+
extend Plivo::Element::ClassMethods
|
535
|
+
include Plivo::Element
|
536
|
+
end
|
537
|
+
|
538
|
+
class SIPTransfer
|
539
|
+
extend Plivo::Element::ClassMethods
|
540
|
+
include Plivo::Element
|
541
|
+
attributes :method
|
542
|
+
end
|
543
|
+
|
544
|
+
class Wait
|
545
|
+
extend Plivo::Element::ClassMethods
|
546
|
+
include Plivo::Element
|
547
|
+
attributes :length
|
548
|
+
end
|
549
|
+
|
550
|
+
class Hangup
|
551
|
+
extend Plivo::Element::ClassMethods
|
552
|
+
include Plivo::Element
|
553
|
+
attributes :reason, :schedule
|
554
|
+
end
|
555
|
+
|
556
|
+
class Number
|
557
|
+
extend Plivo::Element::ClassMethods
|
558
|
+
include Plivo::Element
|
559
|
+
attributes :sendDigits, :sendOnPreanswer, :gateways, :gatewayCodecs, :gatewayTimeouts, :gatewayRetries, :extraDialString
|
560
|
+
end
|
561
|
+
|
562
|
+
class Conference
|
563
|
+
extend Plivo::Element::ClassMethods
|
564
|
+
include Plivo::Element
|
565
|
+
attributes :muted, :enterSound, :exitSound, :startConferenceOnEnter, :endConferenceOnExit, :waitSound, :timeLimit, :hangupOnStar, :recordFilePath, :recordFileFormat, :recordFileName, :action, :method, :callbackUrl, :callbackMethod, :digitsMatch, :stayAlone, :floorEvent
|
566
|
+
end
|
567
|
+
|
568
|
+
class PreAnswer
|
569
|
+
extend Plivo::Element::ClassMethods
|
570
|
+
include Plivo::Element
|
571
|
+
allowed_element :Speak, :Play, :GetDigits, :Wait, :GetSpeech, :SIPTransfer, :Redirect
|
572
|
+
end
|
573
|
+
|
574
|
+
class Response
|
575
|
+
extend Plivo::Element::ClassMethods
|
576
|
+
include Plivo::Element
|
577
|
+
allowed_element :Speak, :Play, :GetDigits, :Record, :Dial, :Redirect, :Wait, :Hangup, :PreAnswer, :Conference, :GetSpeech, :SIPTransfer
|
578
|
+
end
|
579
|
+
|
580
|
+
# Plivo Utility function and Request Validation class
|
581
|
+
class Utils
|
582
|
+
|
583
|
+
#initialize a plivo utils abject
|
584
|
+
#
|
585
|
+
#@param [String, String] Your Plivo SID/ID and Auth Token
|
586
|
+
#@return [Object] Utils object
|
587
|
+
def initialize(id, token)
|
588
|
+
@id = id
|
589
|
+
@token = token
|
590
|
+
end
|
591
|
+
|
592
|
+
def validateRequest(signature, url, params = {})
|
593
|
+
sorted_post_params = params.sort
|
594
|
+
data = url
|
595
|
+
sorted_post_params.each do |pkey|
|
596
|
+
data = data + pkey[0]+pkey[1]
|
597
|
+
end
|
598
|
+
digest = OpenSSL::Digest::Digest.new('sha1')
|
599
|
+
expected = Base64.encode64(OpenSSL::HMAC.digest(digest, @token, data)).strip
|
600
|
+
return expected == signature
|
601
|
+
end
|
602
|
+
end
|
603
|
+
|
604
|
+
end
|
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: plivohelper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: "0.1"
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Plivo
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2012-01-19 00:00:00 Z
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Used to interface with Plivo Telephony Solution from Ruby
|
17
|
+
email:
|
18
|
+
- support@plivo.com
|
19
|
+
executables: []
|
20
|
+
|
21
|
+
extensions: []
|
22
|
+
|
23
|
+
extra_rdoc_files: []
|
24
|
+
|
25
|
+
files:
|
26
|
+
- lib/plivohelper.rb
|
27
|
+
- LICENSE
|
28
|
+
- Rakefile
|
29
|
+
- README.rst
|
30
|
+
homepage: http://github.com/plivo/plivohelper
|
31
|
+
licenses: []
|
32
|
+
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options: []
|
35
|
+
|
36
|
+
require_paths:
|
37
|
+
- lib
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: "0"
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: "0"
|
50
|
+
requirements: []
|
51
|
+
|
52
|
+
rubyforge_project:
|
53
|
+
rubygems_version: 1.8.11
|
54
|
+
signing_key:
|
55
|
+
specification_version: 3
|
56
|
+
summary: Plivo Helper Libraries for Ruby
|
57
|
+
test_files: []
|
58
|
+
|