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.
222 223 224 225 226 227 228 229 |
# File 'lib/tina4/router.rb', line 222 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.
220 221 222 |
# File 'lib/tina4/router.rb', line 220 def auth_required @auth_required end |
#handler ⇒ Object (readonly)
Returns the value of attribute handler.
216 217 218 |
# File 'lib/tina4/router.rb', line 216 def handler @handler end |
#param_names ⇒ Object (readonly)
Returns the value of attribute param_names.
216 217 218 |
# File 'lib/tina4/router.rb', line 216 def param_names @param_names end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
216 217 218 |
# File 'lib/tina4/router.rb', line 216 def path @path end |
#path_regex ⇒ Object (readonly)
Returns the value of attribute path_regex.
216 217 218 |
# File 'lib/tina4/router.rb', line 216 def path_regex @path_regex end |
Instance Method Details
#match?(request_path) ⇒ Boolean
Returns params hash if matched, false otherwise
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/tina4/router.rb', line 245 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] params[param_def[:name]] = raw_value end params end end |
#no_auth ⇒ Object
Opt back out (the default). Returns self for chaining.
239 240 241 242 |
# File 'lib/tina4/router.rb', line 239 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
233 234 235 236 |
# File 'lib/tina4/router.rb', line 233 def secure @auth_required = true self end |