Class: TwilioTestToolkit::CallScope

Inherits:
Object
  • Object
show all
Defined in:
lib/twilio-test-toolkit/call_scope.rb

Overview

Models a scope within a call.

Direct Known Subclasses

CallInProgress

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object

Make this easier to support TwiML elements…



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
103
104
105
106
107
# File 'lib/twilio-test-toolkit/call_scope.rb', line 72

def method_missing(meth, *args, &block)
  # support any check for a given attribute on a given element
  #
  # eg. has_action_on_dial?, has_method_on_sip?, etc.
  #
  # Attribute-checking appears to be case-sensitive, which x means:
  #
  # has_finishOnKey_on_record?("#")
  #
  # I'm not crazy about this mixed case, so we can also do a more
  # Rubyish way:
  #
  # has_finish_on_key_on_record?("#")
  #
  if meth.to_s =~ /^has_([a-zA-Z_]+)_on_([a-zA-Z]+)\?$/
    has_attr_on_element?($2, $1, *args, &block)

  # support any check for the existence of a given element
  # with an optional check on the inner_text.
  elsif meth.to_s =~ /^has_([a-zA-Z]+)\?$/
    has_element?($1, *args, &block)

  # get a given element node
  elsif meth.to_s =~ /^get_([a-z]+)_node$/
    get_element_node($1, *args, &block)

  # run a block within a given node context
  elsif meth.to_s =~ /^within_([a-z]+)$/
    within_element($1, *args, &block)

  else
    super # You *must* call super if you don't handle the
          # method, otherwise you'll mess up Ruby's method
          # lookup.
  end
end

Class Method Details

.has_element(el, options = {}) ⇒ Object

Note that el is case sensitive and must match the desired TwiML element. eg. Play (correct) vs play (incorrect).



7
8
9
10
11
# File 'lib/twilio-test-toolkit/call_scope.rb', line 7

def self.has_element(el, options = {})
  define_method "has_#{el.downcase}?" do |inner = nil|
    return has_element?(el, inner, options)
  end
end

Instance Method Details

#current_pathObject

Some basic accessors



114
115
116
# File 'lib/twilio-test-toolkit/call_scope.rb', line 114

def current_path
  @current_path
end

#follow_redirect(options = {}) ⇒ Object



27
28
29
30
31
32
# File 'lib/twilio-test-toolkit/call_scope.rb', line 27

def follow_redirect(options = {})
  el = get_redirect_node
  raise "No redirect" if el.nil?

  return CallScope.from_request(self, el.text, { :method =>el[:method]}.merge(options))
end

#follow_redirect!(options = {}) ⇒ Object



34
35
36
37
38
39
# File 'lib/twilio-test-toolkit/call_scope.rb', line 34

def follow_redirect!(options = {})
  el = get_redirect_node
  raise "No redirect" if el.nil?

  request_for_twiml!(normalize_redirect_path(el.text), { :method => el[:method] }.merge(options))
end

#gather?Boolean

Stuff for gatherers

Returns:

  • (Boolean)


42
43
44
# File 'lib/twilio-test-toolkit/call_scope.rb', line 42

def gather?
  @xml.name == "Gather"
end

#gather_actionObject



46
47
48
49
# File 'lib/twilio-test-toolkit/call_scope.rb', line 46

def gather_action
  raise "Not a gather" unless gather?
  return @xml["action"]
end

#gather_finish_on_keyObject



56
57
58
59
# File 'lib/twilio-test-toolkit/call_scope.rb', line 56

def gather_finish_on_key
  raise "Not a gather" unless gather?
  return @xml["finishOnKey"] || '#' # '#' is the default finish key if not specified
end

#gather_methodObject



51
52
53
54
# File 'lib/twilio-test-toolkit/call_scope.rb', line 51

def gather_method
  raise "Not a gather" unless gather?
  return @xml["method"]
end

#has_redirect_to?(url) ⇒ Boolean

Stuff for redirects

Returns:

  • (Boolean)


21
22
23
24
25
# File 'lib/twilio-test-toolkit/call_scope.rb', line 21

def has_redirect_to?(url)
  el = get_redirect_node
  return false if el.nil?
  return normalize_redirect_path(el.text) == normalize_redirect_path(url)
end

#press(digits) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/twilio-test-toolkit/call_scope.rb', line 61

def press(digits)
  raise "Not a gather" unless gather?

  # Fetch the path and then post
  path = gather_action

  # Update the root call
  root_call.request_for_twiml!(path, :digits => digits, :method => gather_method, :finish_on_key => gather_finish_on_key)
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/twilio-test-toolkit/call_scope.rb', line 109

def respond_to_missing?(method_name, include_private = false)
  method_name.to_s.match(/^(has_|get_[a-z]+_node|within_)/) || super
end

#response_xmlObject



118
119
120
# File 'lib/twilio-test-toolkit/call_scope.rb', line 118

def response_xml
  @response_xml
end

#root_callObject



122
123
124
# File 'lib/twilio-test-toolkit/call_scope.rb', line 122

def root_call
  @root_call
end