jsonrpc-rails 0.5.0 → 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: 439108bab45d71dabc7ca40fa35042cdaecd7f3dc6988bc3a13fb0d825ef38ac
4
- data.tar.gz: 34142d82edff4b8099610d44ca19bc039571fbcc0651a44c24588047a141838a
3
+ metadata.gz: 017eda83610eb76324999392d1f824fbc274f5182a3a1412ac3aaef19e9beaa8
4
+ data.tar.gz: 22f7ee6229d94a2f1dce2f03c7205d6c855b37b359a31b3ff6402e6c2fb40e4f
5
5
  SHA512:
6
- metadata.gz: a9b228edf10da4b05019c87643079e3ebe673fb98913dfba6567877293c28e8d2fd3a6507174d2aec8010d8254bab551631b38ac20e3784281773b4546bafb16
7
- data.tar.gz: 3a4cbec99a1dd380a32263d6db57b4550f030aa50c67f29af3b8b25f827dc7d8a5c2be600e282426f36b826a82d393d2a61adda782040aeb0a44f76a455d96ba
6
+ metadata.gz: dac03145eeddd7906005c9ad1770b745b6f6e8a4fd5185b7146b9adc9787cc21fa18697922063ba94e2ae5b3ab64839da27f4fa88fdfbe47c6cd88cb86b93b8e
7
+ data.tar.gz: 298e2f0955364e3175332f0288cffea4646afc410f73393c97573d1f3054fac105563af655a78a768e6ca3f69a920454dd6c3e7a859232df30fe93b9827e4fbc
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 jsonrpc_batch?
114
+ if jsonrpc_params_batch?
115
115
  # ── batch ───────────────────────────────────────────────────────────────
116
- responses = jsonrpc.filter_map { |req| handle_single_request(req) } # strip nil (notifications)
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(jsonrpc)
126
+ response = handle_single_request(jsonrpc_params)
127
127
 
128
128
  if response # request (has id)
129
129
  render jsonrpc: response.result,
@@ -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
@@ -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 jsonrpc
10
+ def jsonrpc_params
11
11
  request.env[:jsonrpc]
12
12
  end
13
13
 
14
14
  # Convenience boolean
15
- def jsonrpc_batch?
16
- jsonrpc.is_a?(Array)
15
+ def jsonrpc_params_batch?
16
+ jsonrpc_params.is_a?(Array)
17
17
  end
18
18
  end
19
19
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JSONRPC_Rails
4
- VERSION = "0.5.0"
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.0
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: []