jsonrpc-rails 0.4.0 → 0.5.1
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/README.md +3 -3
- data/lib/jsonrpc_rails/controller_helpers.rb +3 -3
- data/lib/jsonrpc_rails/railtie.rb +73 -48
- data/lib/jsonrpc_rails/version.rb +1 -1
- 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: 93b8692cfb780990697a06219f80c323096bd5b12d21b95ec0a70e83dcbe555c
|
4
|
+
data.tar.gz: a3a15cc185bfb704d6a11cd2085be7beb291c20e727ef9cce317d4b993d5efc4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 305bde8d1013629a31736c9da2de66a90e6d0e860311b784cbac65615646a24ae6460b1cb21699c8c79141027b65c8a988b6f20fbcdbad6d7e1425125fe458df
|
7
|
+
data.tar.gz: 232740f9c3756071955993b87f7f8ae4832d6159dd6273cd71a6b944003dbc1d68f32692addab5b3ea90d04bfc32908f4572da0791b778e7fdd8f4394abe640d
|
data/README.md
CHANGED
@@ -111,9 +111,9 @@ In your controller action, you can access the validated payload like this:
|
|
111
111
|
class MyApiController < ApplicationController
|
112
112
|
# POST /rpc
|
113
113
|
def process
|
114
|
-
if
|
114
|
+
if jsonrpc_params_batch?
|
115
115
|
# ── batch ───────────────────────────────────────────────────────────────
|
116
|
-
responses =
|
116
|
+
responses = jsonrpc_params.filter_map { |req| handle_single_request(req) } # strip nil (notifications)
|
117
117
|
|
118
118
|
if responses.empty?
|
119
119
|
head :no_content
|
@@ -123,7 +123,7 @@ class MyApiController < ApplicationController
|
|
123
123
|
end
|
124
124
|
else
|
125
125
|
# ── single ──────────────────────────────────────────────────────────────
|
126
|
-
response = handle_single_request(
|
126
|
+
response = handle_single_request(jsonrpc_params)
|
127
127
|
|
128
128
|
if response # request (has id)
|
129
129
|
render jsonrpc: response.result,
|
@@ -7,13 +7,13 @@ module JSONRPC_Rails
|
|
7
7
|
included do
|
8
8
|
# Returns a JSON_RPC::Request / Notification / Response
|
9
9
|
# or an Array of them for batch calls.
|
10
|
-
def
|
10
|
+
def jsonrpc_params
|
11
11
|
request.env[:jsonrpc]
|
12
12
|
end
|
13
13
|
|
14
14
|
# Convenience boolean
|
15
|
-
def
|
16
|
-
|
15
|
+
def jsonrpc_params_batch?
|
16
|
+
jsonrpc_params.is_a?(Array)
|
17
17
|
end
|
18
18
|
end
|
19
19
|
end
|
@@ -1,72 +1,97 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
# lib/jsonrpc_rails/railtie.rb
|
3
4
|
require_relative "middleware/validator"
|
4
5
|
|
5
6
|
module JSONRPC_Rails
|
6
|
-
# Use Rails::Railtie to integrate with the Rails application
|
7
7
|
class Railtie < Rails::Railtie
|
8
8
|
config.jsonrpc_rails = ActiveSupport::OrderedOptions.new
|
9
|
-
config.jsonrpc_rails.validated_paths = []
|
10
|
-
|
9
|
+
config.jsonrpc_rails.validated_paths = []
|
10
|
+
|
11
11
|
initializer "jsonrpc-rails.add_validator_middleware" do |app|
|
12
12
|
app.middleware.use JSONRPC_Rails::Middleware::Validator
|
13
13
|
end
|
14
14
|
|
15
|
+
# -----------------------------------------------------------------------
|
16
|
+
# Renderer
|
17
|
+
# -----------------------------------------------------------------------
|
15
18
|
initializer "jsonrpc-rails.add_renderers" do
|
16
19
|
ActiveSupport.on_load(:action_controller) do
|
17
20
|
ActionController::Renderers.add :jsonrpc do |obj, options|
|
18
|
-
|
19
|
-
|
20
|
-
error_input = options[:error] # Can be nil, Symbol, Hash, or JsonRpcError
|
21
|
-
payload_obj = obj # The main object passed to render
|
21
|
+
response_id = options[:id]
|
22
|
+
error_opt = options[:error]
|
22
23
|
|
23
24
|
begin
|
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
|
-
|
25
|
+
payload =
|
26
|
+
case obj
|
27
|
+
# ─── Already JSON-RPC objects ───────────────────────────────
|
28
|
+
when JSON_RPC::Response,
|
29
|
+
JSON_RPC::Request,
|
30
|
+
JSON_RPC::Notification
|
31
|
+
obj.to_h
|
32
|
+
|
33
|
+
# ─── Batch of objects ──────────────────────────────────────
|
34
|
+
when Array
|
35
|
+
unless obj.all? { |o| o.is_a?(JSON_RPC::Response) ||
|
36
|
+
o.is_a?(JSON_RPC::Request) ||
|
37
|
+
o.is_a?(JSON_RPC::Notification) }
|
38
|
+
raise ArgumentError, "Batch contains non-JSON-RPC objects"
|
39
|
+
end
|
40
|
+
obj.map(&:to_h)
|
41
|
+
|
42
|
+
# ─── Legacy “result + :error” path ─────────────────────────
|
43
|
+
else
|
44
|
+
case error_opt
|
45
|
+
when nil, false
|
46
|
+
JSON_RPC::Response.new(id: response_id,
|
47
|
+
result: obj).to_h
|
48
|
+
|
49
|
+
when Symbol
|
50
|
+
msg = obj.is_a?(Hash) ? obj[:message] : nil
|
51
|
+
dat = obj.is_a?(Hash) ? obj[:data] : nil
|
52
|
+
err = JSON_RPC::JsonRpcError.build(error_opt,
|
53
|
+
message: msg,
|
54
|
+
data: dat)
|
55
|
+
JSON_RPC::Response.new(id: response_id,
|
56
|
+
error: err).to_h
|
57
|
+
|
58
|
+
when Integer
|
59
|
+
defaults = JSON_RPC::JsonRpcError.find_by_code(error_opt)
|
60
|
+
msg = obj.is_a?(Hash) ? obj[:message] : nil
|
61
|
+
msg ||= defaults&.fetch(:message, "Unknown error")
|
62
|
+
|
63
|
+
dat = obj.is_a?(Hash) ? obj[:data] : nil
|
64
|
+
hash = { code: error_opt, message: msg }
|
65
|
+
hash[:data] = dat if dat
|
66
|
+
JSON_RPC::Response.new(id: response_id,
|
67
|
+
error: hash).to_h
|
68
|
+
|
69
|
+
when JSON_RPC::JsonRpcError
|
70
|
+
JSON_RPC::Response.new(id: response_id,
|
71
|
+
error: error_opt.to_h).to_h
|
72
|
+
|
73
|
+
when Hash
|
74
|
+
JSON_RPC::Response.new(id: response_id,
|
75
|
+
error: error_opt).to_h
|
76
|
+
|
77
|
+
else
|
78
|
+
raise ArgumentError,
|
79
|
+
":error must be Symbol, Integer, Hash, or JSON_RPC::JsonRpcError " \
|
80
|
+
"(got #{error_opt.class})"
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
54
84
|
rescue ArgumentError => e
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
response_payload = { jsonrpc: "2.0", error: internal_error.to_h, id: response_id }
|
60
|
-
# Consider logging the error e.message
|
85
|
+
internal = JSON_RPC::JsonRpcError.new(:internal_error,
|
86
|
+
message: "Server error: #{e.message}")
|
87
|
+
payload = { jsonrpc: "2.0", error: internal.to_h, id: response_id }
|
88
|
+
|
61
89
|
rescue JSON_RPC::JsonRpcError => e
|
62
|
-
|
63
|
-
response_payload = { jsonrpc: "2.0", error: e.to_h, id: response_id }
|
64
|
-
# Consider logging the error e.message
|
90
|
+
payload = { jsonrpc: "2.0", error: e.to_h, id: response_id }
|
65
91
|
end
|
66
92
|
|
67
|
-
# Set the proper MIME type and convert the hash to JSON.
|
68
93
|
self.content_type ||= Mime[:json]
|
69
|
-
self.response_body =
|
94
|
+
self.response_body = payload.to_json
|
70
95
|
end
|
71
96
|
end
|
72
97
|
end
|