class Plivo::Resources::Call

Public Class Methods

new(client, options = nil) click to toggle source
Calls superclass method Plivo::Base::Resource::new
# File lib/plivo/resources/calls.rb, line 5
def initialize(client, options = nil)
  @_name = 'Call'
  @_identifier_string = 'call_uuid'
  super
  @_is_voice_request = true
end

Public Instance Methods

cancel_request() click to toggle source
# File lib/plivo/resources/calls.rb, line 207
def cancel_request
  resource_path = @_resource_uri.sub('Call', 'Request')
  @_client.send_request(resource_path, 'DELETE', nil, nil, false , is_voice_request: @_is_voice_request)
end
delete() click to toggle source
# File lib/plivo/resources/calls.rb, line 62
def delete
  perform_delete
end
get_all_streams() click to toggle source
# File lib/plivo/resources/calls.rb, line 270
def get_all_streams
  perform_action('Stream', 'GET', nil, true )
end
get_stream(stream_id) click to toggle source
# File lib/plivo/resources/calls.rb, line 274
def get_stream(stream_id)
  valid_param?(:stream_id, stream_id, [String, Symbol, Integer], true)
  perform_action('Stream/' + stream_id, 'GET', nil, true)
end
play(urls, options = nil) click to toggle source
# File lib/plivo/resources/calls.rb, line 113
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) click to toggle source
# File lib/plivo/resources/calls.rb, line 66
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) click to toggle source
# File lib/plivo/resources/calls.rb, line 193
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) click to toggle source
# File lib/plivo/resources/calls.rb, line 147
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

  if options.key?(:type) &&
    valid_param?(:type, options[:type],
                 [String, Symbol], true, %w[text ssml])
   params[:type] = options[:type]
 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
start_stream(service_url, options = nil) click to toggle source
# File lib/plivo/resources/calls.rb, line 212
def start_stream(service_url, options = nil)
  valid_param?(:service_url, service_url, [String, Symbol], true)
  if options.nil?
    return perform_action('Stream', 'POST', { service_url: service_url }, true)
  end

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

  params = { service_url: service_url }

  if options.key?(:bidirectional) &&
    valid_param?(:bidirectional, options[:bidirectional], [TrueClass, FalseClass], false )
    params[:bidirectional] = options[:bidirectional]
  end

  if options.key?(:audio_track) &&
    valid_param?(:audio_track, options[:audio_track],
                 [String, Symbol], false, %w[inbound outbound both])
    params[:audio_track] = options[:audio_track]
  end

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

  if options.key?(:status_callback_url) &&
    valid_param?(:status_callback_url, options[:status_callback_url], [String, Symbol], false)
    params[:status_callback_url] = options[:status_callback_url]
  end

  if options.key?(:status_callback_method) &&
    valid_param?(:status_callback_method, options[:status_callback_method],
                 [String, Symbol], false, %w[GET POST get post])
    params[:status_callback_method] = options[:status_callback_method]
  end

  if options.key?(:content_type) &&
    valid_param?(:content_type, options[:content_type], [String, Symbol, Integer], false)
    params[:content_type] = options[:content_type]
  end

  if options.key?(:extra_headers) &&
    valid_param?(:extra_headers, options[:extra_headers], [String], false)
    params[:extra_headers] = options[:extra_headers]
  end
  perform_action('Stream', 'POST', params, true)
end
stop_all_streams() click to toggle source
# File lib/plivo/resources/calls.rb, line 261
def stop_all_streams
  perform_action('Stream', 'DELETE', nil, false)
end
stop_play() click to toggle source
# File lib/plivo/resources/calls.rb, line 143
def stop_play
  perform_action('Play', 'DELETE', nil, false)
end
stop_record(url = nil) click to toggle source
# File lib/plivo/resources/calls.rb, line 105
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_speak() click to toggle source
# File lib/plivo/resources/calls.rb, line 189
def stop_speak
  perform_action('Speak', 'DELETE', nil, false)
end
stop_stream(stream_id) click to toggle source
# File lib/plivo/resources/calls.rb, line 265
def stop_stream(stream_id)
  valid_param?(:stream_id, stream_id, [String, Symbol, Integer], true)
  perform_action('Stream/' + stream_id, 'DELETE', nil, false)
end
to_s() click to toggle source
# File lib/plivo/resources/calls.rb, line 279
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_state: @call_state,
    call_uuid: @call_uuid,
    conference_uuid: @conference_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,
    stir_verification: @stir_verification,
    stir_attestation: @stir_attestation,
    source_ip: @source_ip,
    cnam_lookup: @cnam_lookup
  }
  call_details = call_details.select {|k, v| !v.nil? }
  call_details.to_s
end
update(options) click to toggle source
# File lib/plivo/resources/calls.rb, line 12
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