Finished routing, simple website now works yay

This commit is contained in:
Hannah-Dagemark 2024-04-18 16:01:30 +02:00
commit 7a112697ec
11 changed files with 148 additions and 15 deletions

View file

@ -1,20 +1,34 @@
class Route
require 'debug'
class Router
attr_reader :routes
def initialize()
@routes = {}
@routes = []
end
def add_route(input)
@routes.merge!(input)
def add_route(method, input, &block)
routeHash = {method: method, resource: input, block: block}
@routes << routeHash
p "added successfully, @routes are: #{@routes}"
end
def match_route(input)
resource = input.resource
if @routes[resource]
return @routes[resource]
def match_route(request)
#spec ["3" "bostongurka"]
match = @routes.find {|route| (route[:method] == request.method) && (request.resource.match?(route[:resource]))}
if match != nil
print("\nMatch: ", match, "\n")
return 200
else
p "failed to find: #{resource} route"
if request.resource.match?(@routes[0][:resource]) != true
p "failed to find: #{request.resource} resource.\nExpected: #{String(@routes[0][:resource])} resource"
elsif @routes[0][:method] != request.method
p "failed to find #{request.method} method.\nExpected: #{@routes[0][:method]} method"
else
p "Failed to match, got no reason, just kinda didn't feel like it"
end
return 404
end
end