Class: Rubee::Router
Constant Summary collapse
- HTTP_METHODS =
%i[get post put patch delete head connect options trace].freeze
Instance Attribute Summary collapse
-
#request ⇒ Object
readonly
Returns the value of attribute request.
-
#routes ⇒ Object
readonly
Returns the value of attribute routes.
Class Method Summary collapse
- .draw {|_self| ... } ⇒ Object
- .route_for(request) ⇒ Object
- .set_route(path, to:, method: __method__, **args) ⇒ Object
Instance Attribute Details
#request ⇒ Object (readonly)
Returns the value of attribute request.
7 8 9 |
# File 'lib/rubee/router.rb', line 7 def request @request end |
#routes ⇒ Object (readonly)
Returns the value of attribute routes.
7 8 9 |
# File 'lib/rubee/router.rb', line 7 def routes @routes end |
Class Method Details
.draw {|_self| ... } ⇒ Object
12 13 14 |
# File 'lib/rubee/router.rb', line 12 def draw yield(self) if block_given? end |
.route_for(request) ⇒ Object
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/rubee/router.rb', line 16 def route_for(request) method = (request.params['_method'] || request.request_method).downcase.to_sym @routes.find do |route| return route if request.path == route[:path] && request.request_method&.downcase&.to_sym == route[:method] pattern = route[:path].gsub(/{.*?}/, '([^/]+)') regex = /^#{pattern}$/ regex.match?(request.path) && method.to_s == route[:method].to_s end end |
.set_route(path, to:, method: __method__, **args) ⇒ Object
27 28 29 30 31 |
# File 'lib/rubee/router.rb', line 27 def set_route(path, to:, method: __method__, **args) controller, action = to.split('#') @routes.delete_if { |route| route[:path] == path && route[:method] == method } @routes << { path:, controller:, action:, method:, **args } end |