Class: Wouter
- Inherits:
-
Object
- Object
- Wouter
- Defined in:
- lib/wouter.rb
Defined Under Namespace
Classes: Endpoint, Request, Response
Class Method Summary collapse
-
.call(env) ⇒ Object
Rack API.
-
.not_found ⇒ Object
Helpers.
-
.routes ⇒ Object
Internal Data.
Class Method Details
.call(env) ⇒ Object
Rack API
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/wouter.rb', line 68 def self.call(env) request = Request.new(env) route_params = {} route = routes.find do |route| if route[:method] == request.request_method.to_sym if route[:path].include?(":") split_path = route[:path].split("/") # Find all the named parameters in the route, drop the ":" so we have the names: ":id" => "id" route_param_names = split_path.find_all { |s| s.size > 1 ? s[0] == ":" : false }.map { |s| s[1..-1] } # Turn the route into a regex: "/hello/:name" => "\/hello\/(\w*)" path_regex_string = split_path.map { |s| s[0] == ":" ? "(\\w*)" : s }.join("\/") r = Regexp.new(path_regex_string) if r.match?(request.path) match_data = r.match(request.path) # Match the match data with the named params, ex { "id" => 123 } route_param_names.each_with_index do |n, i| route_params[n] = match_data[i+1] end true else false end else route[:path] == request.path end end end if route route_params.each do |k, v| request.update_param(k, v) end response = Response.new rack_response = route[:route_class].new.call(request, response) rack_response.finish else not_found.finish end end |
.not_found ⇒ Object
Helpers
114 115 116 117 118 119 |
# File 'lib/wouter.rb', line 114 def self.not_found resp = Response.new resp.status = 404 resp.write "Not Found" resp end |
.routes ⇒ Object
Internal Data
48 49 50 |
# File 'lib/wouter.rb', line 48 def self.routes @routes ||= [] end |