primate-run 0.2.0 → 0.3.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/primate/route.rb +46 -95
- data/lib/primate/version.rb +1 -1
- data/sig/primate.rbs +5 -3
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 8b2c148d3977eec632a6dbaf26f9f287aa31103186aa483f0ec1344f7373bd72
|
|
4
|
+
data.tar.gz: 984e63ba747fbafb366e08a697fdef9619a363d7972f97d44b8946e72228beb0
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 396027b032cbc82037e8a425d55aebf54e1adf94736f24a54baa46ce6d6cda2c985177b50af3d2ce086a8767ca8ce5d7ccf5b0ff63ebfd6c1a47c92fe84bb163
|
|
7
|
+
data.tar.gz: 1bcf294a39b094a2ab643326ec0af72230dd317cd7d21a88751030e2afe93bca3ffe75f5de16dd194d6222f72ad528976f4a61a8748b038ee22e1355362a552a
|
data/lib/primate/route.rb
CHANGED
|
@@ -4,101 +4,52 @@ require_relative 'request'
|
|
|
4
4
|
require_relative 'response'
|
|
5
5
|
require_relative '../primate'
|
|
6
6
|
|
|
7
|
-
# Route registration and handling
|
|
8
7
|
module Route
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
def self.head(&block)
|
|
56
|
-
@routes['HEAD'] = block
|
|
57
|
-
end
|
|
58
|
-
|
|
59
|
-
# Register a OPTIONS route handler
|
|
60
|
-
#
|
|
61
|
-
# @yieldparam request [Request] The HTTP request object
|
|
62
|
-
# @return [void]
|
|
63
|
-
def self.options(&block)
|
|
64
|
-
@routes['OPTIONS'] = block
|
|
65
|
-
end
|
|
66
|
-
|
|
67
|
-
# Register a CONNECT route handler
|
|
68
|
-
#
|
|
69
|
-
# @yieldparam request [Request] The HTTP request object
|
|
70
|
-
# @return [void]
|
|
71
|
-
def self.connect(&block)
|
|
72
|
-
@routes['CONNECT'] = block
|
|
73
|
-
end
|
|
74
|
-
|
|
75
|
-
# Register a TRACE route handler
|
|
76
|
-
#
|
|
77
|
-
# @yieldparam request [Request] The HTTP request object
|
|
78
|
-
# @return [void]
|
|
79
|
-
def self.trace(&block)
|
|
80
|
-
@routes['TRACE'] = block
|
|
81
|
-
end
|
|
82
|
-
|
|
83
|
-
# Get all registered routes
|
|
84
|
-
#
|
|
85
|
-
# @return [Hash] Hash of HTTP method => handler block
|
|
86
|
-
def self.routes
|
|
87
|
-
@routes
|
|
88
|
-
end
|
|
89
|
-
|
|
90
|
-
def self.set_session(session, helpers)
|
|
91
|
-
PrimateInternal.set_session(session, helpers)
|
|
92
|
-
end
|
|
93
|
-
# Execute a route handler for the given HTTP method
|
|
94
|
-
#
|
|
95
|
-
# @param method [String] HTTP method
|
|
96
|
-
# @param request [Request] Request object
|
|
97
|
-
# @return [Object] Response from the route handler
|
|
98
|
-
def self.call_route(method, request)
|
|
99
|
-
handler = @routes[method.upcase]
|
|
100
|
-
return Response.error(status: 404) unless handler
|
|
101
|
-
|
|
102
|
-
handler.call(request)
|
|
8
|
+
# registry: { scope_id => { "GET" => Proc, "POST" => Proc, ... } }
|
|
9
|
+
@registry = Hash.new { |h, k| h[k] = {} }
|
|
10
|
+
@current_scope = "__global__"
|
|
11
|
+
|
|
12
|
+
class << self
|
|
13
|
+
# set the active scope for subsequent handler registrations
|
|
14
|
+
# (wrapper calls this before evaluating a route file)
|
|
15
|
+
def scope(id)
|
|
16
|
+
@current_scope = id.to_s
|
|
17
|
+
@registry[@current_scope]
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# clear routes for a specific scope (or the current scope if none given)
|
|
21
|
+
# wrapper calls this right before (re)evaluating the route file
|
|
22
|
+
def clear(id = nil)
|
|
23
|
+
target = (id || @current_scope).to_s
|
|
24
|
+
@registry.delete(target)
|
|
25
|
+
@registry[target] = {}
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# return the verb->handler map for a scope (read-only use)
|
|
29
|
+
def registry(id = nil)
|
|
30
|
+
@registry[(id || @current_scope).to_s]
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
%w[GET POST PUT PATCH DELETE HEAD OPTIONS CONNECT TRACE].each do |verb|
|
|
34
|
+
define_method(verb.downcase) do |&block|
|
|
35
|
+
raise ArgumentError, "block required" unless block
|
|
36
|
+
registry[verb] = block
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def set_session(session, helpers)
|
|
41
|
+
PrimateInternal.set_session(session, helpers)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def call_js(scope_id, verb, js_req, helpers, session)
|
|
45
|
+
set_session(session, helpers)
|
|
46
|
+
request = Request.new(js_req, helpers)
|
|
47
|
+
|
|
48
|
+
verb_up = verb.to_s.upcase
|
|
49
|
+
handler = @registry.dig(scope_id.to_s, verb_up)
|
|
50
|
+
return Response.error(status: 404) unless handler
|
|
51
|
+
|
|
52
|
+
handler.call(request)
|
|
53
|
+
end
|
|
103
54
|
end
|
|
104
55
|
end
|
data/lib/primate/version.rb
CHANGED
data/sig/primate.rbs
CHANGED
|
@@ -49,7 +49,7 @@ class RequestBody
|
|
|
49
49
|
def initialize: (untyped body, untyped helpers) -> void
|
|
50
50
|
def json: () -> Hash[String, untyped]
|
|
51
51
|
def text: () -> String
|
|
52
|
-
def
|
|
52
|
+
def form: () -> Hash[String, untyped]
|
|
53
53
|
def files: () -> Array[Primate::UploadedFile]
|
|
54
54
|
def binary: () -> Primate::Readable
|
|
55
55
|
end
|
|
@@ -105,6 +105,9 @@ module Response
|
|
|
105
105
|
end
|
|
106
106
|
|
|
107
107
|
module Route
|
|
108
|
+
def self.scope: (String id) -> void
|
|
109
|
+
def self.clear: (?String id) -> void
|
|
110
|
+
def self.registry: (?String id) -> Hash[String, Proc]
|
|
108
111
|
def self.get: () { (Request) -> untyped } -> void
|
|
109
112
|
def self.post: () { (Request) -> untyped } -> void
|
|
110
113
|
def self.put: () { (Request) -> untyped } -> void
|
|
@@ -114,8 +117,7 @@ module Route
|
|
|
114
117
|
def self.options: () { (Request) -> untyped } -> void
|
|
115
118
|
def self.connect: () { (Request) -> untyped } -> void
|
|
116
119
|
def self.trace: () { (Request) -> untyped } -> void
|
|
117
|
-
def self.
|
|
118
|
-
def self.set_session: (untyped session, untyped helpers) -> void
|
|
120
|
+
def self.call_js: (String scope_id, String verb, untyped js_req, untyped helpers, untyped session) -> untyped
|
|
119
121
|
def self.call_route: (String method, Request request) -> untyped
|
|
120
122
|
end
|
|
121
123
|
|