Module: Tina4::Router

Defined in:
lib/tina4/router.rb

Defined Under Namespace

Classes: GroupContext

Class Method Summary collapse

Class Method Details

.add(method, path, handler, auth_handler: nil, swagger_meta: {}, middleware: [], template: nil) ⇒ Object



261
262
263
264
265
266
267
268
269
270
271
# File 'lib/tina4/router.rb', line 261

def add(method, path, handler, auth_handler: nil, swagger_meta: {}, middleware: [], template: nil)
  route = Route.new(method, path, handler,
                    auth_handler: auth_handler,
                    swagger_meta: swagger_meta,
                    middleware: middleware,
                    template: template)
  routes << route
  method_index[route.method] << route
  Tina4::Log.debug("Route registered: #{method.upcase} #{path}")
  route
end

.any(path, middleware: [], swagger_meta: {}, template: nil, &block) ⇒ Object



293
294
295
# File 'lib/tina4/router.rb', line 293

def any(path, middleware: [], swagger_meta: {}, template: nil, &block)
  add("ANY", path, block, middleware: middleware, swagger_meta: swagger_meta, template: template)
end

.clear!Object Also known as: clear



335
336
337
338
339
# File 'lib/tina4/router.rb', line 335

def clear!
  @routes = []
  @method_index = Hash.new { |h, k| h[k] = [] }
  @ws_routes = []
end

.delete(path, middleware: [], swagger_meta: {}, template: nil, &block) ⇒ Object



289
290
291
# File 'lib/tina4/router.rb', line 289

def delete(path, middleware: [], swagger_meta: {}, template: nil, &block)
  add("DELETE", path, block, middleware: middleware, swagger_meta: swagger_meta, template: template)
end

.find_route(method, path) ⇒ Object



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/tina4/router.rb', line 297

def find_route(method, path)
  normalized_method = method.upcase
  # Normalize path once (not per-route)
  normalized_path = path.gsub("\\", "/")
  normalized_path = "/#{normalized_path}" unless normalized_path.start_with?("/")
  normalized_path = normalized_path.chomp("/") unless normalized_path == "/"

  # Check ANY routes first, then method-specific routes
  candidates = (method_index["ANY"] || []) + (method_index[normalized_method] || [])
  candidates.each do |route|
    params = route.match_path(normalized_path)
    return [route, params] if params
  end
  nil
end

.find_ws_route(path) ⇒ Object

Find a matching WebSocket route for a given path. Returns [ws_route, params] or nil.



244
245
246
247
248
249
250
251
252
253
254
# File 'lib/tina4/router.rb', line 244

def find_ws_route(path)
  normalized = path.gsub("\\", "/")
  normalized = "/#{normalized}" unless normalized.start_with?("/")
  normalized = normalized.chomp("/") unless normalized == "/"

  ws_routes.each do |ws_route|
    params = ws_route.match?(normalized)
    return [ws_route, params] if params
  end
  nil
end

.get(path, middleware: [], swagger_meta: {}, template: nil, &block) ⇒ Object

Convenience registration methods



273
274
275
# File 'lib/tina4/router.rb', line 273

def get(path, middleware: [], swagger_meta: {}, template: nil, &block)
  add("GET", path, block, middleware: middleware, swagger_meta: swagger_meta, template: template)
end

.get_routesObject



212
213
214
# File 'lib/tina4/router.rb', line 212

def get_routes
  routes
end

.get_web_socket_routesObject

Parity alias — returns all registered WebSocket routes.



226
227
228
# File 'lib/tina4/router.rb', line 226

def get_web_socket_routes
  ws_routes
end

.group(prefix, auth_handler: nil, middleware: [], &block) ⇒ Object



342
343
344
# File 'lib/tina4/router.rb', line 342

def group(prefix, auth_handler: nil, middleware: [], &block)
  GroupContext.new(prefix, auth_handler, middleware).instance_eval(&block)
end

.list_routesObject



216
217
218
# File 'lib/tina4/router.rb', line 216

def list_routes
  routes
end

.load_routes(directory) ⇒ Object

Load route files from a directory (file-based route discovery)



347
348
349
350
351
352
353
354
355
356
357
# File 'lib/tina4/router.rb', line 347

def load_routes(directory)
  return unless Dir.exist?(directory)
  Dir.glob(File.join(directory, "**/*.rb")).sort.each do |file|
    begin
      load file
      Tina4::Log.debug("Route loaded: #{file}")
    rescue => e
      Tina4::Log.error("Failed to load route #{file}: #{e.message}")
    end
  end
end

.match(method, path) ⇒ Object

Find a route matching method + path. Returns [route, params] or nil. match(method, path) — consistent with Python, PHP, and Node.



315
316
317
# File 'lib/tina4/router.rb', line 315

def match(method, path)
  find_route(method, path)
end

.method_indexObject

Routes indexed by HTTP method for O(1) method lookup



257
258
259
# File 'lib/tina4/router.rb', line 257

def method_index
  @method_index ||= Hash.new { |h, k| h[k] = [] }
end

.patch(path, middleware: [], swagger_meta: {}, template: nil, &block) ⇒ Object



285
286
287
# File 'lib/tina4/router.rb', line 285

def patch(path, middleware: [], swagger_meta: {}, template: nil, &block)
  add("PATCH", path, block, middleware: middleware, swagger_meta: swagger_meta, template: template)
end

.post(path, middleware: [], swagger_meta: {}, template: nil, &block) ⇒ Object



277
278
279
# File 'lib/tina4/router.rb', line 277

def post(path, middleware: [], swagger_meta: {}, template: nil, &block)
  add("POST", path, block, middleware: middleware, swagger_meta: swagger_meta, template: template)
end

.put(path, middleware: [], swagger_meta: {}, template: nil, &block) ⇒ Object



281
282
283
# File 'lib/tina4/router.rb', line 281

def put(path, middleware: [], swagger_meta: {}, template: nil, &block)
  add("PUT", path, block, middleware: middleware, swagger_meta: swagger_meta, template: template)
end

.routesObject



208
209
210
# File 'lib/tina4/router.rb', line 208

def routes
  @routes ||= []
end

.use(klass) ⇒ Object

Register a class-based middleware globally. The class should define static before_* and/or after_* methods. Example:

class AuthMiddleware
  def self.before_auth(request, response)
    unless request.headers["authorization"]
      return [request, response.json({ error: "Unauthorized" }, 401)]
    end
    [request, response]
  end
end
Tina4::Router.use(AuthMiddleware)


331
332
333
# File 'lib/tina4/router.rb', line 331

def use(klass)
  Tina4::Middleware.use(klass)
end

.websocket(path, &block) ⇒ Object

Register a WebSocket route. The handler block receives (connection, event, data) where:

connection — WebSocketConnection with #send, #broadcast, #close, #params
event      — :open, :message, or :close
data       — String payload for :message, nil for :open/:close


235
236
237
238
239
240
# File 'lib/tina4/router.rb', line 235

def websocket(path, &block)
  ws_route = WebSocketRoute.new(path, block)
  ws_routes << ws_route
  Tina4::Log.debug("WebSocket route registered: #{path}")
  ws_route
end

.ws_routesObject

Registered WebSocket routes



221
222
223
# File 'lib/tina4/router.rb', line 221

def ws_routes
  @ws_routes ||= []
end