Class: Tina4::WebSocketRoute
- Inherits:
-
Object
- Object
- Tina4::WebSocketRoute
- Defined in:
- lib/tina4/router.rb
Overview
A registered WebSocket route with path pattern matching (reuses Route's compile logic)
Instance Attribute Summary collapse
-
#auth_required ⇒ Object
PUBLIC by default (mirrors GET).
-
#handler ⇒ Object
readonly
Returns the value of attribute handler.
-
#param_names ⇒ Object
readonly
Returns the value of attribute param_names.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#path_regex ⇒ Object
readonly
Returns the value of attribute path_regex.
Instance Method Summary collapse
-
#initialize(path, handler, auth_required: false) ⇒ WebSocketRoute
constructor
A new instance of WebSocketRoute.
-
#match?(request_path) ⇒ Boolean
Returns params hash if matched, false otherwise.
-
#no_auth ⇒ Object
Opt back out (the default).
-
#secure ⇒ Object
Mark this WebSocket route as requiring bearer-token auth on the upgrade.
Constructor Details
#initialize(path, handler, auth_required: false) ⇒ WebSocketRoute
Returns a new instance of WebSocketRoute.
229 230 231 232 233 234 235 236 |
# File 'lib/tina4/router.rb', line 229 def initialize(path, handler, auth_required: false) @path = normalize_path(path).freeze @handler = handler @auth_required = auth_required @param_names = [] @path_regex = compile_pattern(@path) @param_names.freeze end |
Instance Attribute Details
#auth_required ⇒ Object
PUBLIC by default (mirrors GET). Flip to true with #secure (or via Tina4.secure_websocket) to require a valid JWT on the upgrade. Mirrors the HTTP Route's auth_required so the upgrade path enforces it identically.
227 228 229 |
# File 'lib/tina4/router.rb', line 227 def auth_required @auth_required end |
#handler ⇒ Object (readonly)
Returns the value of attribute handler.
223 224 225 |
# File 'lib/tina4/router.rb', line 223 def handler @handler end |
#param_names ⇒ Object (readonly)
Returns the value of attribute param_names.
223 224 225 |
# File 'lib/tina4/router.rb', line 223 def param_names @param_names end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
223 224 225 |
# File 'lib/tina4/router.rb', line 223 def path @path end |
#path_regex ⇒ Object (readonly)
Returns the value of attribute path_regex.
223 224 225 |
# File 'lib/tina4/router.rb', line 223 def path_regex @path_regex end |
Instance Method Details
#match?(request_path) ⇒ Boolean
Returns params hash if matched, false otherwise
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/tina4/router.rb', line 252 def match?(request_path) match = @path_regex.match(request_path) return false unless match if @param_names.empty? {} else params = {} @param_names.each_with_index do |param_def, i| raw_value = match[i + 1] # Same ASCII-8BIT -> UTF-8 relabel as the HTTP Route matcher above, # so a WS path param binds to SQL as TEXT (not a BLOB). raw_value = raw_value.dup.force_encoding(Encoding::UTF_8) if raw_value.is_a?(::String) params[param_def[:name]] = raw_value end params end end |
#no_auth ⇒ Object
Opt back out (the default). Returns self for chaining.
246 247 248 249 |
# File 'lib/tina4/router.rb', line 246 def no_auth @auth_required = false self end |
#secure ⇒ Object
Mark this WebSocket route as requiring bearer-token auth on the upgrade. Returns self for chaining: Tina4::Router.websocket("/chat") { ... }.secure
240 241 242 243 |
# File 'lib/tina4/router.rb', line 240 def secure @auth_required = true self end |