jsonrpc-rails 0.5.1 → 0.5.2
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/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: 017eda83610eb76324999392d1f824fbc274f5182a3a1412ac3aaef19e9beaa8
|
4
|
+
data.tar.gz: 22f7ee6229d94a2f1dce2f03c7205d6c855b37b359a31b3ff6402e6c2fb40e4f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dac03145eeddd7906005c9ad1770b745b6f6e8a4fd5185b7146b9adc9787cc21fa18697922063ba94e2ae5b3ab64839da27f4fa88fdfbe47c6cd88cb86b93b8e
|
7
|
+
data.tar.gz: 298e2f0955364e3175332f0288cffea4646afc410f73393c97573d1f3054fac105563af655a78a768e6ca3f69a920454dd6c3e7a859232df30fe93b9827e4fbc
|
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
|
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.2
|
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: []
|