Class: Tina4::Route
- Inherits:
-
Object
- Object
- Tina4::Route
- Defined in:
- lib/tina4/router.rb
Instance Attribute Summary collapse
-
#auth_handler ⇒ Object
readonly
Returns the value of attribute auth_handler.
-
#handler ⇒ Object
readonly
Returns the value of attribute handler.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#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.
-
#swagger_meta ⇒ Object
readonly
Returns the value of attribute swagger_meta.
Instance Method Summary collapse
-
#initialize(method, path, handler, auth_handler: nil, swagger_meta: {}) ⇒ Route
constructor
A new instance of Route.
-
#match_path(request_path) ⇒ Object
Returns params hash if matched, false otherwise.
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 = @param_names = [] @path_regex = compile_pattern(@path) @param_names.freeze end |
Instance Attribute Details
#auth_handler ⇒ Object (readonly)
Returns the value of attribute auth_handler.
5 6 7 |
# File 'lib/tina4/router.rb', line 5 def auth_handler @auth_handler end |
#handler ⇒ Object (readonly)
Returns the value of attribute handler.
5 6 7 |
# File 'lib/tina4/router.rb', line 5 def handler @handler end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
5 6 7 |
# File 'lib/tina4/router.rb', line 5 def method @method end |
#param_names ⇒ Object (readonly)
Returns the value of attribute param_names.
5 6 7 |
# File 'lib/tina4/router.rb', line 5 def param_names @param_names end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
5 6 7 |
# File 'lib/tina4/router.rb', line 5 def path @path end |
#path_regex ⇒ Object (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_meta ⇒ Object (readonly)
Returns the value of attribute swagger_meta.
5 6 7 |
# File 'lib/tina4/router.rb', line 5 def @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 |