Class: Signalwire::Relay::Calling::Component

Inherits:
Object
  • Object
show all
Defined in:
lib/signalwire/relay/calling/component.rb

Direct Known Subclasses

Answer, Await, Connect, ControlComponent, Dial, Hangup

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#blockerObject (readonly)

Returns the value of attribute blocker.



109
110
111
# File 'lib/signalwire/relay/calling/component.rb', line 109

def blocker
  @blocker
end

#callObject (readonly)

Returns the value of attribute call.



5
6
7
# File 'lib/signalwire/relay/calling/component.rb', line 5

def call
  @call
end

#completedObject (readonly)

Returns the value of attribute completed.



5
6
7
# File 'lib/signalwire/relay/calling/component.rb', line 5

def completed
  @completed
end

#eventObject (readonly)

Returns the value of attribute event.



5
6
7
# File 'lib/signalwire/relay/calling/component.rb', line 5

def event
  @event
end

#execute_resultObject (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

#stateObject (readonly)

Returns the value of attribute state.



5
6
7
# File 'lib/signalwire/relay/calling/component.rb', line 5

def state
  @state
end

#successfulObject (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_eventsObject



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_blockerObject



101
102
103
# File 'lib/signalwire/relay/calling/component.rb', line 101

def create_blocker
  @blocker = Concurrent::Promises.resolvable_future
end

#event_typeObject

The event type the subclass listens to

Raises:

  • (NotImplementedError)


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

#executeObject



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_paramsObject



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

#methodObject

Relay method corresponding to the command

Raises:

  • (NotImplementedError)


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

Raises:

  • (NotImplementedError)


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

#payloadObject

Relay payload Implement this on child classes



23
24
25
# File 'lib/signalwire/relay/calling/component.rb', line 23

def payload
  nil
end

#setup_handlersObject



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_blockerObject



85
86
87
88
# File 'lib/signalwire/relay/calling/component.rb', line 85

def wait_on_blocker
  create_blocker
  blocker.wait
end