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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 93b8692cfb780990697a06219f80c323096bd5b12d21b95ec0a70e83dcbe555c
4
- data.tar.gz: a3a15cc185bfb704d6a11cd2085be7beb291c20e727ef9cce317d4b993d5efc4
3
+ metadata.gz: 017eda83610eb76324999392d1f824fbc274f5182a3a1412ac3aaef19e9beaa8
4
+ data.tar.gz: 22f7ee6229d94a2f1dce2f03c7205d6c855b37b359a31b3ff6402e6c2fb40e4f
5
5
  SHA512:
6
- metadata.gz: 305bde8d1013629a31736c9da2de66a90e6d0e860311b784cbac65615646a24ae6460b1cb21699c8c79141027b65c8a988b6f20fbcdbad6d7e1425125fe458df
7
- data.tar.gz: 232740f9c3756071955993b87f7f8ae4832d6159dd6273cd71a6b944003dbc1d68f32692addab5b3ea90d04bfc32908f4572da0791b778e7fdd8f4394abe640d
6
+ metadata.gz: dac03145eeddd7906005c9ad1770b745b6f6e8a4fd5185b7146b9adc9787cc21fa18697922063ba94e2ae5b3ab64839da27f4fa88fdfbe47c6cd88cb86b93b8e
7
+ data.tar.gz: 298e2f0955364e3175332f0288cffea4646afc410f73393c97573d1f3054fac105563af655a78a768e6ca3f69a920454dd6c3e7a859232df30fe93b9827e4fbc
@@ -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:, result: nil, error: nil)
13
- validate_response(id, result, error)
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
- new(id: h["id"], result: h["result"], error: h["error"])
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
- # ID must be present (can be null) in a response matching a request.
48
-
49
- raise ArgumentError, "Response cannot contain both 'result' and 'error'" if !error_input.nil? && !result.nil?
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
- # This check assumes if both are nil, it's invalid for non-null id.
55
- # `result: nil` is a valid success response. The check should ideally know
56
- # if `result` was explicitly passed as nil vs not passed at all.
57
- # Data.define might make this tricky. Let's keep the original logic for now.
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").to_h
89
+ JSON_RPC::JsonRpcError.build(:internal_error, message: "Invalid error format provided")
80
90
  end
81
91
  end
82
92
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JSONRPC_Rails
4
- VERSION = "0.5.1"
4
+ VERSION = "0.5.2"
5
5
  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.1
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.7
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: []