wouter 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/wouter.rb +67 -40
- metadata +27 -28
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4731ee9735261f806ded54813c9655fcae91349f6c3b9a5061f0664ac2618b72
|
4
|
+
data.tar.gz: 0bb29c4009bcc7846e07dfad0dae5c8c0ba4af3ceaa5bf1607ca4f89590e3e59
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 81a9adf9928ce8af300ede523ed9fad8ac9c657ccee665fae108b4f13d54c3fa77a11c61cefdd3d4bceae992d4f68e8cf2e65e6f103dafd0b2287ca2e310fb1a
|
7
|
+
data.tar.gz: ad519803a201b62cbb9e9b1d93365329a74d0517859dd60fff1ca8b10088d86788baad5d9d5903aa04df8dbdd423bcf26f88433e3efd1f9e334ece9037ee59c0
|
data/lib/wouter.rb
CHANGED
@@ -1,12 +1,11 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'bundler'
|
2
4
|
Bundler.setup(:default)
|
3
|
-
require
|
5
|
+
require 'rack'
|
4
6
|
|
7
|
+
# Wouter main class to define routes from
|
5
8
|
class Wouter
|
6
|
-
# ========== Rack wrappers
|
7
|
-
class Request < Rack::Request; end
|
8
|
-
class Response < Rack::Response; end
|
9
|
-
|
10
9
|
# ========== Endpoint class to make life easier
|
11
10
|
class Endpoint
|
12
11
|
attr_accessor :req, :res
|
@@ -21,7 +20,7 @@ class Wouter
|
|
21
20
|
def self.call(env)
|
22
21
|
endpoint = new(Rack::Request.new(env), Rack::Response.new)
|
23
22
|
final_resp = endpoint.respond
|
24
|
-
# Return a `Rack::Response` or write to the body of the response
|
23
|
+
# Return a `Rack::Response` or write to the body of the response
|
25
24
|
if final_resp.is_a?(Rack::Response)
|
26
25
|
final_resp
|
27
26
|
else
|
@@ -68,11 +67,12 @@ class Wouter
|
|
68
67
|
class <<self
|
69
68
|
%i[get post put delete].each do |m|
|
70
69
|
define_method(m) do |path, route_class|
|
71
|
-
|
70
|
+
route = {
|
72
71
|
method: m.to_s.upcase.to_sym,
|
73
72
|
path: path,
|
74
73
|
route_class: route_class
|
75
|
-
}
|
74
|
+
}
|
75
|
+
routes.push(route)
|
76
76
|
end
|
77
77
|
end
|
78
78
|
|
@@ -88,6 +88,7 @@ class Wouter
|
|
88
88
|
app.to_app
|
89
89
|
end
|
90
90
|
|
91
|
+
# Wrapper class for creating single `.call` interface for `Rack::Builder`
|
91
92
|
class Wrapper
|
92
93
|
attr_reader :routes
|
93
94
|
|
@@ -98,58 +99,84 @@ class Wouter
|
|
98
99
|
def call(env)
|
99
100
|
request = Rack::Request.new(env)
|
100
101
|
|
101
|
-
route = routes.find do |
|
102
|
-
|
103
|
-
if parameterized_path?(route[:path])
|
104
|
-
split_path = route[:path].split("/")
|
105
|
-
path_regex = build_path_regex(split_path)
|
106
|
-
path_regex.match?(request.path)
|
107
|
-
else
|
108
|
-
route[:path] == request.path
|
109
|
-
end
|
110
|
-
end
|
102
|
+
route = routes.find do |r|
|
103
|
+
matching_route?(r, request)
|
111
104
|
end
|
112
105
|
|
113
|
-
|
114
|
-
if parameterized_path?(route[:path])
|
115
|
-
split_path = route[:path].split("/")
|
116
|
-
path_parameter_data = build_path_regex(split_path).match(request.path)
|
117
|
-
path_parameter_names = find_parameter_names_in_path(split_path)
|
118
|
-
path_parameter_names.each_with_index do |parameter_name, i|
|
119
|
-
request.update_param(parameter_name, path_parameter_data[i+1])
|
120
|
-
end
|
121
|
-
end
|
106
|
+
return not_found unless route
|
122
107
|
|
123
|
-
|
124
|
-
|
125
|
-
else
|
126
|
-
not_found
|
108
|
+
path_parameters(route, request.path) do |name, value|
|
109
|
+
request.update_param(name, value)
|
127
110
|
end
|
111
|
+
|
112
|
+
resp = route[:route_class].call(request.env)
|
113
|
+
resp.finish
|
128
114
|
end
|
129
115
|
|
130
116
|
private
|
131
117
|
|
132
|
-
# Does the
|
118
|
+
# Does the given route match the request
|
119
|
+
def matching_route?(route, request)
|
120
|
+
return false unless route[:method] == request.request_method.to_sym
|
121
|
+
|
122
|
+
if parameterized_path?(route[:path])
|
123
|
+
path_regex = build_path_regex(route[:path].split('/'))
|
124
|
+
path_regex.match?(request.path)
|
125
|
+
else
|
126
|
+
route[:path] == request.path
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
# Yield the parameters parsed from the path
|
131
|
+
# Example definition: "/hello/:name"
|
132
|
+
# Example request: "/hello/robert"
|
133
|
+
# Example yield: name, robert
|
134
|
+
def path_parameters(route, path)
|
135
|
+
return unless parameterized_path?(route[:path])
|
136
|
+
|
137
|
+
split_path = route[:path].split('/')
|
138
|
+
path_parameter_data = build_path_regex(split_path).match(path)
|
139
|
+
path_parameter_names = find_parameter_names_in_path(split_path)
|
140
|
+
path_parameter_names.each_with_index do |parameter_name, i|
|
141
|
+
yield parameter_name, path_parameter_data[i + 1]
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
# Does the path have a parameter in it? ex: '/user/:id'
|
133
146
|
def parameterized_path?(path)
|
134
|
-
path.include?(
|
147
|
+
path.include?(':')
|
135
148
|
end
|
136
149
|
|
137
|
-
# Find all the named parameters in the route
|
150
|
+
# Find all the named parameters in the route
|
151
|
+
# Then drop the ':' so we have the names: ':id' => 'id'
|
138
152
|
def find_parameter_names_in_path(split_path)
|
139
|
-
split_path.find_all
|
153
|
+
params = split_path.find_all do |s|
|
154
|
+
if s.size > 1
|
155
|
+
s[0] == ':'
|
156
|
+
else
|
157
|
+
false
|
158
|
+
end
|
159
|
+
end
|
160
|
+
params.map { |s| s[1..-1] }
|
140
161
|
end
|
141
162
|
|
142
|
-
# Turn the route into a regex:
|
163
|
+
# Turn the route into a regex: '/hello/:name' => '\/hello\/(\w*)'
|
143
164
|
def build_path_regex(split_path)
|
144
|
-
regex_string = split_path.map
|
145
|
-
|
165
|
+
regex_string = split_path.map do |s|
|
166
|
+
if s[0] == ':'
|
167
|
+
'(\\w*)'
|
168
|
+
else
|
169
|
+
s
|
170
|
+
end
|
171
|
+
end
|
172
|
+
Regexp.new(regex_string.join('\/'))
|
146
173
|
end
|
147
174
|
|
148
175
|
# Return HTTP 404
|
149
176
|
def not_found
|
150
177
|
resp = Rack::Response.new
|
151
178
|
resp.status = 404
|
152
|
-
resp.write
|
179
|
+
resp.write 'Not Found'
|
153
180
|
resp
|
154
181
|
end
|
155
182
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wouter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robert Peterson
|
@@ -31,107 +31,106 @@ dependencies:
|
|
31
31
|
- !ruby/object:Gem::Version
|
32
32
|
version: 2.0.6
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
|
-
name:
|
34
|
+
name: m
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
36
36
|
requirements:
|
37
37
|
- - "~>"
|
38
38
|
- !ruby/object:Gem::Version
|
39
|
-
version: '
|
39
|
+
version: '1.5'
|
40
40
|
- - ">="
|
41
41
|
- !ruby/object:Gem::Version
|
42
|
-
version:
|
42
|
+
version: 1.5.1
|
43
43
|
type: :development
|
44
44
|
prerelease: false
|
45
45
|
version_requirements: !ruby/object:Gem::Requirement
|
46
46
|
requirements:
|
47
47
|
- - "~>"
|
48
48
|
- !ruby/object:Gem::Version
|
49
|
-
version: '
|
49
|
+
version: '1.5'
|
50
50
|
- - ">="
|
51
51
|
- !ruby/object:Gem::Version
|
52
|
-
version:
|
52
|
+
version: 1.5.1
|
53
53
|
- !ruby/object:Gem::Dependency
|
54
|
-
name:
|
54
|
+
name: minitest
|
55
55
|
requirement: !ruby/object:Gem::Requirement
|
56
56
|
requirements:
|
57
57
|
- - "~>"
|
58
58
|
- !ruby/object:Gem::Version
|
59
|
-
version: '
|
59
|
+
version: '5.11'
|
60
60
|
- - ">="
|
61
61
|
- !ruby/object:Gem::Version
|
62
|
-
version:
|
62
|
+
version: 5.11.3
|
63
63
|
type: :development
|
64
64
|
prerelease: false
|
65
65
|
version_requirements: !ruby/object:Gem::Requirement
|
66
66
|
requirements:
|
67
67
|
- - "~>"
|
68
68
|
- !ruby/object:Gem::Version
|
69
|
-
version: '
|
69
|
+
version: '5.11'
|
70
70
|
- - ">="
|
71
71
|
- !ruby/object:Gem::Version
|
72
|
-
version:
|
72
|
+
version: 5.11.3
|
73
73
|
- !ruby/object:Gem::Dependency
|
74
|
-
name: minitest
|
74
|
+
name: minitest-rg
|
75
75
|
requirement: !ruby/object:Gem::Requirement
|
76
76
|
requirements:
|
77
77
|
- - "~>"
|
78
78
|
- !ruby/object:Gem::Version
|
79
|
-
version: '5.
|
79
|
+
version: '5.2'
|
80
80
|
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version: 5.
|
82
|
+
version: 5.2.0
|
83
83
|
type: :development
|
84
84
|
prerelease: false
|
85
85
|
version_requirements: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
87
|
- - "~>"
|
88
88
|
- !ruby/object:Gem::Version
|
89
|
-
version: '5.
|
89
|
+
version: '5.2'
|
90
90
|
- - ">="
|
91
91
|
- !ruby/object:Gem::Version
|
92
|
-
version: 5.
|
92
|
+
version: 5.2.0
|
93
93
|
- !ruby/object:Gem::Dependency
|
94
|
-
name:
|
94
|
+
name: rack-test
|
95
95
|
requirement: !ruby/object:Gem::Requirement
|
96
96
|
requirements:
|
97
97
|
- - "~>"
|
98
98
|
- !ruby/object:Gem::Version
|
99
|
-
version: '
|
99
|
+
version: '1.1'
|
100
100
|
- - ">="
|
101
101
|
- !ruby/object:Gem::Version
|
102
|
-
version:
|
102
|
+
version: 1.1.0
|
103
103
|
type: :development
|
104
104
|
prerelease: false
|
105
105
|
version_requirements: !ruby/object:Gem::Requirement
|
106
106
|
requirements:
|
107
107
|
- - "~>"
|
108
108
|
- !ruby/object:Gem::Version
|
109
|
-
version: '
|
109
|
+
version: '1.1'
|
110
110
|
- - ">="
|
111
111
|
- !ruby/object:Gem::Version
|
112
|
-
version:
|
112
|
+
version: 1.1.0
|
113
113
|
- !ruby/object:Gem::Dependency
|
114
|
-
name:
|
114
|
+
name: rake
|
115
115
|
requirement: !ruby/object:Gem::Requirement
|
116
116
|
requirements:
|
117
117
|
- - "~>"
|
118
118
|
- !ruby/object:Gem::Version
|
119
|
-
version: '
|
119
|
+
version: '12.3'
|
120
120
|
- - ">="
|
121
121
|
- !ruby/object:Gem::Version
|
122
|
-
version:
|
122
|
+
version: 12.3.1
|
123
123
|
type: :development
|
124
124
|
prerelease: false
|
125
125
|
version_requirements: !ruby/object:Gem::Requirement
|
126
126
|
requirements:
|
127
127
|
- - "~>"
|
128
128
|
- !ruby/object:Gem::Version
|
129
|
-
version: '
|
129
|
+
version: '12.3'
|
130
130
|
- - ">="
|
131
131
|
- !ruby/object:Gem::Version
|
132
|
-
version:
|
133
|
-
description: Wouter is a modular web router built on Rack.
|
134
|
-
like modularity. Wouter aims to make both of those aspects simple.
|
132
|
+
version: 12.3.1
|
133
|
+
description: Wouter is a modular web router built on Rack.
|
135
134
|
email: robertpeterson@gmail.com
|
136
135
|
executables: []
|
137
136
|
extensions: []
|