Class: Plivo::Resources::Call

Inherits:
Base::Resource show all
Defined in:
lib/plivo/resources/calls.rb

Constant Summary

Constants included from Utils

Utils::TYPE_WHITELIST

Instance Attribute Summary

Attributes inherited from Base::Resource

#id

Instance Method Summary collapse

Methods included from Utils

expected_type?, expected_value?, raise_invalid_request, valid_account?, valid_mainaccount?, valid_param?, valid_signature?, valid_subaccount?

Constructor Details

#initialize(client, options = nil) ⇒ Call

Returns a new instance of Call.



5
6
7
8
9
# File 'lib/plivo/resources/calls.rb', line 5

def initialize(client, options = nil)
  @_name = 'Call'
  @_identifier_string = 'call_uuid'
  super
end

Instance Method Details

#cancel_requestObject



200
201
202
203
# File 'lib/plivo/resources/calls.rb', line 200

def cancel_request
  resource_path = @_resource_uri.sub('Call', 'Request')
  @_client.send_request(resource_path, 'DELETE', nil)
end

#deleteObject



61
62
63
# File 'lib/plivo/resources/calls.rb', line 61

def delete
  perform_delete
end

#play(urls, options = nil) ⇒ Object



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

def play(urls, options = nil)
  valid_param?(:urls, urls, Array, true)
  if options.nil?
    return perform_action('Play', 'POST', { urls: urls.join(',') }, true)
  end
  valid_param?(:options, options, Hash, true)

  params = { urls: urls.join(',') }

  if options.key?(:length) &&
     valid_param?(:length, options[:length], Integer, true)
    params[:length] = options[:length]
  end

  if options.key?(:legs) &&
     valid_param?(:legs, options[:legs],
                  [String, Symbol], true, %w[aleg bleg both])
    params[:legs] = options[:legs]
  end

  i[loop mix].each do |param|
    if options.key?(param) &&
       valid_param?(param, options[param], [TrueClass, FalseClass], true)
      params[param] = options[param]
    end
  end

  perform_action('Play', 'POST', params, true)
end

#record(options = nil) ⇒ Object



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

def record(options = nil)
  return perform_action('Record', 'POST', nil, true) if options.nil?
  valid_param?(:options, options, Hash, true)

  params = {}
  i[transcription_url callback_url].each do |param|
    if options.key?(param) &&
       valid_param?(param, options[param], [String, Symbol], true)
      params[param] = options[param]
    end
  end

  i[transcription_method callback_method].each do |param|
    if options.key?(param) &&
       valid_param?(param, options[param], [String, Symbol], true, %w[GET POST])
      params[param] = options[param]
    end
  end

  if options.key?(:time_limit) &&
     valid_param?(:time_limit, options[:time_limit], Integer, true)
    params[:time_limit] = options[:time_limit]
  end

  if options.key?(:file_format) &&
     valid_param?(:file_format, options[:file_format],
                  [String, Symbol], true, %w[wav mp3])
    params[:file_format] = options[:file_format]
  end

  if options.key?(:transcription_type) &&
     valid_param?(:transcription_type, options[:transcription_type],
                  [String, Symbol], true, %w[auto hybrid])
    params[:transcription_type] = options[:transcription_type]
  end

  perform_action('Record', 'POST', params, true)
end

#send_digits(digits, leg = nil) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/plivo/resources/calls.rb', line 186

def send_digits(digits, leg = nil)
  valid_param?(:digits, digits, String, true)

  params = { digits: digits }

  if !leg.nil? &&
     valid_param?(:leg, leg,
                  [String, Symbol], true, %w[aleg bleg both])
    params[:leg] = leg
  end

  perform_action('DTMF', 'POST', params, true)
end

#speak(text, options = nil) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/plivo/resources/calls.rb', line 146

def speak(text, options = nil)
  valid_param?(:text, text, String, true)
  if options.nil?
    return perform_action('Speak', 'POST', { text: text }, true)
  end
  valid_param?(:options, options, Hash, true)

  params = { text: text }

  if options.key?(:language) &&
     valid_param?(:language, options[:language], String, true)
    params[:language] = options[:language]
  end

  if options.key?(:voice) &&
     valid_param?(:voice, options[:voice],
                  [String, Symbol], true, %w[MAN WOMAN])
    params[:voice] = options[:voice]
  end

  if options.key?(:legs) &&
     valid_param?(:legs, options[:legs],
                  [String, Symbol], true, %w[aleg bleg both])
    params[:legs] = options[:legs]
  end

  i[loop mix].each do |param|
    if options.key?(param) &&
       valid_param?(param, options[param], [TrueClass, FalseClass], true)
      params[param] = options[param]
    end
  end

  perform_action('Speak', 'POST', params, true)
end

#stop_playObject



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

def stop_play
  perform_action('Play', 'DELETE', nil, false)
end

#stop_record(url = nil) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/plivo/resources/calls.rb', line 104

def stop_record(url = nil)
  if !url.nil? &&
     valid_param?(:URL, url, [String, Symbol], true)
    return perform_action('Record', 'DELETE', { URL: url }, false)
  end
  perform_action('Record', 'DELETE')
end

#stop_speakObject



182
183
184
# File 'lib/plivo/resources/calls.rb', line 182

def stop_speak
  perform_action('Speak', 'DELETE', nil, false)
end

#to_sObject



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/plivo/resources/calls.rb', line 205

def to_s
  call_details = {
    answer_time: @answer_time,
    api_id: @api_id,
    bill_duration: @bill_duration,
    billed_duration: @billed_duration,
    call_direction: @call_direction,
    call_duration: @call_duration,
    call_status: @call_status,
    call_uuid: @call_uuid,
    end_time: @end_time,
    from_number: @from_number,
    initiation_time: @initiation_time,
    parent_call_uuid: @parent_call_uuid,
    hangup_cause_code: @hangup_cause_code,
    hangup_cause_name: @hangup_cause_name,
    hangup_source: @hangup_source,
    resource_uri: @resource_uri,
    to_number: @to_number,
    total_amount: @total_amount,
    total_rate: @total_rate,
    to: @to,
    from: @from,
    request_uuid: @request_uuid,
    direction: @direction,
    caller_name: @caller_name
  }
  call_details = call_details.select {|k, v| !v.nil? }
  call_details.to_s
end

#update(options) ⇒ Object



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

def update(options)
  valid_param?(:options, options, Hash, true)

  params = {}

  if options.key?(:legs) &&
     valid_param?(:legs, options[:legs],
                  [String, Symbol], true, %w[aleg bleg both])
    params[:legs] = options[:legs]
  end

  unless options.key?(:legs)
    unless options.key?(:aleg_url)
      raise_invalid_request('default leg is aleg, aleg_url has to be specified')
    end
    params[:aleg_url] = options[:aleg_url]
  end

  if options[:legs] == 'aleg'
    unless options.key?(:aleg_url)
      raise_invalid_request('leg is aleg, aleg_url has to be specified')
    end
    params[:aleg_url] = options[:aleg_url]
  end

  if options[:legs] == 'bleg'
    unless options.key?(:bleg_url)
      raise_invalid_request('leg is bleg, bleg_url has to be specified')
    end
    params[:bleg_url] = options[:bleg_url]
  end

  if options[:legs] == 'both'
    unless options.key?(:aleg_url) && options.key?(:bleg_url)
      raise_invalid_request('leg is both, aleg_url & bleg_url have to be specified')
    end
    params[:aleg_url] = options[:aleg_url]
    params[:bleg_url] = options[:bleg_url]
  end

  i[aleg_method bleg_method].each do |param|
    if options.key?(param) &&
       valid_param?(param, options[param], [String, Symbol], true, %w[GET POST])
      params[param] = options[param]
    end
  end

  perform_update(params)
end