Class: Signalwire::Relay::Client

Inherits:
Object
  • Object
show all
Includes:
Blade::EventHandler, Common, Logger
Defined in:
lib/signalwire/relay/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Blade::EventHandler

#broadcast

Methods included from Logger

#level=, #logger, logger

Constructor Details

#initialize(project:, token:, host: nil) ⇒ Client

Creates a Relay client

Parameters:

  • project (String)

    Your SignalWire project identifier

  • token (String)

    Your SignalWire secret token

  • SIGNALWIRE_HOST (String)

    Your SignalWire space URL (not needed for production usage)



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/signalwire/relay/client.rb', line 17

def initialize(project:, token:, host: nil)
  @project = project
  @token = token
  @host = host || ENV.fetch('SIGNALWIRE_HOST', Signalwire::Relay::DEFAULT_URL)
  @url = clean_up_space_url(@host)
  @protocol = nil

  @connected = false

  setup_session
  setup_handlers
  setup_events
end

Instance Attribute Details

#connectedObject

Returns the value of attribute connected.



9
10
11
# File 'lib/signalwire/relay/client.rb', line 9

def connected
  @connected
end

#hostObject

Returns the value of attribute host.



9
10
11
# File 'lib/signalwire/relay/client.rb', line 9

def host
  @host
end

#projectObject

Returns the value of attribute project.



9
10
11
# File 'lib/signalwire/relay/client.rb', line 9

def project
  @project
end

#protocolObject

Returns the value of attribute protocol.



9
10
11
# File 'lib/signalwire/relay/client.rb', line 9

def protocol
  @protocol
end

#sessionObject

Returns the value of attribute session.



9
10
11
# File 'lib/signalwire/relay/client.rb', line 9

def session
  @session
end

#urlObject

Returns the value of attribute url.



9
10
11
# File 'lib/signalwire/relay/client.rb', line 9

def url
  @url
end

Instance Method Details

#callingObject



91
92
93
# File 'lib/signalwire/relay/client.rb', line 91

def calling
  @calling ||= Signalwire::Relay::Calling::Instance.new(self)
end

#clean_up_space_url(space_url) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/signalwire/relay/client.rb', line 44

def clean_up_space_url(space_url)
  uri = URI.parse(space_url)
  # oddly, URI.parse interprets a simple hostname as a path
  if uri.scheme.nil? && uri.host.nil?
    unless uri.path.nil?
      uri.scheme = 'wss'
      uri.host = uri.path
      uri.path = ''
    end
  end

  uri.to_s
end

#connect!Object

Starts the client connection



33
34
35
36
# File 'lib/signalwire/relay/client.rb', line 33

def connect!
  logger.debug "Connecting to #{@url}"
  session.connect!
end

#contextsObject



114
115
116
# File 'lib/signalwire/relay/client.rb', line 114

def contexts
  @contexts ||= Concurrent::Array.new
end

#disconnect!Object

Terminates the session



40
41
42
# File 'lib/signalwire/relay/client.rb', line 40

def disconnect!
  session.disconnect!
end

#execute(command, &block) ⇒ Object



58
59
60
# File 'lib/signalwire/relay/client.rb', line 58

def execute(command, &block)
  @session.execute(command, &block)
end

#messagingObject



95
96
97
# File 'lib/signalwire/relay/client.rb', line 95

def messaging
  @messaging ||= Signalwire::Relay::Messaging::Instance.new(self)
end

#relay_execute(command, timeout = Signalwire::Relay::COMMAND_TIMEOUT, &block) ⇒ Object

TODO: refactor this for style



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/signalwire/relay/client.rb', line 63

def relay_execute(command, timeout = Signalwire::Relay::COMMAND_TIMEOUT, &block)
  promise = Concurrent::Promises.resolvable_future

  execute(command) do |event|
    promise.fulfill event
  end

  promise.wait timeout

  if promise.fulfilled?
    event = promise.value
    code = event.dig(:result, :result, :code)
    message = event.dig(:result, :result, :message)
    success = code == '200' ? :success : :failure

    if code
      block.call(event, success) if block_given?
      logger.error "Relay command failed with code #{code} and message: #{message}" unless success
    else
      logger.error("Relay error occurred, code #{event.error_code}: #{event.error_message}") if event.error?
      block.call(event, :failure) if block_given?
    end
  else
    logger.error 'Unknown Relay command failure, command timed out'
    block.call(event, :failure) if block_given?
  end
end

#setup_context(context) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/signalwire/relay/client.rb', line 99

def setup_context(context)
  return if contexts.include?(context)
  receive_command = {
    protocol: @protocol,
    method: 'signalwire.receive',
    params: {
      context: context
    }
  }

  relay_execute receive_command do
    contexts << context
  end
end