Class: Signalwire::Relay::Calling::Component
- Inherits:
-
Object
- Object
- Signalwire::Relay::Calling::Component
- Defined in:
- lib/signalwire/relay/calling/component.rb
Instance Attribute Summary collapse
-
#blocker ⇒ Object
readonly
Returns the value of attribute blocker.
-
#call ⇒ Object
readonly
Returns the value of attribute call.
-
#completed ⇒ Object
readonly
Returns the value of attribute completed.
-
#event ⇒ Object
readonly
Returns the value of attribute event.
-
#execute_result ⇒ Object
readonly
Returns the value of attribute execute_result.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
-
#successful ⇒ Object
readonly
Returns the value of attribute successful.
Instance Method Summary collapse
- #check_for_waiting_events ⇒ Object
- #create_blocker ⇒ Object
-
#event_type ⇒ Object
The event type the subclass listens to.
- #execute ⇒ Object
- #execute_params(method_suffix = nil) ⇒ Object
- #handle_execute_result(event, outcome) ⇒ Object
-
#initialize(call:) ⇒ Component
constructor
A new instance of Component.
- #inner_params ⇒ Object
-
#method ⇒ Object
Relay method corresponding to the command.
-
#notification_handler(_event) ⇒ Object
This is the most important method to implement in a subclass.
-
#payload ⇒ Object
Relay payload Implement this on child classes.
- #setup_handlers ⇒ Object
- #setup_waiting_events(events) ⇒ Object
- #terminate(event = nil) ⇒ Object
- #unblock(value) ⇒ Object
- #wait_for(*events) ⇒ Object
- #wait_on_blocker ⇒ Object
Constructor Details
#initialize(call:) ⇒ Component
Returns a new instance of Component.
7 8 9 10 11 12 13 |
# File 'lib/signalwire/relay/calling/component.rb', line 7 def initialize(call:) @call = call @completed = false @successful = false @call.register_component(self) end |
Instance Attribute Details
#blocker ⇒ Object (readonly)
Returns the value of attribute blocker.
109 110 111 |
# File 'lib/signalwire/relay/calling/component.rb', line 109 def blocker @blocker end |
#call ⇒ Object (readonly)
Returns the value of attribute call.
5 6 7 |
# File 'lib/signalwire/relay/calling/component.rb', line 5 def call @call end |
#completed ⇒ Object (readonly)
Returns the value of attribute completed.
5 6 7 |
# File 'lib/signalwire/relay/calling/component.rb', line 5 def completed @completed end |
#event ⇒ Object (readonly)
Returns the value of attribute event.
5 6 7 |
# File 'lib/signalwire/relay/calling/component.rb', line 5 def event @event end |
#execute_result ⇒ Object (readonly)
Returns the value of attribute execute_result.
5 6 7 |
# File 'lib/signalwire/relay/calling/component.rb', line 5 def execute_result @execute_result end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
5 6 7 |
# File 'lib/signalwire/relay/calling/component.rb', line 5 def state @state end |
#successful ⇒ Object (readonly)
Returns the value of attribute successful.
5 6 7 |
# File 'lib/signalwire/relay/calling/component.rb', line 5 def successful @successful end |
Instance Method Details
#check_for_waiting_events ⇒ Object
111 112 113 |
# File 'lib/signalwire/relay/calling/component.rb', line 111 def check_for_waiting_events unblock(event) if @events_waiting.include?(@state) end |
#create_blocker ⇒ Object
101 102 103 |
# File 'lib/signalwire/relay/calling/component.rb', line 101 def create_blocker @blocker = Concurrent::Promises.resolvable_future end |
#event_type ⇒ Object
The event type the subclass listens to
28 29 30 |
# File 'lib/signalwire/relay/calling/component.rb', line 28 def event_type raise NotImplementedError, 'Define this method on the child classes' end |
#execute ⇒ Object
49 50 51 52 53 54 |
# File 'lib/signalwire/relay/calling/component.rb', line 49 def execute setup_handlers @call.relay_execute execute_params do |event, outcome| handle_execute_result(event, outcome) end end |
#execute_params(method_suffix = nil) ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/signalwire/relay/calling/component.rb', line 32 def execute_params(method_suffix = nil) { protocol: @call.client.protocol, method: method + method_suffix.to_s, params: inner_params } end |
#handle_execute_result(event, outcome) ⇒ Object
62 63 64 65 |
# File 'lib/signalwire/relay/calling/component.rb', line 62 def handle_execute_result(event, outcome) @execute_result = event terminate if outcome == :failure end |
#inner_params ⇒ Object
40 41 42 43 44 45 46 47 |
# File 'lib/signalwire/relay/calling/component.rb', line 40 def inner_params result = { node_id: @call.node_id, call_id: @call.id } result[:params] = payload if payload result end |
#method ⇒ Object
Relay method corresponding to the command
17 18 19 |
# File 'lib/signalwire/relay/calling/component.rb', line 17 def method raise NotImplementedError, 'Define this method on the child classes' end |
#notification_handler(_event) ⇒ Object
This is the most important method to implement in a subclass
92 93 94 95 96 97 98 99 |
# File 'lib/signalwire/relay/calling/component.rb', line 92 def notification_handler(_event) # to be implemented by subclasses. An example could be: # # if event.call_params[:call_state] == 'ended' # unblock # end raise NotImplementedError, 'Define this method on the child classes' end |
#payload ⇒ Object
Relay payload Implement this on child classes
23 24 25 |
# File 'lib/signalwire/relay/calling/component.rb', line 23 def payload nil end |
#setup_handlers ⇒ Object
56 57 58 59 60 |
# File 'lib/signalwire/relay/calling/component.rb', line 56 def setup_handlers @call.on :event, event_type: event_type do |evt| notification_handler(evt) end end |
#setup_waiting_events(events) ⇒ Object
75 76 77 |
# File 'lib/signalwire/relay/calling/component.rb', line 75 def setup_waiting_events(events) @events_waiting = events end |
#terminate(event = nil) ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/signalwire/relay/calling/component.rb', line 67 def terminate(event = nil) @completed = true @successful = false @state = 'failed' @event = event if event blocker&.reject end |
#unblock(value) ⇒ Object
105 106 107 |
# File 'lib/signalwire/relay/calling/component.rb', line 105 def unblock(value) blocker&.resolve value end |
#wait_for(*events) ⇒ Object
79 80 81 82 83 |
# File 'lib/signalwire/relay/calling/component.rb', line 79 def wait_for(*events) setup_waiting_events(events) execute wait_on_blocker unless @completed end |
#wait_on_blocker ⇒ Object
85 86 87 88 |
# File 'lib/signalwire/relay/calling/component.rb', line 85 def wait_on_blocker create_blocker blocker.wait end |