Class: Switest::Agent
- Inherits:
-
Object
- Object
- Switest::Agent
- Defined in:
- lib/switest/agent.rb
Class Attribute Summary collapse
-
.client ⇒ Object
Shared client for all agents in a test.
-
.events ⇒ Object
Shared client for all agents in a test.
Instance Attribute Summary collapse
-
#call ⇒ Object
readonly
Returns the value of attribute call.
Class Method Summary collapse
- .dial(destination, from: nil, timeout: nil, headers: {}) ⇒ Object
- .listen_for_call(guards = {}) ⇒ Object
- .setup(client, events) ⇒ Object
- .teardown ⇒ Object
Instance Method Summary collapse
- #active? ⇒ Boolean
-
#alive? ⇒ Boolean
Delegate state queries to call.
- #answer(wait: 5) ⇒ Object
- #answer_time ⇒ Object
- #answered? ⇒ Boolean
- #call? ⇒ Boolean
- #end_reason ⇒ Object
- #end_time ⇒ Object
- #ended? ⇒ Boolean
- #flush_dtmf ⇒ Object
- #hangup(wait: 5) ⇒ Object
- #headers ⇒ Object
- #id ⇒ Object
- #inbound? ⇒ Boolean
-
#initialize(call) ⇒ Agent
constructor
A new instance of Agent.
- #outbound? ⇒ Boolean
- #receive_call(call) ⇒ Object
- #receive_dtmf(count: 1, timeout: 5) ⇒ Object
- #reject(reason = :decline) ⇒ Object
- #send_dtmf(digits) ⇒ Object
- #start_time ⇒ Object
- #wait_for_answer(timeout: 5) ⇒ Object
- #wait_for_call(timeout: 5) ⇒ Object
- #wait_for_end(timeout: 5) ⇒ Object
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
.client ⇒ Object
Shared client for all agents in a test
10 11 12 |
# File 'lib/switest/agent.rb', line 10 def client @client end |
.events ⇒ Object
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
#call ⇒ Object (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 |
.teardown ⇒ Object
17 18 19 20 |
# File 'lib/switest/agent.rb', line 17 def teardown @client = nil @events = nil end |
Instance Method Details
#active? ⇒ Boolean
107 108 109 |
# File 'lib/switest/agent.rb', line 107 def active? @call&.active? || false end |
#alive? ⇒ Boolean
Delegate state queries to call
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_time ⇒ Object
135 136 137 |
# File 'lib/switest/agent.rb', line 135 def answer_time @call&.answer_time end |
#answered? ⇒ Boolean
111 112 113 |
# File 'lib/switest/agent.rb', line 111 def answered? @call&.answered? || false end |
#call? ⇒ Boolean
50 51 52 |
# File 'lib/switest/agent.rb', line 50 def call? !@call.nil? end |
#end_reason ⇒ Object
143 144 145 |
# File 'lib/switest/agent.rb', line 143 def end_reason @call&.end_reason end |
#end_time ⇒ Object
139 140 141 |
# File 'lib/switest/agent.rb', line 139 def end_time @call&.end_time end |
#ended? ⇒ Boolean
115 116 117 |
# File 'lib/switest/agent.rb', line 115 def ended? @call&.ended? || false end |
#flush_dtmf ⇒ Object
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 |
#headers ⇒ Object
147 148 149 |
# File 'lib/switest/agent.rb', line 147 def headers @call&.headers end |
#id ⇒ Object
127 128 129 |
# File 'lib/switest/agent.rb', line 127 def id @call&.id end |
#inbound? ⇒ Boolean
123 124 125 |
# File 'lib/switest/agent.rb', line 123 def inbound? @call&.inbound? || false end |
#outbound? ⇒ 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_time ⇒ Object
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 |