Class: Tina4::Route

Inherits:
Object
  • Object
show all
Defined in:
lib/tina4/router.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, path, handler, auth_handler: nil, swagger_meta: {}) ⇒ Route

Returns a new instance of Route.



7
8
9
10
11
12
13
14
15
16
# File 'lib/tina4/router.rb', line 7

def initialize(method, path, handler, auth_handler: nil, swagger_meta: {})
  @method = method.to_s.upcase.freeze
  @path = normalize_path(path).freeze
  @handler = handler
  @auth_handler = auth_handler
  @swagger_meta = swagger_meta
  @param_names = []
  @path_regex = compile_pattern(@path)
  @param_names.freeze
end

Instance Attribute Details

#auth_handlerObject (readonly)

Returns the value of attribute auth_handler.



5
6
7
# File 'lib/tina4/router.rb', line 5

def auth_handler
  @auth_handler
end

#handlerObject (readonly)

Returns the value of attribute handler.



5
6
7
# File 'lib/tina4/router.rb', line 5

def handler
  @handler
end

#methodObject (readonly)

Returns the value of attribute method.



5
6
7
# File 'lib/tina4/router.rb', line 5

def method
  @method
end

#param_namesObject (readonly)

Returns the value of attribute param_names.



5
6
7
# File 'lib/tina4/router.rb', line 5

def param_names
  @param_names
end

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/tina4/router.rb', line 5

def path
  @path
end

#path_regexObject (readonly)

Returns the value of attribute path_regex.



5
6
7
# File 'lib/tina4/router.rb', line 5

def path_regex
  @path_regex
end

#swagger_metaObject (readonly)

Returns the value of attribute swagger_meta.



5
6
7
# File 'lib/tina4/router.rb', line 5

def swagger_meta
  @swagger_meta
end

Instance Method Details

#match_path(request_path) ⇒ Object

Returns params hash if matched, false otherwise



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/tina4/router.rb', line 19

def match_path(request_path)
  match = @path_regex.match(request_path)
  return false unless match

  if @param_names.empty?
    # Static route — no params to extract
    {}
  else
    params = {}
    @param_names.each_with_index do |param_def, i|
      raw_value = match[i + 1]
      params[param_def[:name]] = cast_param(raw_value, param_def[:type])
    end
    params
  end
end