Class: Switest::Agent

Inherits:
Object
  • Object
show all
Defined in:
lib/switest/agent.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(call) ⇒ Agent

Returns a new instance of Agent.



45
46
47
48
# File 'lib/switest/agent.rb', line 45

def initialize(call)
  @call = call
  @call_promise = Async::Promise.new
end

Class Attribute Details

.clientObject

Shared client for all agents in a test



10
11
12
# File 'lib/switest/agent.rb', line 10

def client
  @client
end

.eventsObject

Shared client for all agents in a test



10
11
12
# File 'lib/switest/agent.rb', line 10

def events
  @events
end

Instance Attribute Details

#callObject (readonly)

Returns the value of attribute call.



43
44
45
# File 'lib/switest/agent.rb', line 43

def call
  @call
end

Class Method Details

.dial(destination, from: nil, timeout: nil, headers: {}) ⇒ Object



22
23
24
25
26
27
# File 'lib/switest/agent.rb', line 22

def dial(destination, from: nil, timeout: nil, headers: {})
  raise "Agent.setup not called" unless @client

  call = @client.dial(to: destination, from: from, timeout: timeout, headers: headers)
  new(call)
end

.listen_for_call(guards = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/switest/agent.rb', line 29

def listen_for_call(guards = {})
  raise "Agent.setup not called" unless @client

  agent = new(nil)

  # Register a one-time handler for matching inbound calls
  @events.once(:offer, guards) do |data|
    agent.receive_call(data[:call])
  end

  agent
end

.setup(client, events) ⇒ Object



12
13
14
15
# File 'lib/switest/agent.rb', line 12

def setup(client, events)
  @client = client
  @events = events
end

.teardownObject



17
18
19
20
# File 'lib/switest/agent.rb', line 17

def teardown
  @client = nil
  @events = nil
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/switest/agent.rb', line 107

def active?
  @call&.active? || false
end

#alive?Boolean

Delegate state queries to call

Returns:

  • (Boolean)


103
104
105
# File 'lib/switest/agent.rb', line 103

def alive?
  @call&.alive? || false
end

#answer(wait: 5) ⇒ Object



54
55
56
57
# File 'lib/switest/agent.rb', line 54

def answer(wait: 5)
  raise "No call to answer" unless @call
  @call.answer(wait: wait)
end

#answer_timeObject



135
136
137
# File 'lib/switest/agent.rb', line 135

def answer_time
  @call&.answer_time
end

#answered?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/switest/agent.rb', line 111

def answered?
  @call&.answered? || false
end

#call?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/switest/agent.rb', line 50

def call?
  !@call.nil?
end

#end_reasonObject



143
144
145
# File 'lib/switest/agent.rb', line 143

def end_reason
  @call&.end_reason
end

#end_timeObject



139
140
141
# File 'lib/switest/agent.rb', line 139

def end_time
  @call&.end_time
end

#ended?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/switest/agent.rb', line 115

def ended?
  @call&.ended? || false
end

#flush_dtmfObject



79
80
81
82
# File 'lib/switest/agent.rb', line 79

def flush_dtmf
  raise "No call for DTMF" unless @call
  @call.flush_dtmf
end

#hangup(wait: 5) ⇒ Object



59
60
61
62
# File 'lib/switest/agent.rb', line 59

def hangup(wait: 5)
  raise "No call to hangup" unless @call
  @call.hangup(wait: wait)
end

#headersObject



147
148
149
# File 'lib/switest/agent.rb', line 147

def headers
  @call&.headers
end

#idObject



127
128
129
# File 'lib/switest/agent.rb', line 127

def id
  @call&.id
end

#inbound?Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/switest/agent.rb', line 123

def inbound?
  @call&.inbound? || false
end

#outbound?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/switest/agent.rb', line 119

def outbound?
  @call&.outbound? || false
end

#receive_call(call) ⇒ Object



152
153
154
155
# File 'lib/switest/agent.rb', line 152

def receive_call(call)
  @call = call
  @call_promise.resolve(true)
end

#receive_dtmf(count: 1, timeout: 5) ⇒ Object



74
75
76
77
# File 'lib/switest/agent.rb', line 74

def receive_dtmf(count: 1, timeout: 5)
  raise "No call for DTMF" unless @call
  @call.receive_dtmf(count: count, timeout: timeout)
end

#reject(reason = :decline) ⇒ Object



64
65
66
67
# File 'lib/switest/agent.rb', line 64

def reject(reason = :decline)
  raise "No call to reject" unless @call
  @call.reject(reason)
end

#send_dtmf(digits) ⇒ Object



69
70
71
72
# File 'lib/switest/agent.rb', line 69

def send_dtmf(digits)
  raise "No call for DTMF" unless @call
  @call.send_dtmf(digits)
end

#start_timeObject



131
132
133
# File 'lib/switest/agent.rb', line 131

def start_time
  @call&.start_time
end

#wait_for_answer(timeout: 5) ⇒ Object



92
93
94
95
# File 'lib/switest/agent.rb', line 92

def wait_for_answer(timeout: 5)
  raise "No call to wait for" unless @call
  @call.wait_for_answer(timeout: timeout)
end

#wait_for_call(timeout: 5) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/switest/agent.rb', line 84

def wait_for_call(timeout: 5)
  return true if @call
  Async::Task.current.with_timeout(timeout) { @call_promise.wait }
  !@call.nil?
rescue Async::TimeoutError
  !@call.nil?
end

#wait_for_end(timeout: 5) ⇒ Object



97
98
99
100
# File 'lib/switest/agent.rb', line 97

def wait_for_end(timeout: 5)
  raise "No call to wait for" unless @call
  @call.wait_for_end(timeout: timeout)
end