plivo 0.3.19 → 4.0.0.beta.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +14 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +10 -0
  5. data/AUTHORS.md +4 -0
  6. data/CHANGELOG.md +19 -0
  7. data/Gemfile +3 -0
  8. data/LICENSE.txt +19 -0
  9. data/README.md +105 -24
  10. data/Rakefile +7 -0
  11. data/lib/plivo.rb +9 -815
  12. data/lib/plivo/base.rb +9 -0
  13. data/lib/plivo/base/resource.rb +85 -0
  14. data/lib/plivo/base/resource_interface.rb +93 -0
  15. data/lib/plivo/base/response.rb +29 -0
  16. data/lib/plivo/exceptions.rb +50 -0
  17. data/lib/plivo/resources.rb +14 -0
  18. data/lib/plivo/resources/accounts.rb +174 -0
  19. data/lib/plivo/resources/applications.rb +233 -0
  20. data/lib/plivo/resources/calls.rb +492 -0
  21. data/lib/plivo/resources/conferences.rb +371 -0
  22. data/lib/plivo/resources/endpoints.rb +130 -0
  23. data/lib/plivo/resources/messages.rb +178 -0
  24. data/lib/plivo/resources/numbers.rb +302 -0
  25. data/lib/plivo/resources/pricings.rb +43 -0
  26. data/lib/plivo/resources/recordings.rb +114 -0
  27. data/lib/plivo/rest_client.rb +199 -0
  28. data/lib/plivo/utils.rb +107 -0
  29. data/lib/plivo/version.rb +3 -0
  30. data/lib/plivo/xml.rb +27 -0
  31. data/lib/plivo/xml/conference.rb +20 -0
  32. data/lib/plivo/xml/dial.rb +16 -0
  33. data/lib/plivo/xml/dtmf.rb +13 -0
  34. data/lib/plivo/xml/element.rb +83 -0
  35. data/lib/plivo/xml/get_digits.rb +15 -0
  36. data/lib/plivo/xml/hangup.rb +12 -0
  37. data/lib/plivo/xml/message.rb +13 -0
  38. data/lib/plivo/xml/number.rb +13 -0
  39. data/lib/plivo/xml/play.rb +13 -0
  40. data/lib/plivo/xml/plivo_xml.rb +19 -0
  41. data/lib/plivo/xml/pre_answer.rb +12 -0
  42. data/lib/plivo/xml/record.rb +17 -0
  43. data/lib/plivo/xml/redirect.rb +13 -0
  44. data/lib/plivo/xml/response.rb +21 -0
  45. data/lib/plivo/xml/speak.rb +17 -0
  46. data/lib/plivo/xml/user.rb +13 -0
  47. data/lib/plivo/xml/wait.rb +12 -0
  48. data/plivo.gemspec +44 -0
  49. metadata +134 -45
  50. data/ext/mkrf_conf.rb +0 -9
@@ -0,0 +1,233 @@
1
+ module Plivo
2
+ module Resources
3
+ include Plivo::Utils
4
+ class Application < Base::Resource
5
+
6
+
7
+ def initialize(client, options = nil)
8
+ @_name = 'Application'
9
+ @_identifier_string = 'app_id'
10
+ super
11
+ end
12
+
13
+ # @param [Hash] options
14
+ # @option options [String] :answer_url - The URL invoked by Plivo when a call executes this application.
15
+ # @option options [String] :answer_method - The method used to call the answer_url. Defaults to POST.
16
+ # @option options [String] :hangup_url - The URL that will be notified by Plivo when the call hangs up. Defaults to answer_url.
17
+ # @option options [String] :hangup_method - The method used to call the hangup_url. Defaults to POST.
18
+ # @option options [String] :fallback_answer_url - Invoked by Plivo only if answer_url is unavailable or the XML response is invalid. Should contain a XML response.
19
+ # @option options [String] :fallback_method - The method used to call the fallback_answer_url. Defaults to POST.
20
+ # @option options [String] :message_url - The URL that will be notified by Plivo when an inbound message is received. Defaults not set.
21
+ # @option options [String] :message_method - The method used to call the message_url. Defaults to POST.
22
+ # @option options [Boolean] :default_number_app - If set to true, this parameter ensures that newly created numbers, which don't have an app_id, point to this application.
23
+ # @option options [Boolean] :default_endpoint_app - If set to true, this parameter ensures that newly created endpoints, which don't have an app_id, point to this application.
24
+ # @option options [String] :subaccount - Id of the subaccount, in case only subaccount applications are needed.
25
+ # @return [Application] Application
26
+ def update(options = nil)
27
+ return perform_update({}) if options.nil?
28
+
29
+ valid_param?(:options, options, Hash, true)
30
+
31
+ params = {}
32
+
33
+ %i[answer_url hangup_url fallback_answer_url message_url subaccount]
34
+ .each do |param|
35
+ if options.key?(param) &&
36
+ valid_param?(param, options[param], [String, Symbol], true)
37
+ params[param] = options[param]
38
+ end
39
+ end
40
+
41
+ %i[answer_method hangup_method fallback_method message_method]
42
+ .each do |param|
43
+ if options.key?(param) &&
44
+ valid_param?(param, options[param], [String, Symbol], true, %w[GET POST])
45
+ params[param] = options[param]
46
+ end
47
+ end
48
+
49
+ %i[default_number_app default_endpoint_app].each do |param|
50
+ if options.key?(param) &&
51
+ valid_param?(param, options[param], [TrueClass, FalseClass], true)
52
+ params[param] = options[param]
53
+ end
54
+ end
55
+
56
+ perform_update(params)
57
+ end
58
+
59
+ def delete
60
+ perform_delete
61
+ end
62
+
63
+ def to_s
64
+ {
65
+ answer_method: @answer_method,
66
+ answer_url: @answer_url,
67
+ app_id: @app_id,
68
+ api_id: @api_id,
69
+ app_name: @app_name,
70
+ default_app: @default_app,
71
+ default_endpoint_app: @default_endpoint_app,
72
+ enabled: @enabled,
73
+ fallback_answer_url: @fallback_answer_url,
74
+ fallback_method: @fallback_method,
75
+ hangup_method: @hangup_method,
76
+ hangup_url: @hangup_url,
77
+ message_method: @message_method,
78
+ message_url: @message_url,
79
+ public_uri: @public_uri,
80
+ resource_uri: @resource_uri,
81
+ sip_uri: @sip_uri,
82
+ sub_account: @sub_account
83
+ }.to_s
84
+ end
85
+ end
86
+
87
+ # @!method get
88
+ # @!method create
89
+ # @!method list
90
+ class ApplicationInterface < Base::ResourceInterface
91
+ def initialize(client, resource_list_json = nil)
92
+ @_name = 'Application'
93
+ @_resource_type = Application
94
+ @_identifier_string = 'app_id'
95
+ super
96
+ end
97
+
98
+ # @param [String] app_id
99
+ # @return [Application] Application
100
+ def get(app_id)
101
+ valid_param?(:app_id, app_id, [String, Symbol], true)
102
+ perform_get(app_id)
103
+ end
104
+
105
+ # @param [String] app_name
106
+ # @param [Hash] options
107
+ # @option options [String] :answer_url - The URL invoked by Plivo when a call executes this application.
108
+ # @option options [String] :answer_method - The method used to call the answer_url. Defaults to POST.
109
+ # @option options [String] :hangup_url - The URL that will be notified by Plivo when the call hangs up. Defaults to answer_url.
110
+ # @option options [String] :hangup_method - The method used to call the hangup_url. Defaults to POST.
111
+ # @option options [String] :fallback_answer_url - Invoked by Plivo only if answer_url is unavailable or the XML response is invalid. Should contain a XML response.
112
+ # @option options [String] :fallback_method - The method used to call the fallback_answer_url. Defaults to POST.
113
+ # @option options [String] :message_url - The URL that will be notified by Plivo when an inbound message is received. Defaults not set.
114
+ # @option options [String] :message_method - The method used to call the message_url. Defaults to POST.
115
+ # @option options [Boolean] :default_number_app - If set to true, this parameter ensures that newly created numbers, which don't have an app_id, point to this application.
116
+ # @option options [Boolean] :default_endpoint_app - If set to true, this parameter ensures that newly created endpoints, which don't have an app_id, point to this application.
117
+ # @option options [String] :subaccount - Id of the subaccount, in case only subaccount applications are needed.
118
+ # @return [Application] Application
119
+ def create(app_name, options = nil)
120
+ valid_param?(:app_name, app_name, [String, Symbol], true)
121
+ valid_param?(:options, options, Hash, true) unless options.nil?
122
+
123
+ params = {
124
+ app_name: app_name
125
+ }
126
+
127
+ return perform_create(params) if options.nil?
128
+
129
+ %i[answer_url hangup_url fallback_answer_url message_url subaccount]
130
+ .each do |param|
131
+ if options.key?(param) &&
132
+ valid_param?(param, options[param], [String, Symbol], true)
133
+ params[param] = options[param]
134
+ end
135
+ end
136
+
137
+ %i[answer_method hangup_method fallback_method message_method]
138
+ .each do |param|
139
+ if options.key?(param) &&
140
+ valid_param?(param, options[param], [String, Symbol], true, %w[GET POST])
141
+ params[param] = options[param]
142
+ end
143
+ end
144
+
145
+ %i[default_number_app default_endpoint_app].each do |param|
146
+ if options.key?(param) &&
147
+ valid_param?(param, options[param], [TrueClass, FalseClass], true)
148
+ params[param] = options[param]
149
+ end
150
+ end
151
+
152
+ perform_create(params)
153
+ end
154
+
155
+ ##
156
+ # Lists all applications
157
+ # @param [Hash] options
158
+ # @option options [String] :subaccount
159
+ # @option options [Int] :offset
160
+ # @option options [Int] :limit
161
+ # @return [Hash]
162
+ def list(options = nil)
163
+ return perform_list if options.nil?
164
+
165
+ params = {}
166
+
167
+ if options.key?(:subaccount) &&
168
+ valid_param?(:subaccount, options[:subaccount], [String, Symbol], true)
169
+ params[:subaccount] = options[:subaccount]
170
+ end
171
+
172
+ %i[offset limit].each do |param|
173
+ if options.key?(param) && valid_param?(param, options[param],
174
+ [Integer, Integer], true)
175
+ params[param] = options[param]
176
+ end
177
+ end
178
+
179
+ if options.key?(:limit) && (options[:limit] > 20 || options[:limit] <= 0)
180
+ raise_invalid_request('The maximum number of results that can be '\
181
+ "fetched is 20. limit can't be more than 20 or less than 1")
182
+ end
183
+
184
+ if options.key?(:offset) && options[:offset] < 0
185
+ raise_invalid_request("Offset can't be negative")
186
+ end
187
+
188
+ perform_list(params)
189
+ end
190
+
191
+ def each
192
+ offset = 0
193
+ loop do
194
+ app_list = list(offset: offset)
195
+ app_list[:objects].each { |app| yield app }
196
+ offset += 20
197
+ return unless app_list.length == 20
198
+ end
199
+ end
200
+
201
+ ##
202
+ # Modify an application
203
+ # @param [String] app_id
204
+ # @param [Hash] options
205
+ # @option options [String] :answer_url - The URL invoked by Plivo when a call executes this application.
206
+ # @option options [String] :answer_method - The method used to call the answer_url. Defaults to POST.
207
+ # @option options [String] :hangup_url - The URL that will be notified by Plivo when the call hangs up. Defaults to answer_url.
208
+ # @option options [String] :hangup_method - The method used to call the hangup_url. Defaults to POST.
209
+ # @option options [String] :fallback_answer_url - Invoked by Plivo only if answer_url is unavailable or the XML response is invalid. Should contain a XML response.
210
+ # @option options [String] :fallback_method - The method used to call the fallback_answer_url. Defaults to POST.
211
+ # @option options [String] :message_url - The URL that will be notified by Plivo when an inbound message is received. Defaults not set.
212
+ # @option options [String] :message_method - The method used to call the message_url. Defaults to POST.
213
+ # @option options [Boolean] :default_number_app - If set to true, this parameter ensures that newly created numbers, which don't have an app_id, point to this application.
214
+ # @option options [Boolean] :default_endpoint_app - If set to true, this parameter ensures that newly created endpoints, which don't have an app_id, point to this application.
215
+ # @option options [String] :subaccount - Id of the subaccount, in case only subaccount applications are needed.
216
+ # @return [Application] Application
217
+ def update(app_id, options = nil)
218
+ valid_param?(:app_id, app_id, [String, Symbol], true)
219
+ Application.new(@_client,
220
+ resource_id: app_id).update(options)
221
+ end
222
+
223
+ ##
224
+ # Delete an application
225
+ # @param [String] app_id
226
+ def delete(app_id)
227
+ valid_param?(:app_id, app_id, [String, Symbol], true)
228
+ Application.new(@_client,
229
+ resource_id: app_id).delete
230
+ end
231
+ end
232
+ end
233
+ end
@@ -0,0 +1,492 @@
1
+ module Plivo
2
+ module Resources
3
+ include Plivo::Utils
4
+ class Call < Base::Resource
5
+ def initialize(client, options = nil)
6
+ @_name = 'Call'
7
+ @_identifier_string = 'call_uuid'
8
+ super
9
+ end
10
+
11
+ def update(options)
12
+ valid_param?(:options, options, Hash, true)
13
+
14
+ params = {}
15
+
16
+ if options.key?(:legs) &&
17
+ valid_param?(:legs, options[:legs],
18
+ [String, Symbol], true, %w[aleg bleg both])
19
+ params[:legs] = options[:legs]
20
+ end
21
+
22
+ unless options.key?(:legs)
23
+ unless options.key?(:aleg_url)
24
+ raise_invalid_request('default leg is aleg, aleg_url has to be specified')
25
+ end
26
+ params[:aleg_url] = options[:aleg_url]
27
+ end
28
+
29
+ if options[:legs] == 'aleg'
30
+ unless options.key?(:aleg_url)
31
+ raise_invalid_request('leg is aleg, aleg_url has to be specified')
32
+ end
33
+ params[:aleg_url] = options[:aleg_url]
34
+ end
35
+
36
+ if options[:legs] == 'bleg'
37
+ unless options.key?(:bleg_url)
38
+ raise_invalid_request('leg is bleg, bleg_url has to be specified')
39
+ end
40
+ params[:bleg_url] = options[:bleg_url]
41
+ end
42
+
43
+ if options[:legs] == 'both'
44
+ unless options.key?(:aleg_url) && options.key?(:bleg_url)
45
+ raise_invalid_request('leg is both, aleg_url & bleg_url have to be specified')
46
+ end
47
+ params[:aleg_url] = options[:aleg_url]
48
+ params[:bleg_url] = options[:bleg_url]
49
+ end
50
+
51
+ %i[aleg_method bleg_method].each do |param|
52
+ if options.key?(param) &&
53
+ valid_param?(param, options[param], [String, Symbol], true, %w[GET POST])
54
+ params[param] = options[param]
55
+ end
56
+ end
57
+
58
+ perform_update(params)
59
+ end
60
+
61
+ def delete
62
+ perform_delete
63
+ end
64
+
65
+ def record(options = nil)
66
+ return perform_action('Record', 'POST', nil, true) if options.nil?
67
+ valid_param?(:options, options, Hash, true)
68
+
69
+ params = {}
70
+ %i[transcription_url callback_url].each do |param|
71
+ if options.key?(param) &&
72
+ valid_param?(param, options[param], [String, Symbol], true)
73
+ params[param] = options[param]
74
+ end
75
+ end
76
+
77
+ %i[transcription_method callback_method].each do |param|
78
+ if options.key?(param) &&
79
+ valid_param?(param, options[param], [String, Symbol], true, %w[GET POST])
80
+ params[param] = options[param]
81
+ end
82
+ end
83
+
84
+ if options.key?(:time_limit) &&
85
+ valid_param?(:time_limit, options[:time_limit], Integer, true)
86
+ params[:time_limit] = options[:time_limit]
87
+ end
88
+
89
+ if options.key?(:file_format) &&
90
+ valid_param?(:file_format, options[:file_format],
91
+ [String, Symbol], true, %w[wav mp3])
92
+ params[:file_format] = options[:file_format]
93
+ end
94
+
95
+ if options.key?(:transcription_type) &&
96
+ valid_param?(:transcription_type, options[:transcription_type],
97
+ [String, Symbol], true, %w[auto hybrid])
98
+ params[:transcription_type] = options[:transcription_type]
99
+ end
100
+
101
+ perform_action('Record', 'POST', params, true)
102
+ end
103
+
104
+ def stop_record(url = nil)
105
+ if !url.nil? &&
106
+ valid_param?(:URL, url, [String, Symbol], true)
107
+ return perform_action('Record', 'DELETE', { URL: url }, false)
108
+ end
109
+ perform_action('Record', 'DELETE')
110
+ end
111
+
112
+ def play(urls, options = nil)
113
+ valid_param?(:urls, urls, Array, true)
114
+ if options.nil?
115
+ return perform_action('Play', 'POST', { urls: urls.join(',') }, true)
116
+ end
117
+ valid_param?(:options, options, Hash, true)
118
+
119
+ params = { urls: urls.join(',') }
120
+
121
+ if options.key?(:length) &&
122
+ valid_param?(:length, options[:length], Integer, true)
123
+ params[:length] = options[:length]
124
+ end
125
+
126
+ if options.key?(:legs) &&
127
+ valid_param?(:legs, options[:legs],
128
+ [String, Symbol], true, %w[aleg bleg both])
129
+ params[:legs] = options[:legs]
130
+ end
131
+
132
+ %i[loop mix].each do |param|
133
+ if options.key?(param) &&
134
+ valid_param?(param, options[param], [TrueClass, FalseClass], true)
135
+ params[param] = options[param]
136
+ end
137
+ end
138
+
139
+ perform_action('Play', 'POST', params, true)
140
+ end
141
+
142
+ def stop_play
143
+ perform_action('Play', 'DELETE', nil, false)
144
+ end
145
+
146
+ def speak(text, options = nil)
147
+ valid_param?(:text, text, String, true)
148
+ if options.nil?
149
+ return perform_action('Speak', 'POST', { text: text }, true)
150
+ end
151
+ valid_param?(:options, options, Hash, true)
152
+
153
+ params = { text: text }
154
+
155
+ if options.key?(:language) &&
156
+ valid_param?(:language, options[:language], String, true)
157
+ params[:language] = options[:language]
158
+ end
159
+
160
+ if options.key?(:voice) &&
161
+ valid_param?(:voice, options[:voice],
162
+ [String, Symbol], true, %w[MAN WOMAN])
163
+ params[:voice] = options[:voice]
164
+ end
165
+
166
+ if options.key?(:legs) &&
167
+ valid_param?(:legs, options[:legs],
168
+ [String, Symbol], true, %w[aleg bleg both])
169
+ params[:legs] = options[:legs]
170
+ end
171
+
172
+ %i[loop mix].each do |param|
173
+ if options.key?(param) &&
174
+ valid_param?(param, options[param], [TrueClass, FalseClass], true)
175
+ params[param] = options[param]
176
+ end
177
+ end
178
+
179
+ perform_action('Speak', 'POST', params, true)
180
+ end
181
+
182
+ def stop_speak
183
+ perform_action('Speak', 'DELETE', nil, false)
184
+ end
185
+
186
+ def send_digits(digits, leg = nil)
187
+ valid_param?(:digits, digits, String, true)
188
+
189
+ params = { digits: digits }
190
+
191
+ if !leg.nil? &&
192
+ valid_param?(:leg, leg,
193
+ [String, Symbol], true, %w[aleg bleg both])
194
+ params[:leg] = leg
195
+ end
196
+
197
+ perform_action('DTMF', 'POST', params, true)
198
+ end
199
+
200
+ def cancel_request
201
+ resource_path = @_resource_uri.sub('Call', 'Request')
202
+ @_client.send_request(resource_path, 'DELETE', nil)
203
+ end
204
+
205
+ def to_s
206
+ {
207
+ answer_time: @answer_time,
208
+ api_id: @api_id,
209
+ bill_duration: @bill_duration,
210
+ billed_duration: @billed_duration,
211
+ call_direction: @call_direction,
212
+ call_duration: @call_duration,
213
+ call_uuid: @call_uuid,
214
+ end_time: @end_time,
215
+ from_number: @from_number,
216
+ initiation_time: @initiation_time,
217
+ parent_call_uuid: @parent_call_uuid,
218
+ resource_uri: @resource_uri,
219
+ to_number: @to_number,
220
+ total_amount: @total_amount,
221
+ total_rate: @total_rate
222
+ }.to_s
223
+ end
224
+ end
225
+
226
+ class CallInterface < Base::ResourceInterface
227
+ def initialize(client, resource_list_json = nil)
228
+ @_name = 'Call'
229
+ @_resource_type = Call
230
+ @_identifier_string = 'call_uuid'
231
+ super
232
+ end
233
+
234
+ ##
235
+ # Makes an outbound call
236
+ #
237
+ # @param [String] from
238
+ # @param [Array] to
239
+ # @param [String] answer_url
240
+ # @param [String] answer_method
241
+ # @param [Hash] options
242
+ # @option options [String] :answer_method - The method used to call the answer_url. Defaults to POST.
243
+ # @option options [String] :ring_url - The URL that is notified by Plivo when the call is ringing. Defaults not set.
244
+ # @option options [String] :ring_method - The method used to call the ring_url. Defaults to POST.
245
+ # @option options [String] :hangup_url - The URL that will be notified by Plivo when the call hangs up. Defaults to answer_url.
246
+ # @option options [String] :hangup_method - The method used to call the hangup_url. Defaults to POST.
247
+ # @option options [String] :fallback_url - Invoked by Plivo only if answer_url is unavailable or the XML response is invalid. Should contain a XML response.
248
+ # @option options [String] :fallback_method - The method used to call the fallback_answer_url. Defaults to POST.
249
+ # @option options [String] :caller_name - Caller name to use with the call.
250
+ # @option options [String] :send_digits - Plivo plays DTMF tones when the call is answered. This is useful when dialing a phone number and an extension. Plivo will dial the number, and when the automated system picks up, sends the DTMF tones to connect to the extension. E.g. If you want to dial the 2410 extension after the call is connected, and you want to wait for a few seconds before sending the extension, add a few leading 'w' characters. Each 'w' character waits 0.5 second before sending a digit. Each 'W' character waits 1 second before sending a digit. You can also add the tone duration in ms by appending @duration after the string (default duration is 2000 ms). For example, 1w2w3@1000 See the DTMF API for additional information.
251
+ # @option options [Boolean] :send_on_preanswer - If set to true and send_digits is also set, digits are sent when the call is in preanswer state. Defaults to false.
252
+ # @option options [Int] :time_limit - Schedules the call for hangup at a specified time after the call is answered. Value should be an integer > 0(in seconds).
253
+ # @option options [Int] :hangup_on_ring - Schedules the call for hangup at a specified time after the call starts ringing. Value should be an integer >= 0 (in seconds).
254
+ # @option options [String] :machine_detection - Used to detect if the call has been answered by a machine. The valid values are true and hangup. Default time to analyze is 5000 milliseconds (or 5 seconds). You can change it with the machine_detection_time parameter. Note that no XML is processed during the analysis phase. If a machine is detected during the call and machine_detection is set to true, the Machine parameter will be set to true and will be sent to the answer_url, hangup_url, or any other URL that is invoked by the call. If a machine is detected during the call and machine_detection is set to hangup, the call hangs up immediately and a request is made to the hangup_url with the Machine parameter set to true
255
+ # @option options [Int] :machine_detection_time - Time allotted to analyze if the call has been answered by a machine. It should be an integer >= 2000 and <= 10000 and the unit is ms. The default value is 5000 ms.
256
+ # @option options [String] :machine_detection_url - A URL where machine detection parameters will be sent by Plivo. This parameter should be used to make machine detection asynchronous
257
+ # @option options [String] :machine_detection_method - The HTTP method which will be used by Plivo to request the machine_detection_url. Defaults to POST.
258
+ # @option options [String] :sip_headers- List of SIP headers in the form of 'key=value' pairs, separated by commas. E.g. head1=val1,head2=val2,head3=val3,...,headN=valN. The SIP headers are always prefixed with X-PH-. The SIP headers are present for every HTTP request made by the outbound call. Only [A-Z], [a-z] and [0-9] characters are allowed for the SIP headers key and value. Additionally, the '%' character is also allowed for the SIP headers value so that you can encode this value in the URL.
259
+ # @option options [Int] :ring_timeout - Determines the time in seconds the call should ring. If the call is not answered within the ring_timeout value or the default value of 120s, it is canceled.
260
+ # @option options [String] :parent_call_uuid - The call_uuid of the first leg in an ongoing conference call. It is recommended to use this parameter in scenarios where a member who is already present in the conference intends to add new members by initiating outbound API calls. This minimizes the delay in adding a new memeber to the conference.
261
+ # @option options [Boolean] :error_parent_not_found - if set to true and the parent_call_uuid cannot be found, the API request would return an error. If set to false, the outbound call API request will be executed even if the parent_call_uuid is not found. Defaults to false.
262
+ # @return [Call] Call
263
+ def create(from, to, answer_url, answer_method = 'POST', options = nil)
264
+ valid_param?(:from, from, [String, Symbol, Integer], true)
265
+ valid_param?(:to, to, Array, true)
266
+ to.each do |to_num|
267
+ valid_param?(:to_num, to_num, [Integer, String, Symbol], true)
268
+ end
269
+ valid_param?(:answer_url, answer_url, [String, Symbol], true)
270
+ valid_param?(:answer_method, answer_method, [String, Symbol],
271
+ true, %w[GET POST])
272
+
273
+ params = {
274
+ from: from,
275
+ to: to.join('<'),
276
+ answer_url: answer_url,
277
+ answer_method: answer_method
278
+ }
279
+
280
+ return perform_create(params) if options.nil?
281
+
282
+ perform_create(params.merge(options))
283
+ end
284
+
285
+ ##
286
+ # Get details of a call
287
+ # @param [String] call_uuid
288
+ def get(call_uuid)
289
+ valid_param?(:call_uuid, call_uuid, [String, Symbol], true)
290
+ perform_get(call_uuid)
291
+ end
292
+
293
+ # @param [String] call_uuid
294
+ def get_live(call_uuid)
295
+ perform_get(call_uuid, status: 'live')
296
+ end
297
+
298
+ # @param [Hash] options
299
+ # @option options [String] :subaccount - The id of the subaccount, if call details of the subaccount are needed.
300
+ # @option options [String] :call_direction - Filter the results by call direction. The valid inputs are inbound and outbound.
301
+ # @option options [String] :from_number - Filter the results by the number from where the call originated. For example:
302
+ # - To filter out those numbers that contain a particular number sequence, use from_number={ sequence}
303
+ # - To filter out a number that matches an exact number, use from_number={ exact_number}
304
+ # @option options [String] :to_number - Filter the results by the number to which the call was made. Tips to use this filter are:
305
+ # - To filter out those numbers that contain a particular number sequence, use to_number={ sequence}
306
+ # - To filter out a number that matches an exact number, use to_number={ exact_number}
307
+ # @option options [String] :bill_duration - Filter the results according to billed duration. The value of billed duration is in seconds. The filter can be used in one of the following five forms:
308
+ # - bill_duration: Input the exact value. E.g., to filter out calls that were exactly three minutes long, use bill_duration=180
309
+ # - bill_duration\__gt: gt stands for greater than. E.g., to filter out calls that were more than two hours in duration bill_duration\__gt=7200
310
+ # - bill_duration\__gte: gte stands for greater than or equal to. E.g., to filter out calls that were two hours or more in duration bill_duration\__gte=7200
311
+ # - bill_duration\__lt: lt stands for lesser than. E.g., to filter out calls that were less than seven minutes in duration bill_duration\__lt=420
312
+ # - bill_duration\__lte: lte stands for lesser than or equal to. E.g., to filter out calls that were two hours or less in duration bill_duration\__lte=7200
313
+ # @option options [String] :end_time - Filter out calls according to the time of completion. The filter can be used in the following five forms:
314
+ # - end_time: The format expected is YYYY-MM-DD HH:MM[:ss[.uuuuuu]]. E.g., To get all calls that ended at 2012-03-21 11:47[:30], use end_time=2012-03-21 11:47[:30]
315
+ # - end_time\__gt: gt stands for greater than. The format expected is YYYY-MM-DD HH:MM[:ss[.uuuuuu]]. E.g., To get all calls that ended after 2012-03-21 11:47, use end_time\__gt=2012-03-21 11:47
316
+ # - end_time\__gte: gte stands for greater than or equal. The format expected is YYYY-MM-DD HH:MM[:ss[.uuuuuu]]. E.g., To get all calls that ended after or exactly at 2012-03-21 11:47[:30], use end_time\__gte=2012-03-21 11:47[:30]
317
+ # - end_time\__lt: lt stands for lesser than. The format expected is YYYY-MM-DD HH:MM[:ss[.uuuuuu]]. E.g., To get all calls that ended before 2012-03-21 11:47, use end_time\__lt=2012-03-21 11:47
318
+ # - end_time\__lte: lte stands for lesser than or equal. The format expected is YYYY-MM-DD HH:MM[:ss[.uuuuuu]]. E.g., To get all calls that ended before or exactly at 2012-03-21 11:47[:30], use end_time\__lte=2012-03-21 11:47[:30]
319
+ # - Note: The above filters can be combined to get calls that ended in a particular time range. The timestamps need to be UTC timestamps.
320
+ # @option options [Int] :limit - Used to display the number of results per page. The maximum number of results that can be fetched is 20.
321
+ # @option options [Int] :offset - Denotes the number of value items by which the results should be offset. E.g., If the result contains a 1000 values and limit is set to 10 and offset is set to 705, then values 706 through 715 are displayed in the results. This parameter is also used for pagination of the results.
322
+
323
+ def list(options = nil)
324
+ return perform_list if options.nil?
325
+ valid_param?(:options, options, Hash, true)
326
+
327
+ params = {}
328
+ params_expected = %i[
329
+ subaccount bill_duration bill_duration__gt bill_duration__gte
330
+ bill_duration__lt bill_duration__lte end_time end_time__gt
331
+ end_time__gte end_time__lt end_time__lte
332
+ ]
333
+ params_expected.each do |param|
334
+ if options.key?(param) &&
335
+ valid_param?(param, options[param], [String, Symbol], true)
336
+ params[param] = options[param]
337
+ end
338
+ end
339
+
340
+ if options.key?(:call_direction) &&
341
+ valid_param?(:call_direction, options[:call_direction],
342
+ [String, Symbol], true, %w[inbound outbound])
343
+ params[:call_direction] = options[:call_direction]
344
+ end
345
+
346
+ %i[offset limit].each do |param|
347
+ if options.key?(param) && valid_param?(param, options[param], [Integer, Integer], true)
348
+ params[param] = options[param]
349
+ end
350
+ end
351
+
352
+ raise_invalid_request("Offset can't be negative") if options.key?(:offset) && options[:offset] < 0
353
+
354
+ if options.key?(:limit) && (options[:limit] > 20 || options[:limit] <= 0)
355
+ raise_invalid_request('The maximum number of results that can be '\
356
+ "fetched is 20. limit can't be more than 20 or less than 1")
357
+ end
358
+
359
+ perform_list(params)
360
+ end
361
+
362
+ def each
363
+ offset = 0
364
+ loop do
365
+ call_list = list(offset: offset)
366
+ call_list[:objects].each { |call| yield call }
367
+ offset += 20
368
+ return unless call_list.length == 20
369
+ end
370
+ end
371
+
372
+ def list_live
373
+ perform_list_without_object(status: 'live')
374
+ {
375
+ api_id: @api_id,
376
+ calls: @calls
377
+ }
378
+ end
379
+
380
+ def each_live
381
+ call_list = list_live
382
+ call_list[:calls].each { |call| yield call }
383
+ end
384
+
385
+ ##
386
+ # Transfer a call
387
+ # @param [String] call_uuid
388
+ # @param [Hash] details
389
+ # @option details [String] :legs - aleg, bleg or both Defaults to aleg. aleg will transfer call_uuid ; bleg will transfer the bridged leg (if found) of call_uuid ; both will transfer call_uuid and bridged leg of call_uuid
390
+ # @option details [String] :aleg_url - URL to transfer for aleg, if legs is aleg or both, then aleg_url has to be specified.
391
+ # @option details [String] :aleg_method - HTTP method to invoke aleg_url. Defaults to POST.
392
+ # @option details [String] :bleg_url - URL to transfer for bridged leg, if legs is bleg or both, then bleg_url has to be specified.
393
+ # @option details [String] :bleg_method - HTTP method to invoke bleg_url. Defaults to POST.
394
+ # @return [Call] Call
395
+ def update(call_uuid, details)
396
+ valid_param?(:call_uuid, call_uuid, [String, Symbol], true)
397
+ Call.new(@_client, resource_id: call_uuid).update(details)
398
+ end
399
+
400
+ # @param [String] call_uuid
401
+ def delete(call_uuid)
402
+ valid_param?(:call_uuid, call_uuid, [String, Symbol], true)
403
+ Call.new(@_client, resource_id: call_uuid).delete
404
+ end
405
+
406
+ # @param [String] call_uuid
407
+ # @param [Hash] options
408
+ # @option options [Int] :time_limit - Max recording duration in seconds. Defaults to 60.
409
+ # @option options [String] :file_format - The format of the recording. The valid formats are mp3 and wav formats. Defaults to mp3.
410
+ # @option options [String] :transcription_type - The type of transcription required. The following values are allowed:
411
+ # - auto - This is the default value. Transcription is completely automated; turnaround time is about 5 minutes.
412
+ # - hybrid - Transcription is a combination of automated and human verification processes; turnaround time is about 10-15 minutes.
413
+ # - *Our transcription service is primarily for the voicemail use case (limited to recorded files lasting for up to 2 minutes). Currently the service is available only in English and you will be charged for the usage. Please check out the price details.
414
+ # @option options [String] :transcription_url - The URL where the transcription is available.
415
+ # @option options [String] :transcription_method - The method used to invoke the transcription_url. Defaults to POST.
416
+ # @option options [String] :callback_url - The URL invoked by the API when the recording ends. The following parameters are sent to the callback_url:
417
+ # - api_id - the same API ID returned by the call record API.
418
+ # - record_url - the URL to access the recorded file.
419
+ # - call_uuid - the call uuid of the recorded call.
420
+ # - recording_id - the recording ID of the recorded call.
421
+ # - recording_duration - duration in seconds of the recording.
422
+ # - recording_duration_ms - duration in milliseconds of the recording.
423
+ # - recording_start_ms - when the recording started (epoch time UTC) in milliseconds.
424
+ # - recording_end_ms - when the recording ended (epoch time UTC) in milliseconds.
425
+ # @option options [String] :callback_method - The method which is used to invoke the callback_url URL. Defaults to POST.
426
+ def record(call_uuid, options = nil)
427
+ valid_param?(:call_uuid, call_uuid, [String, Symbol], true)
428
+ Call.new(@_client, resource_id: call_uuid).record(options)
429
+ end
430
+
431
+ # @param [String] call_uuid
432
+ # @param [String] url - You can specify a record URL to stop only one record. By default all recordings are stopped.
433
+ def stop_record(call_uuid, url = nil)
434
+ valid_param?(:call_uuid, call_uuid, [String, Symbol], true)
435
+ Call.new(@_client, resource_id: call_uuid).stop_record(url)
436
+ end
437
+
438
+ # @param [String] call_uuid
439
+ # @param [Array] urls
440
+ # @param [Hash] options
441
+ # @option options [Array of strings] :urls - A single URL or a list of comma separated URLs linking to an mp3 or wav file.
442
+ # @option options [Int] :length - Maximum length in seconds that the audio should be played.
443
+ # @option options [String] :legs - The leg on which the music will be played, can be aleg (i.e., A-leg is the first leg of the call or current call), bleg (i.e., B-leg is the second leg of the call),or both (i.e., both legs of the call).
444
+ # @option options [Boolean] :loop - If set to true, the audio file will play indefinitely.
445
+ # @option options [Boolean] :mix - If set to true, sounds are mixed with current audio flow.
446
+ def play(call_uuid, urls, options = nil)
447
+ valid_param?(:call_uuid, call_uuid, [String, Symbol], true)
448
+ valid_param?(:urls, urls, Array, true)
449
+ Call.new(@_client, resource_id: call_uuid).play(urls, options)
450
+ end
451
+
452
+ # @param [String] call_uuid
453
+ def stop_play(call_uuid)
454
+ valid_param?(:call_uuid, call_uuid, [String, Symbol], true)
455
+ Call.new(@_client, resource_id: call_uuid).stop_play
456
+ end
457
+
458
+ # @param [String] call_uuid
459
+ # @param [String] text
460
+ # @param [Hash] options
461
+ # @option options [String] :voice - The voice to be used, can be MAN, WOMAN.
462
+ # @option options [Int] :language - The language to be used, see Supported voices and languages {https://www.plivo.com/docs/api/call/speak/#supported-voices-and-languages}
463
+ # @option options [String] :legs - The leg on which the music will be played, can be aleg (i.e., A-leg is the first leg of the call or current call), bleg (i.e., B-leg is the second leg of the call),or both (i.e., both legs of the call).
464
+ # @option options [Boolean] :loop - If set to true, the audio file will play indefinitely.
465
+ # @option options [Boolean] :mix - If set to true, sounds are mixed with current audio flow.
466
+ def speak(call_uuid, text, options = nil)
467
+ valid_param?(:call_uuid, call_uuid, [String, Symbol], true)
468
+ Call.new(@_client, resource_id: call_uuid).speak(text, options)
469
+ end
470
+
471
+ # @param [String] call_uuid
472
+ def stop_speak(call_uuid)
473
+ valid_param?(:call_uuid, call_uuid, [String, Symbol], true)
474
+ Call.new(@_client, resource_id: call_uuid).stop_speak
475
+ end
476
+
477
+ # @param [String] call_uuid
478
+ # @param [String] digits - Digits to be sent.
479
+ # @param [String] leg - The leg to be used, can be aleg (the current call) or bleg (the other party in a Dial). Defaults to aleg.
480
+ def send_digits(call_uuid, digits, leg = nil)
481
+ valid_param?(:call_uuid, call_uuid, [String, Symbol], true)
482
+ Call.new(@_client, resource_id: call_uuid).send_digits(digits, leg)
483
+ end
484
+
485
+ # @param [String] call_uuid
486
+ def cancel_request(call_uuid)
487
+ valid_param?(:call_uuid, call_uuid, [String, Symbol], true)
488
+ Call.new(@_client, resource_id: call_uuid).cancel_request
489
+ end
490
+ end
491
+ end
492
+ end