jsonrpc-rails 0.5.1 → 0.5.3
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/json_rpc/response.rb +27 -17
- data/lib/jsonrpc_rails/middleware/validator.rb +18 -5
- data/lib/jsonrpc_rails/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 16369528b9adcf6bc2b66b42365e475be375d330312694626d8244f2205e9df3
|
4
|
+
data.tar.gz: 4eeda34cff81e732d5aac37d684c81eba9c7a66de1729a7f1f2c1c8097de21fb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ebefd2f90219d0dd655a9e7fc21023aa38b26de412e52102bdb6d2014a75f1975c284a417d2f353989e09360c34b56c07e42af89643e99bb3fe990db886a1039
|
7
|
+
data.tar.gz: 2c447976bc3b1dc7a8e9c2ffc695278bd7ec0e44bbad864f5810c16adefb901ec1212145a8d56865e4f7a59eba36b760db71e69addac2b70422f2b8276b53a7f
|
data/lib/json_rpc/response.rb
CHANGED
@@ -9,14 +9,25 @@ module JSON_RPC
|
|
9
9
|
# @param result [Object, nil] The result data (if successful).
|
10
10
|
# @param error [Hash, JSON_RPC::JsonRpcError, Symbol, nil] The error object/symbol (if failed).
|
11
11
|
# @raise [ArgumentError] if both result and error are provided, or neither is provided for non-null id.
|
12
|
-
def initialize(id:,
|
13
|
-
|
12
|
+
def initialize(id:, **kwargs)
|
13
|
+
# Check which parameters were actually provided
|
14
|
+
has_result = kwargs.key?(:result)
|
15
|
+
has_error = kwargs.key?(:error)
|
16
|
+
|
17
|
+
result = kwargs[:result]
|
18
|
+
error = kwargs[:error]
|
19
|
+
|
20
|
+
validate_response(id, has_result, has_error, result, error)
|
14
21
|
error_obj = process_error(error)
|
22
|
+
|
15
23
|
super(id: id, result: result, error: error_obj)
|
16
24
|
end
|
17
25
|
|
18
26
|
def self.from_h(h)
|
19
|
-
|
27
|
+
args = { id: h["id"] }
|
28
|
+
args[:result] = h["result"] if h.key?("result")
|
29
|
+
args[:error] = h["error"] if h.key?("error")
|
30
|
+
new(**args)
|
20
31
|
end
|
21
32
|
|
22
33
|
# Returns a hash representation of the response, ready for JSON serialization.
|
@@ -40,22 +51,21 @@ module JSON_RPC
|
|
40
51
|
# Validates the response structure according to JSON-RPC 2.0 spec.
|
41
52
|
#
|
42
53
|
# @param id [Object] The request ID.
|
54
|
+
# @param has_result [Boolean] Whether result was provided.
|
55
|
+
# @param has_error [Boolean] Whether error was provided.
|
43
56
|
# @param result [Object] The result data.
|
44
57
|
# @param error_input [Object] The error data/object/symbol.
|
45
58
|
# @raise [ArgumentError] for invalid combinations.
|
46
|
-
def validate_response(id, result, error_input)
|
47
|
-
#
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
# If id is not null, either result or error MUST be present.
|
52
|
-
return unless !id.nil? && error_input.nil? && result.nil?
|
59
|
+
def validate_response(id, has_result, has_error, result, error_input)
|
60
|
+
# Cannot have both result and error
|
61
|
+
if has_result && has_error
|
62
|
+
raise ArgumentError, "Response cannot contain both 'result' and 'error'"
|
63
|
+
end
|
53
64
|
|
54
|
-
#
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
raise ArgumentError, "Response with non-null ID must contain either 'result' or 'error'"
|
65
|
+
# If id is not null, either result or error MUST be present
|
66
|
+
if !id.nil? && !has_result && !has_error
|
67
|
+
raise ArgumentError, "Response with non-null ID must contain either 'result' or 'error'"
|
68
|
+
end
|
59
69
|
end
|
60
70
|
|
61
71
|
# Processes the error input into a standard error hash.
|
@@ -72,11 +82,11 @@ module JSON_RPC
|
|
72
82
|
# Assume it's already a valid JSON-RPC error object hash
|
73
83
|
error_input
|
74
84
|
when Symbol
|
75
|
-
# Build from a standard error symbol
|
85
|
+
# Build from a standard error symbol (build returns a hash)
|
76
86
|
JSON_RPC::JsonRpcError.build(error_input)
|
77
87
|
else
|
78
88
|
# Fallback to internal error if the format is unexpected
|
79
|
-
JSON_RPC::JsonRpcError.build(:internal_error, message: "Invalid error format provided")
|
89
|
+
JSON_RPC::JsonRpcError.build(:internal_error, message: "Invalid error format provided")
|
80
90
|
end
|
81
91
|
end
|
82
92
|
end
|
@@ -35,7 +35,10 @@ module JSONRPC_Rails
|
|
35
35
|
|
36
36
|
raw_payload = parse_json(body)
|
37
37
|
|
38
|
-
|
38
|
+
unless raw_payload.is_a?(Hash) || raw_payload.is_a?(Array)
|
39
|
+
id = extract_id_from_raw_payload(raw_payload)
|
40
|
+
return jsonrpc_error_response(:invalid_request, id: id)
|
41
|
+
end
|
39
42
|
|
40
43
|
validity, = if raw_payload.is_a?(Array)
|
41
44
|
validate_batch(raw_payload)
|
@@ -89,9 +92,10 @@ module JSONRPC_Rails
|
|
89
92
|
|
90
93
|
def validate_single(obj)
|
91
94
|
if validate_single_structure(obj)
|
92
|
-
[ :valid,
|
95
|
+
[ :valid, obj["id"] ]
|
93
96
|
else
|
94
|
-
|
97
|
+
id = obj.is_a?(Hash) ? obj["id"] : nil
|
98
|
+
[ jsonrpc_error_response(:invalid_request, id: id), id ]
|
95
99
|
end
|
96
100
|
end
|
97
101
|
|
@@ -119,13 +123,22 @@ module JSONRPC_Rails
|
|
119
123
|
|
120
124
|
# @param error_sym [Symbol]
|
121
125
|
# @param status [Integer]
|
126
|
+
# @param id [String, Integer, nil]
|
122
127
|
# @return [Array] Rack triplet
|
123
|
-
def jsonrpc_error_response(error_sym, status: 400)
|
128
|
+
def jsonrpc_error_response(error_sym, status: 400, id: nil)
|
124
129
|
error_obj = JSON_RPC::JsonRpcError.build(error_sym)
|
125
|
-
payload = JSON_RPC::Response.new(id:
|
130
|
+
payload = JSON_RPC::Response.new(id: id, error: error_obj).to_json
|
126
131
|
|
127
132
|
[ status, { "Content-Type" => CONTENT_TYPE }, [ payload ] ]
|
128
133
|
end
|
134
|
+
|
135
|
+
# Extract ID from raw payload if possible
|
136
|
+
def extract_id_from_raw_payload(raw)
|
137
|
+
return nil unless raw.is_a?(Hash)
|
138
|
+
raw["id"]
|
139
|
+
rescue
|
140
|
+
nil
|
141
|
+
end
|
129
142
|
end
|
130
143
|
end
|
131
144
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsonrpc-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Abdelkader Boudih
|
@@ -67,7 +67,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
69
|
requirements: []
|
70
|
-
rubygems_version: 3.6.
|
70
|
+
rubygems_version: 3.6.9
|
71
71
|
specification_version: 4
|
72
72
|
summary: A Railtie-based gem that brings JSON-RPC 2.0 support to your Rails application.
|
73
73
|
test_files: []
|