hmlons_web 0.1.0 → 0.2.0
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/hmlons_web/router.rb +35 -8
- data/lib/hmlons_web/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6a7e5692d27046682c7e4a3e00d1485e0ef4a216
|
4
|
+
data.tar.gz: 35b58d2d7f2bd78fd3c53bea8aa3256e5529a1e6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d90c1a84d61ab0122d2b25528b5ceacccfebb5fc58792ad8482c38bf6b63d9da8c25978d5428519175154e66fed9fba75e0834b3de342de1bf3db64124e43634
|
7
|
+
data.tar.gz: b2e95c5fa605cf757ea96a280c83e2dea2765ab809ad5d1527461a90ec0a0d24df1e4ec6f3a2d0d2167f2f9f87ac48dcd10f2d35b6131d232d4118049c2014f5
|
data/lib/hmlons_web/router.rb
CHANGED
@@ -13,11 +13,12 @@ private
|
|
13
13
|
def find_route(env)
|
14
14
|
@routes.each do |route|
|
15
15
|
if env['REQUEST_METHOD'] == route[:method] && env['REQUEST_PATH'] =~ route[:regexp]
|
16
|
+
env['router.params'] = extract_params(route[:pattern], env['REQUEST_PATH'])
|
16
17
|
return route[:app]
|
17
18
|
end
|
18
19
|
end
|
19
20
|
|
20
|
-
->(_env) { [404, {}, ['page not found']] }
|
21
|
+
return ->(_env) { [404, {}, ['page not found']] }
|
21
22
|
end
|
22
23
|
|
23
24
|
def get(path, rack_app)
|
@@ -29,15 +30,41 @@ private
|
|
29
30
|
end
|
30
31
|
|
31
32
|
def match(http_method, path, rack_app)
|
32
|
-
|
33
|
-
|
34
|
-
pattern: path,
|
35
|
-
app: rack_app,
|
36
|
-
regexp: path_to_regexp(path)
|
37
|
-
}
|
33
|
+
rack_app = get_controller_action(rack_app) if rack_app.is_a?(String)
|
34
|
+
@routes << { pattern: path, app: rack_app, regexp: path_to_regexp(path), method: http_method }
|
38
35
|
end
|
39
36
|
|
37
|
+
def get_controller_action(str)
|
38
|
+
controller_name, action_name = str.split('#') # tests#show => ['tests', 'show']
|
39
|
+
controller_name = to_upper_camel_case(controller_name)
|
40
|
+
Kernel.const_get(controller_name).send(:action, action_name)
|
41
|
+
# controller_name = public_test
|
42
|
+
# action_name = show
|
43
|
+
# PublicTestController.action('show')
|
44
|
+
end
|
45
|
+
|
46
|
+
def to_upper_camel_case(str)
|
47
|
+
str # 'public_pages/tests' => PublicPages::TestsController
|
48
|
+
.split('/') # ['public_pages', 'test']
|
49
|
+
.map { |part| part.split('_').map(&:capitalize).join } # ['PublicPages', 'Test']
|
50
|
+
.join('::') + 'Controller'
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
# /post/:name
|
40
55
|
def path_to_regexp(path)
|
41
56
|
Regexp.new('\A' + path.gsub(/:[\w-]+/, '[\w-]+') + '\Z')
|
42
57
|
end
|
43
|
-
|
58
|
+
|
59
|
+
# /post/:name
|
60
|
+
# /post/test_one
|
61
|
+
# { name: 'test_one' }
|
62
|
+
def extract_params(pattern, path)
|
63
|
+
pattern
|
64
|
+
.split('/') # ['post', ':name']
|
65
|
+
.zip(path.split('/')) # [['post', 'post'],[':name', 'post']]
|
66
|
+
.reject { |e| e.first == e.last } # [[':name', 'post']]
|
67
|
+
.map { |e| [e.first[1..-1], e.last] } # [['name', 'post']]
|
68
|
+
.to_h
|
69
|
+
end
|
70
|
+
end
|
data/lib/hmlons_web/version.rb
CHANGED