Module: Twilio::Rails::Models::PhoneCall

Extended by:
ActiveSupport::Concern
Included in:
PhoneCall
Defined in:
lib/twilio/rails/models/phone_call.rb

Overview

The record of a phone call. Can be inbound or outbound. The associated Response objects in order track the progress of the call.

Instance Method Summary collapse

Instance Method Details

#answering_machine?true, false

Indicates if the call was answered by an answering machine. Only will return true if answering machine detection is enabled. Is always false for inbound calls.

Returns:

  • (true, false)

    true if the call was answered by an answering machine.



50
51
52
# File 'lib/twilio/rails/models/phone_call.rb', line 50

def answering_machine?
  outbound? && answered_by == "machine_start"
end

#completed?true, false

Checks if the call is in the completed state. This does not cover all possible states for a call that is not in progress. See #in_progress? to check if a call is finished or not.

Returns:

  • (true, false)

    true if the call is in the completed state.



65
66
67
# File 'lib/twilio/rails/models/phone_call.rb', line 65

def completed?
  call_status.in?(["completed"])
end

#for?(tree:) ⇒ true, false

Checks if the call is for a given tree or trees, by class or by name.

Parameters:

Returns:

  • (true, false)

    true if the call is for the given tree or trees.



93
94
95
96
97
# File 'lib/twilio/rails/models/phone_call.rb', line 93

def for?(tree:)
  trees = Array(tree).map { |t| t.is_a?(Twilio::Rails::Phone::Tree) ? t.name : t.to_s }

  trees.include?(tree_name)
end

#in_progress?true, false

Checks if that call is in a state where it is currently in progress with the caller. This includes ringing, queued, initiated, or in progress. Use this method to check if the call has finished or not.

Returns:

  • (true, false)

    true if the call is currently ringing, queued, or in progress.



73
74
75
# File 'lib/twilio/rails/models/phone_call.rb', line 73

def in_progress?
  call_status.blank? || call_status.in?(["queued", "initiated", "ringing", "in-progress"])
end

#locationString

A formatted string for the location data of the caller provided by Twilio, if any is available.

Returns:

  • (String)

    The location of the caller.



80
81
82
# File 'lib/twilio/rails/models/phone_call.rb', line 80

def location
  Twilio::Rails::Formatter.location(city: from_city, country: from_country, province: from_province)
end

#no_answer?true, false

Indicates if the call was not answered, busy, or failed. Is always false for inbound calls.

Returns:

  • (true, false)

    true if the call was not answered by a person.



57
58
59
# File 'lib/twilio/rails/models/phone_call.rb', line 57

def no_answer?
  outbound? && call_status.in?(["busy", "failed", "no-answer"])
end

#recalculate_lengthInteger

Updates the length_seconds attribute based on the time difference between the first and most recent responses in the phone call. Called by Phone::Response when it is updated.

Returns:

  • (Integer)

    The length of the call in seconds.



103
104
105
106
107
108
109
110
111
112
# File 'lib/twilio/rails/models/phone_call.rb', line 103

def recalculate_length
  first_response = responses.in_order.first
  last_response = responses.in_order.last
  result = 0
  estimated_length_seconds = 5 # scientifically determined to be extremely accurate

  result = last_response.created_at.to_i - first_response.created_at.to_i + estimated_length_seconds if first_response
  update!(length_seconds: result)
  result
end

#treeString

Returns The Phone::Tree for the call.

Returns:



85
86
87
# File 'lib/twilio/rails/models/phone_call.rb', line 85

def tree
  @tree ||= Twilio::Rails.config.phone_trees.for(tree_name)
end