web_router 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- metadata +1 -4
- data/lib/web_router/controller.rb +0 -35
- data/lib/web_router/router.rb +0 -65
- data/lib/web_router/version.rb +0 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 24f192704a86bb566bc934a96b35cc423ac8cd1d
|
4
|
+
data.tar.gz: 0162953eb1449997193cbb7008729adfd7aa9c0a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1d0b4f6d74830577790e098530ad8a567586c4e7555ccdc5f908a799ff46fef7566f51e585e17208fcc498eb4dd6985cdb7d19fe9c5c440276b47ba975c45b45
|
7
|
+
data.tar.gz: 5da22b9fc16e48a3f1a0a2d09d0c55f562565e4ccafd62357d3e18435719a4ef5a7c679ba5e3f1df7957ad4787cfe4e6bf5be427a9d5de3327df9394286dae8b
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: web_router
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- superedriver
|
@@ -102,9 +102,6 @@ extensions: []
|
|
102
102
|
extra_rdoc_files: []
|
103
103
|
files:
|
104
104
|
- lib/web_router.rb
|
105
|
-
- lib/web_router/controller.rb
|
106
|
-
- lib/web_router/router.rb
|
107
|
-
- lib/web_router/version.rb
|
108
105
|
homepage: https://github.com/superedriver/web_router
|
109
106
|
licenses:
|
110
107
|
- MIT
|
@@ -1,35 +0,0 @@
|
|
1
|
-
class WebRouter::Controller
|
2
|
-
RESPONSE_TYPES = {
|
3
|
-
text: ['text/plain', -> (c) { c.to_s }],
|
4
|
-
json: ['application/json', -> (c) { Oj.dump(c) }],
|
5
|
-
}
|
6
|
-
|
7
|
-
def call(env)
|
8
|
-
@env = env
|
9
|
-
@request = Rack::Request.new(env)
|
10
|
-
@request.params.merge!(env['router.params'] || {})
|
11
|
-
send(@action_name)
|
12
|
-
[200, @response_headers, [@response_body]]
|
13
|
-
end
|
14
|
-
|
15
|
-
def self.action(action_name)
|
16
|
-
proc { |env| new(action_name).call(env)}
|
17
|
-
end
|
18
|
-
|
19
|
-
private
|
20
|
-
attr_reader :request
|
21
|
-
|
22
|
-
def initialize(action_name)
|
23
|
-
@action_name = action_name
|
24
|
-
end
|
25
|
-
|
26
|
-
def params
|
27
|
-
request.params
|
28
|
-
end
|
29
|
-
|
30
|
-
def response(type, content)
|
31
|
-
@response_headers ||= {}
|
32
|
-
@response_headers.merge!('Content-Type' => RESPONSE_TYPES[type][0] )
|
33
|
-
@response_body = RESPONSE_TYPES[type][1].call(content)
|
34
|
-
end
|
35
|
-
end
|
data/lib/web_router/router.rb
DELETED
@@ -1,65 +0,0 @@
|
|
1
|
-
class WebRouter::Router
|
2
|
-
def call(env)
|
3
|
-
find_route(env).call(env)
|
4
|
-
end
|
5
|
-
|
6
|
-
def configure(&block)
|
7
|
-
instance_exec(&block)
|
8
|
-
self
|
9
|
-
end
|
10
|
-
|
11
|
-
private
|
12
|
-
attr_reader :routes
|
13
|
-
|
14
|
-
def initialize
|
15
|
-
@routes = []
|
16
|
-
end
|
17
|
-
|
18
|
-
def get(path, rack_app) match('GET', path, rack_app) end
|
19
|
-
def post(path, rack_app) match('POST', path, rack_app) end
|
20
|
-
def put(path, rack_app) match('PUT', path, rack_app) end
|
21
|
-
def delete(path, rack_app) match('DELETE', path, rack_app) end
|
22
|
-
|
23
|
-
def match(http_method, path, rack_app)
|
24
|
-
rack_app = get_controller_action(rack_app) if rack_app.is_a? String
|
25
|
-
routes << { pattern: path, app: rack_app, regexp: path_to_regexp(path), method: http_method}
|
26
|
-
end
|
27
|
-
|
28
|
-
def get_controller_action(str)
|
29
|
-
controller_name, action_name = str.split('#') # tests#show => ['tests', 'show']
|
30
|
-
controller_name = to_upper_camel_case(controller_name)
|
31
|
-
Kernel.const_get(controller_name).send(:action, action_name)
|
32
|
-
end
|
33
|
-
|
34
|
-
def to_upper_camel_case(str)
|
35
|
-
str # 'public_pages/tests' => PublicPages::TestsController
|
36
|
-
.split('/') # ['public_pages', 'test']
|
37
|
-
.map { |part| part.split('_').map(&:capitalize).join } # ['PublicPages', 'Test']
|
38
|
-
.join('::') + 'Controller'
|
39
|
-
end
|
40
|
-
|
41
|
-
def find_route(env)
|
42
|
-
routes.each do |route|
|
43
|
-
if env['REQUEST_METHOD'] == route[:method] && env['REQUEST_PATH'] =~ route[:regexp]
|
44
|
-
env['router.params'] = extract_params(route[:pattern], env['REQUEST_PATH'])
|
45
|
-
return route[:app]
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
->(_env) { [404, {}, ['not found']] }
|
50
|
-
end
|
51
|
-
|
52
|
-
def path_to_regexp(path)
|
53
|
-
Regexp.new('\A' + path.gsub(/:[\w-]+/, '[\w-]+') + '\Z')
|
54
|
-
end
|
55
|
-
|
56
|
-
# pattern: "post/:name", route: "post/about_ruby"
|
57
|
-
def extract_params(pattern, path)
|
58
|
-
pattern
|
59
|
-
.split('/') # ['post', ':name']
|
60
|
-
.zip(path.split('/')) # [['post', 'post'],[':name', 'post']]
|
61
|
-
.reject { |e| e.first == e.last } # [[':name', 'post']]
|
62
|
-
.map { |e| [e.first[1..-1], e.last] } # [['name', 'post']]
|
63
|
-
.to_h
|
64
|
-
end
|
65
|
-
end
|
data/lib/web_router/version.rb
DELETED