rack-http_router 0.0.33 → 0.0.35
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rack-http_router/action.rb +17 -5
- data/lib/rack-http_router/router/build_request.rb +12 -10
- data/lib/rack-http_router/router/route.rb +4 -2
- data/lib/rack-http_router/router.rb +29 -12
- data/lib/rack-http_router.rb +2 -2
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4e7eb090e26635738162faa639dae1abd812fe0b5491f4767ae3bdfd8f8db36e
|
4
|
+
data.tar.gz: cf2d047dd885c51b8e45506835d03890502ab98a801065953cf06a2e516969e6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a79812b7fb6d9d93f78d3e4dae086ee14e39036a6f0a82b946d6c68b0b0b3de3f9e0945e6fedad92efb51ccb2d4665bdfdc565cd4e9718efe400fe449a17152a
|
7
|
+
data.tar.gz: f5619bcb193f626bbc0b191798391b63e02a5eb98e04da9f63775f2e69665d502b92536dd5c6dee56feefbeed894132334abc1fcddef9bf10b4accd2ecf0da89
|
@@ -1,7 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'erubi'
|
4
|
-
require '
|
4
|
+
require 'oj'
|
5
5
|
require 'rack'
|
6
6
|
|
7
7
|
module Rack
|
@@ -44,6 +44,10 @@ module Rack
|
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
47
|
+
def assign(obj, hash)
|
48
|
+
Rack::HttpRouter::Action.assign(obj, hash)
|
49
|
+
end
|
50
|
+
|
47
51
|
def html(content, status: 200)
|
48
52
|
Rack::HttpRouter::Action.html(content, status: status)
|
49
53
|
end
|
@@ -85,6 +89,14 @@ module Rack
|
|
85
89
|
end
|
86
90
|
|
87
91
|
class << self
|
92
|
+
def assign(obj, hash)
|
93
|
+
hash.each do |k, v|
|
94
|
+
obj.define_singleton_method(k) { v }
|
95
|
+
end
|
96
|
+
|
97
|
+
obj
|
98
|
+
end
|
99
|
+
|
88
100
|
def html(content, status: 200)
|
89
101
|
[status, { 'Content-Type' => 'text/html' }, [content]]
|
90
102
|
end
|
@@ -122,9 +134,9 @@ module Rack
|
|
122
134
|
response_instance: false
|
123
135
|
)
|
124
136
|
erb = if paths.is_a?(Array)
|
125
|
-
paths.map { |path| erb("views/#{path}", config, route, db, view_params) }.join
|
137
|
+
paths.map { |path| erb("#{config.dig(:views, :path) || "views"}/#{path}", config, route, db, view_params) }.join
|
126
138
|
else
|
127
|
-
erb("views/#{paths}", config, route, db, view_params)
|
139
|
+
erb("#{config.dig(:views, :path) || "views"}/#{paths}", config, route, db, view_params)
|
128
140
|
end
|
129
141
|
|
130
142
|
if response_instance
|
@@ -139,12 +151,12 @@ module Rack
|
|
139
151
|
end
|
140
152
|
|
141
153
|
def json(content = {}, status: 200)
|
142
|
-
[status, { 'Content-Type' => 'application/json' }, [content
|
154
|
+
[status, { 'Content-Type' => 'application/json' }, [Oj.dump(content, mode: :compat)]]
|
143
155
|
end
|
144
156
|
|
145
157
|
def json_response(content = {}, status: 200)
|
146
158
|
Rack::Response.new(
|
147
|
-
content
|
159
|
+
Oj.dump(content, mode: :compat),
|
148
160
|
status,
|
149
161
|
{ 'Content-Type' => 'application/json' }
|
150
162
|
)
|
@@ -22,18 +22,20 @@ module Rack
|
|
22
22
|
def update_request_params(request, route)
|
23
23
|
splitted_request_path = request.path.split('/')
|
24
24
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
end
|
25
|
+
i = 0
|
26
|
+
|
27
|
+
while i < route.splitted_path.size
|
28
|
+
route_word = route.splitted_path[i]
|
29
|
+
if route_word.start_with?(':')
|
30
|
+
param = splitted_request_path[i]
|
31
|
+
param = param.to_i if is_a_integer_string?(param)
|
32
|
+
|
33
|
+
update_request_param(request, route_word, param)
|
35
34
|
end
|
36
35
|
|
36
|
+
i += 1
|
37
|
+
end
|
38
|
+
|
37
39
|
request
|
38
40
|
end
|
39
41
|
|
@@ -4,14 +4,16 @@ module Rack
|
|
4
4
|
class HttpRouter
|
5
5
|
class Router
|
6
6
|
class Route
|
7
|
-
attr_reader :endpoint, :splitted_path, :has_params
|
7
|
+
attr_reader :endpoint, :splitted_path, :has_params, :has_befores, :befores
|
8
8
|
|
9
|
-
def initialize(path, endpoint)
|
9
|
+
def initialize(path, endpoint, befores = [])
|
10
10
|
@path = path
|
11
11
|
@splitted_path = @path.split('/')
|
12
12
|
@endpoint = endpoint
|
13
13
|
@params = fetch_params
|
14
14
|
@has_params = @params != []
|
15
|
+
@befores = befores
|
16
|
+
@has_befores = befores != []
|
15
17
|
end
|
16
18
|
|
17
19
|
def match?(env)
|
@@ -20,6 +20,8 @@ module Rack
|
|
20
20
|
end
|
21
21
|
@config = config
|
22
22
|
@scopes = []
|
23
|
+
@befores = []
|
24
|
+
@scopes_befores = {}
|
23
25
|
@error = proc { |_req, e| raise e }
|
24
26
|
@not_found = proc { [404, {}, ['Not found']] }
|
25
27
|
end
|
@@ -32,29 +34,40 @@ module Rack
|
|
32
34
|
|
33
35
|
return render_not_found(request_builder.call) if route_instance.nil?
|
34
36
|
|
35
|
-
|
36
|
-
return route_instance.endpoint.call(
|
37
|
-
request_builder.call(route_instance)
|
38
|
-
)
|
39
|
-
end
|
37
|
+
rack_request = request_builder.call(route_instance)
|
40
38
|
|
41
|
-
|
42
|
-
|
43
|
-
|
39
|
+
befores = route_instance.befores
|
40
|
+
before_result = nil
|
41
|
+
i = 0
|
42
|
+
while i < befores.size
|
43
|
+
before_result = call_endpoint(befores[i], rack_request)
|
44
|
+
return before_result unless before_result.is_a?(Rack::Request)
|
45
|
+
|
46
|
+
i += 1
|
44
47
|
end
|
45
48
|
|
46
|
-
route_instance.endpoint
|
49
|
+
call_endpoint(route_instance.endpoint, before_result || rack_request)
|
47
50
|
rescue Exception => e
|
48
51
|
@error.call(request_builder.call, e)
|
49
52
|
end
|
50
53
|
|
54
|
+
def call_endpoint(endpoint, rack_request)
|
55
|
+
return endpoint.call(rack_request) if endpoint.respond_to?(:call)
|
56
|
+
|
57
|
+
if endpoint.include?(Rack::HttpRouter::Action)
|
58
|
+
return endpoint.new(route: @route, config: @config).call(rack_request)
|
59
|
+
end
|
60
|
+
|
61
|
+
endpoint.new.call(rack_request)
|
62
|
+
end
|
63
|
+
|
51
64
|
def add(method, path, endpoint, as = nil)
|
52
65
|
method = :get if method == :head
|
53
66
|
|
54
67
|
path_with_scopes = "/#{@scopes.join('/')}#{put_path_slash(path)}"
|
55
68
|
@route[as] = path_with_scopes if as
|
56
69
|
|
57
|
-
route_instance = Route.new(path_with_scopes, endpoint)
|
70
|
+
route_instance = Route.new(path_with_scopes, endpoint, @befores)
|
58
71
|
|
59
72
|
if @scopes.size >= 1
|
60
73
|
return push_to_scope(method.to_s.upcase, route_instance)
|
@@ -71,11 +84,15 @@ module Rack
|
|
71
84
|
@error = endpoint
|
72
85
|
end
|
73
86
|
|
74
|
-
def append_scope(name)
|
87
|
+
def append_scope(name, befores = [])
|
75
88
|
@scopes.push(name)
|
89
|
+
befores = [befores] unless befores.is_a?(Array)
|
90
|
+
@befores.concat(befores)
|
91
|
+
@scopes_befores[name] = befores
|
76
92
|
end
|
77
93
|
|
78
94
|
def clear_last_scope
|
95
|
+
@befores -= @scopes_befores[@scopes.last]
|
79
96
|
@scopes = @scopes.first(@scopes.size - 1)
|
80
97
|
end
|
81
98
|
|
@@ -133,7 +150,7 @@ module Rack
|
|
133
150
|
return @routes[env['REQUEST_METHOD']].dig(
|
134
151
|
*(found_scopes << :__instances)
|
135
152
|
)
|
136
|
-
|
153
|
+
&.detect { |route_instance| route_instance.match?(env) }
|
137
154
|
end
|
138
155
|
|
139
156
|
match_route(env, tail, found_scopes)
|
data/lib/rack-http_router.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-http_router
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.35
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Henrique F. Teixeira
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-07-
|
11
|
+
date: 2023-07-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: erubi
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.12'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: oj
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.15'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.15'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: rack
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|