Class: Router

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

Instance Method Summary collapse

Constructor Details

#initializeRouter

Returns a new instance of Router.



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

def initialize
  @routes = { GET: {}, POST: {}, PUT: {}, DELETE: {} }
end

Instance Method Details

#add_route(method, path, accepted_queries: nil, accepted_body: nil, accepted_response: nil, &block) ⇒ Object



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

def add_route(method, path, accepted_queries: nil, accepted_body: nil, accepted_response: nil, &block)
  @routes[method][path] = {
    accepted_queries: accepted_queries,
    accepted_body: accepted_body,
    accepted_response: accepted_response,
    handler: block
  }
end

#call(path, method, env, full_path) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/TeleRuby/router.rb', line 26

def call(path, method, env, full_path)
  log_request(method, full_path)

  unless @routes.key?(method)
    log_response(405, "Method Not Allowed")
    return json_response(405, { error: "Method Not Allowed" })
  end

  @routes[method].each do |route_path, route|
    match = match_path(route_path, path)

    if match
      # If there's a match, extract URL parameters (if any)
      if match.is_a?(MatchData)
        env["url_params"] = match.named_captures || {}
      else
        env["url_params"] = {}
      end

      # Validate query parameters
      if route[:accepted_queries] && !validate_queries(route[:accepted_queries], env["query_params"])
        log_response(400, "Invalid Query Parameters")
        return json_response(400, { error: "Invalid Query Parameters" })
      end

      # Validate body parameters
      if route[:accepted_body] && !validate_body(route[:accepted_body], env["parsed_body"])
        log_response(400, "Invalid Body Parameters")
        return json_response(400, { error: "Invalid Body Parameters" })
      end

      # Call the handler block
      result = route[:handler].call(env)

      # Ensure result is an HTTPResponse and validate it
      if result.is_a?(HTTPResponse)
        if route[:accepted_response] && !validate_accepted_response(route[:accepted_response], result.body)
          log_response(500, "Invalid Response Structure")
          return json_response(500, { error: "Invalid Response Structure" })
        end

        log_response(result.status, result.body[0] || result.body[0])

        return result.to_a # Convert HTTPResponse to Rack-compatible response
      else
        log_response(500, "Handler did not return an HTTPResponse")
        return json_response(500, { error: "Handler must return an HTTPResponse" })
      end
    end
  end

  # If no match is found, return 404
  log_response(404, "Not Found")
  json_response(404, { error: "Not Found" })
end

#get(path, accepted_queries: nil, accepted_body: nil, accepted_response: nil, &block) ⇒ Object



18
19
20
# File 'lib/TeleRuby/router.rb', line 18

def get(path, accepted_queries: nil, accepted_body: nil, accepted_response: nil, &block)
  add_route(:GET, path, accepted_queries: accepted_queries, accepted_body: accepted_body, accepted_response: accepted_response, &block)
end

#post(path, accepted_queries: nil, accepted_body: nil, accepted_response: nil, &block) ⇒ Object



22
23
24
# File 'lib/TeleRuby/router.rb', line 22

def post(path, accepted_queries: nil, accepted_body: nil, accepted_response: nil, &block)
  add_route(:POST, path, accepted_queries: accepted_queries, accepted_body: accepted_body, accepted_response: accepted_response, &block)
end