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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 437d100cbe3d433787e4ad9f88e9b5e30588ba42
4
- data.tar.gz: 2d058da493b884d8068ea8ac602bbc319723d874
3
+ metadata.gz: 6a7e5692d27046682c7e4a3e00d1485e0ef4a216
4
+ data.tar.gz: 35b58d2d7f2bd78fd3c53bea8aa3256e5529a1e6
5
5
  SHA512:
6
- metadata.gz: 821f5d36ef5c0b8c2f4284e0bcbe256c2a980b210c10321525903242db7bdc1c0ae2053185fba8d3a47c737a5a66b6864e53d17a91f5fcaa9ad4f8f1609d749e
7
- data.tar.gz: c442f9c3b61b00e1bca5752ee381fe0b0cdb0d5dd3af1694caa9ec9090910ea9ec37120c10a7cd786890392c55dcfd0ac41932933de34e339c22136e2b53047a
6
+ metadata.gz: d90c1a84d61ab0122d2b25528b5ceacccfebb5fc58792ad8482c38bf6b63d9da8c25978d5428519175154e66fed9fba75e0834b3de342de1bf3db64124e43634
7
+ data.tar.gz: b2e95c5fa605cf757ea96a280c83e2dea2765ab809ad5d1527461a90ec0a0d24df1e4ec6f3a2d0d2167f2f9f87ac48dcd10f2d35b6131d232d4118049c2014f5
@@ -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
- @routes << {
33
- method: http_method,
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
- end
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
@@ -1,3 +1,3 @@
1
1
  module HmlonsWeb
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hmlons_web
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nik