Class: Rain::Router
- Inherits:
-
Object
- Object
- Rain::Router
- Includes:
- LowType, Observers
- Defined in:
- lib/router/router.rb
Instance Attribute Summary collapse
-
#routes ⇒ Object
readonly
Returns the value of attribute routes.
-
#trie ⇒ Object
readonly
Returns the value of attribute trie.
Instance Method Summary collapse
- #delete(path, &block) ⇒ Object
- #get(path, &block) ⇒ Object
-
#handle(event:) ⇒ Object
TODO: Define type: ::Low::Events::RequestEvent You can override any route/status simply by adding your own observer.
-
#initialize ⇒ Router
constructor
A new instance of Router.
- #post(path, &block) ⇒ Object
- #route(path, verbs = [], &block) ⇒ Object
- #update(path, &block) ⇒ Object
Constructor Details
Instance Attribute Details
#routes ⇒ Object (readonly)
Returns the value of attribute routes.
16 17 18 |
# File 'lib/router/router.rb', line 16 def routes @routes end |
#trie ⇒ Object (readonly)
Returns the value of attribute trie.
16 17 18 |
# File 'lib/router/router.rb', line 16 def trie @trie end |
Instance Method Details
#delete(path, &block) ⇒ Object
49 50 51 |
# File 'lib/router/router.rb', line 49 def delete(path, &block) route(path, 'DELETE', &block) end |
#get(path, &block) ⇒ Object
37 38 39 |
# File 'lib/router/router.rb', line 37 def get(path, &block) route(path, 'GET', &block) end |
#handle(event:) ⇒ Object
TODO: Define type: ::Low::Events::RequestEvent You can override any route/status simply by adding your own observer.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/router/router.rb', line 55 def handle(event:) response_event = nil # The last route event will render a response event which we want to return to the request event. @trie.match(path: event.request.path.delete_suffix('/')).each do |route_event| response_event = route_event.trigger end return response_event if response_event if @routes['/*'] route = Route.new(path: event.request.path, verbs: @routes['/*'].verbs) wildcard_event = WildcardRouteEvent.trigger(key: '/*', action: :render, route:) return wildcard_event if wildcard_event end Low::Events::StatusEvent.trigger(status: Low::Types::Status[404], request: event.request) end |
#post(path, &block) ⇒ Object
41 42 43 |
# File 'lib/router/router.rb', line 41 def post(path, &block) route(path, 'POST', &block) end |
#route(path, verbs = [], &block) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/router/router.rb', line 24 def route(path, verbs = [], &block) @breadcrumbs << path path = @breadcrumbs.join route = Route.new(path:, verbs: [*verbs]) @routes[path] = route @trie.merge(route:) block.call if block_given? @breadcrumbs.pop end |
#update(path, &block) ⇒ Object
45 46 47 |
# File 'lib/router/router.rb', line 45 def update(path, &block) route(path, 'UPDATE', &block) end |