primate-run 0.2.0 → 0.5.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/i18n.rb +58 -0
- data/lib/primate/route.rb +49 -95
- data/lib/primate/session.rb +1 -1
- data/lib/primate/version.rb +1 -1
- data/sig/primate.rbs +5 -3
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 87e509379ff99ace8be7f98839eeee30c17b5be50079fbbbfd6b7adba31a8537
|
|
4
|
+
data.tar.gz: c329779406d4079f8dea9a40b29d1c13a14fa12879ef10b1e95bfc09e394e54f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d12613cb5afa41f93ff3cdba54f56c686288bd0a99014fdb48ac127f22b2e219caea6c94d2e76d4f8ef98652176f8405843a89c9e96136b0d539f2a89299e180
|
|
7
|
+
data.tar.gz: 28e1aad880c9866b2ca0c03f19501ccb78b76c2e0774ac0fe54d4f0388656727048734030c147f6605754391305a33792e16e4ba626b7d74a25370d57021ff59
|
data/lib/primate/i18n.rb
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Internationalization support
|
|
4
|
+
module I18N
|
|
5
|
+
@current = nil
|
|
6
|
+
|
|
7
|
+
class << self
|
|
8
|
+
# Set the current i18n instance (called by framework)
|
|
9
|
+
#
|
|
10
|
+
# @param i18n_obj [Object] JavaScript i18n object from the runtime
|
|
11
|
+
# @return [void]
|
|
12
|
+
def set_current(i18n_obj)
|
|
13
|
+
@current = i18n_obj
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# Translate a key with optional parameters
|
|
17
|
+
#
|
|
18
|
+
# @param key [String] Translation key
|
|
19
|
+
# @param params [Hash] Interpolation parameters
|
|
20
|
+
# @return [String] Translated string
|
|
21
|
+
def t(key, params = nil)
|
|
22
|
+
return key unless @current
|
|
23
|
+
|
|
24
|
+
if params.nil?
|
|
25
|
+
@current.t(key)
|
|
26
|
+
else
|
|
27
|
+
@current.t(key, params.to_json)
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# Locale accessor
|
|
32
|
+
#
|
|
33
|
+
# @return [Locale] Locale accessor object
|
|
34
|
+
def locale
|
|
35
|
+
Locale
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Locale accessor module
|
|
40
|
+
module Locale
|
|
41
|
+
class << self
|
|
42
|
+
# Get the current locale
|
|
43
|
+
#
|
|
44
|
+
# @return [String] Current locale code
|
|
45
|
+
def get
|
|
46
|
+
I18N.instance_variable_get(:@current)&.[]('locale')
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# Set the current locale
|
|
50
|
+
#
|
|
51
|
+
# @param value [String] Locale code to set
|
|
52
|
+
# @return [void]
|
|
53
|
+
def set(value)
|
|
54
|
+
I18N.instance_variable_get(:@current)&.set(value)
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
data/lib/primate/route.rb
CHANGED
|
@@ -2,103 +2,57 @@
|
|
|
2
2
|
|
|
3
3
|
require_relative 'request'
|
|
4
4
|
require_relative 'response'
|
|
5
|
+
require_relative 'i18n'
|
|
5
6
|
require_relative '../primate'
|
|
6
7
|
|
|
7
|
-
# Route registration and handling
|
|
8
8
|
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
|
-
|
|
56
|
-
|
|
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)
|
|
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
|
+
def scope(id)
|
|
15
|
+
@current_scope = id.to_s
|
|
16
|
+
@registry[@current_scope]
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# clear routes for a specific scope (or the current scope if none given)
|
|
20
|
+
def clear(id = nil)
|
|
21
|
+
target = (id || @current_scope).to_s
|
|
22
|
+
@registry.delete(target)
|
|
23
|
+
@registry[target] = {}
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# return the verb->handler map for a scope (read-only use)
|
|
27
|
+
def registry(id = nil)
|
|
28
|
+
@registry[(id || @current_scope).to_s]
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
%w[GET POST PUT PATCH DELETE HEAD OPTIONS CONNECT TRACE].each do |verb|
|
|
32
|
+
define_method(verb.downcase) do |&block|
|
|
33
|
+
raise ArgumentError, "block required" unless block
|
|
34
|
+
registry[verb] = block
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def set_session(session, helpers)
|
|
39
|
+
PrimateInternal.set_session(session, helpers)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def set_i18n(i18n)
|
|
43
|
+
I18N.set_current(i18n)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def call_js(scope_id, verb, js_req, helpers, session, i18n)
|
|
47
|
+
set_session(session, helpers)
|
|
48
|
+
set_i18n(i18n)
|
|
49
|
+
|
|
50
|
+
request = Request.new(js_req, helpers)
|
|
51
|
+
verb_up = verb.to_s.upcase
|
|
52
|
+
handler = @registry.dig(scope_id.to_s, verb_up)
|
|
53
|
+
return Response.error(status: 404) unless handler
|
|
54
|
+
|
|
55
|
+
handler.call(request)
|
|
56
|
+
end
|
|
103
57
|
end
|
|
104
58
|
end
|
data/lib/primate/session.rb
CHANGED
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
|
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: primate-run
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.5.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Primate Team
|
|
@@ -60,6 +60,7 @@ extensions: []
|
|
|
60
60
|
extra_rdoc_files: []
|
|
61
61
|
files:
|
|
62
62
|
- lib/primate.rb
|
|
63
|
+
- lib/primate/i18n.rb
|
|
63
64
|
- lib/primate/pema.rb
|
|
64
65
|
- lib/primate/readable.rb
|
|
65
66
|
- lib/primate/request.rb
|