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

Inherits:
Object
  • Object
show all
Includes:
Logger
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

Methods included from Logger

#level=, #logger, logger

Constructor Details

#initialize(call:) ⇒ Component

Returns a new instance of Component.



8
9
10
11
12
13
14
# File 'lib/signalwire/relay/calling/component.rb', line 8

def initialize(call:)
  @call = call
  @completed = false
  @successful = false
  @events_waiting = []
  @call.register_component(self)
end

Instance Attribute Details

#blockerObject (readonly)

Returns the value of attribute blocker.



119
120
121
# File 'lib/signalwire/relay/calling/component.rb', line 119

def blocker
  @blocker
end

#callObject (readonly)

Returns the value of attribute call.



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

def call
  @call
end

#completedObject (readonly)

Returns the value of attribute completed.



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

def completed
  @completed
end

#eventObject (readonly)

Returns the value of attribute event.



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

def event
  @event
end

#execute_resultObject (readonly)

Returns the value of attribute execute_result.



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

def execute_result
  @execute_result
end

#stateObject (readonly)

Returns the value of attribute state.



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

def state
  @state
end

#successfulObject (readonly)

Returns the value of attribute successful.



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

def successful
  @successful
end

Instance Method Details

#after_execute(event) ⇒ Object



69
70
71
# File 'lib/signalwire/relay/calling/component.rb', line 69

def after_execute(event)
  # implemented in subclasses
end

#check_for_waiting_eventsObject



121
122
123
# File 'lib/signalwire/relay/calling/component.rb', line 121

def check_for_waiting_events
  unblock(event) if @events_waiting.include?(@state)
end

#create_blockerObject



111
112
113
# File 'lib/signalwire/relay/calling/component.rb', line 111

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

#event_typeObject

The event type the subclass listens to

Raises:

  • (NotImplementedError)


29
30
31
# File 'lib/signalwire/relay/calling/component.rb', line 29

def event_type
  raise NotImplementedError, 'Define this method on the child classes'
end

#executeObject



50
51
52
53
54
55
# File 'lib/signalwire/relay/calling/component.rb', line 50

def execute
  setup_handlers
  @call.relay_execute execute_params do |event, outcome|
    handle_execute_result(event, outcome)
  end
end

#execute_params(method_suffix = nil, inner_params_merge = {}) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/signalwire/relay/calling/component.rb', line 33

def execute_params(method_suffix = nil, inner_params_merge = {})
  {
    protocol: @call.client.protocol,
    method: method + method_suffix.to_s,
    params: inner_params.merge(inner_params_merge)
  }
end

#handle_execute_result(event, outcome) ⇒ Object



63
64
65
66
67
# File 'lib/signalwire/relay/calling/component.rb', line 63

def handle_execute_result(event, outcome)
  @execute_result = event
  after_execute(event)
  terminate if outcome == :failure
end

#has_blocker?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/signalwire/relay/calling/component.rb', line 96

def has_blocker?
  !@blocker.nil?
end

#inner_paramsObject



41
42
43
44
45
46
47
48
# File 'lib/signalwire/relay/calling/component.rb', line 41

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)


18
19
20
# File 'lib/signalwire/relay/calling/component.rb', line 18

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)


102
103
104
105
106
107
108
109
# File 'lib/signalwire/relay/calling/component.rb', line 102

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



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

def payload
  nil
end

#setup_handlersObject



57
58
59
60
61
# File 'lib/signalwire/relay/calling/component.rb', line 57

def setup_handlers
  @call.on :event, event_type: event_type do |evt|
    notification_handler(evt)
  end
end

#setup_waiting_events(events) ⇒ Object



81
82
83
# File 'lib/signalwire/relay/calling/component.rb', line 81

def setup_waiting_events(events)
  @events_waiting = events
end

#terminate(event = nil) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/signalwire/relay/calling/component.rb', line 73

def terminate(event = nil)
  @completed = true
  @successful = false
  @state = 'failed'
  @event = event if event
  blocker&.reject false
end

#unblock(value) ⇒ Object



115
116
117
# File 'lib/signalwire/relay/calling/component.rb', line 115

def unblock(value)
  blocker&.resolve value if has_blocker? && blocker.pending?
end

#wait_for(*events) ⇒ Object



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

def wait_for(*events)
  setup_waiting_events(events)
  execute
  wait_on_blocker unless @completed
end

#wait_on_blockerObject



91
92
93
94
# File 'lib/signalwire/relay/calling/component.rb', line 91

def wait_on_blocker
  create_blocker
  blocker.wait
end