Class: Terminalwire::V2::Rails::VersionEndpoint

Inherits:
Object
  • Object
show all
Defined in:
lib/terminalwire/v2/rails.rb

Overview

Routes a WebSocket upgrade to a handler by the version subprotocol it advertises, falling back to default (v2) when none matches — the forward-compatible seam for registering future protocol versions. Same Rack-endpoint shape as Dispatcher, so Rails match to: hands the connection off correctly in production.

Instance Method Summary collapse

Constructor Details

#initialize(default:, by_subprotocol: {}) ⇒ VersionEndpoint

Returns a new instance of VersionEndpoint.



291
292
293
294
# File 'lib/terminalwire/v2/rails.rb', line 291

def initialize(default:, by_subprotocol: {})
  @default = default
  @by_subprotocol = by_subprotocol
end

Instance Method Details

#call(env) ⇒ Object



296
297
298
299
300
# File 'lib/terminalwire/v2/rails.rb', line 296

def call(env)
  protos = env["HTTP_SEC_WEBSOCKET_PROTOCOL"].to_s.split(/,\s*/)
  handler = protos.lazy.filter_map { |proto| @by_subprotocol[proto] }.first || @default
  handler.call(env)
end