Class: Signalwire::Relay::Client
- Inherits:
-
Object
- Object
- Signalwire::Relay::Client
- Includes:
- Blade::EventHandler, Common, Logger
- Defined in:
- lib/signalwire/relay/client.rb
Instance Attribute Summary collapse
-
#connected ⇒ Object
Returns the value of attribute connected.
-
#host ⇒ Object
Returns the value of attribute host.
-
#project ⇒ Object
Returns the value of attribute project.
-
#protocol ⇒ Object
Returns the value of attribute protocol.
-
#session ⇒ Object
Returns the value of attribute session.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
- #calling ⇒ Object
- #clean_up_space_url(space_url) ⇒ Object
-
#connect! ⇒ Object
Starts the client connection.
- #contexts ⇒ Object
-
#disconnect! ⇒ Object
Terminates the session.
- #execute(command, &block) ⇒ Object
-
#initialize(project:, token:, host: nil) ⇒ Client
constructor
Creates a Relay client.
- #messaging ⇒ Object
-
#relay_execute(command, timeout = Signalwire::Relay::COMMAND_TIMEOUT, &block) ⇒ Object
TODO: refactor this for style.
- #setup_context(context) ⇒ Object
Methods included from Blade::EventHandler
Methods included from Logger
Constructor Details
#initialize(project:, token:, host: nil) ⇒ Client
Creates a Relay client
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
#connected ⇒ Object
Returns the value of attribute connected.
9 10 11 |
# File 'lib/signalwire/relay/client.rb', line 9 def connected @connected end |
#host ⇒ Object
Returns the value of attribute host.
9 10 11 |
# File 'lib/signalwire/relay/client.rb', line 9 def host @host end |
#project ⇒ Object
Returns the value of attribute project.
9 10 11 |
# File 'lib/signalwire/relay/client.rb', line 9 def project @project end |
#protocol ⇒ Object
Returns the value of attribute protocol.
9 10 11 |
# File 'lib/signalwire/relay/client.rb', line 9 def protocol @protocol end |
#session ⇒ Object
Returns the value of attribute session.
9 10 11 |
# File 'lib/signalwire/relay/client.rb', line 9 def session @session end |
#url ⇒ Object
Returns the value of attribute url.
9 10 11 |
# File 'lib/signalwire/relay/client.rb', line 9 def url @url end |
Instance Method Details
#calling ⇒ Object
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 |
#contexts ⇒ Object
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 |
#messaging ⇒ Object
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) = 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: #{}" unless success else logger.error("Relay error occurred, code #{event.error_code}: #{event.}") 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 |