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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5ca6d0bdf5ffc4f71a7be0e4669bad7bd1a215a647d9e5604be69b40caa86acd
4
- data.tar.gz: 27359e3bc0eeaab09c659fd618f20eb15448293180d4f437fd0cd1deff1584ef
3
+ metadata.gz: 93b8692cfb780990697a06219f80c323096bd5b12d21b95ec0a70e83dcbe555c
4
+ data.tar.gz: a3a15cc185bfb704d6a11cd2085be7beb291c20e727ef9cce317d4b993d5efc4
5
5
  SHA512:
6
- metadata.gz: fc155ba77be68184eaeefc330a0f1829e5c0854da1cce49b7abddc870cbc158eccac3881d0bf4d00af932609aae4b84abd99868761d2ccb074840456c9d6e0b8
7
- data.tar.gz: 866302ed5d32d518fbd3fc6be6cfd34c7a0e7d3f4e4466cd96cb99a102ab5795b9f29a14501e3ed96332e22472892d764087fc0668898e11dbe2fb90b9ff16ab
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 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,
@@ -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,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 = [] # By default, we inject it into the void.
10
- # Insert the JSON-RPC Validator middleware early in the stack.
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
- # Use the Response class to build the payload
19
- response_id = options[:id] # ID is required for Response
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
- response_obj = case error_input
25
- when Symbol
26
- # Build error from symbol, allowing overrides from payload_obj
27
- message_override = payload_obj.is_a?(Hash) ? payload_obj[:message] : nil
28
- data_override = payload_obj.is_a?(Hash) ? payload_obj[:data] : nil
29
- error_hash = JSON_RPC::JsonRpcError.build(error_input, message: message_override,
30
- data: data_override)
31
- JSON_RPC::Response.new(id: response_id, error: error_hash)
32
- when Integer
33
- # Build error from numeric code, allowing overrides from payload_obj
34
- error_code = error_input
35
- default_details = JSON_RPC::JsonRpcError.find_by_code(error_code)
36
- message_override = payload_obj.is_a?(Hash) ? payload_obj[:message] : nil
37
- data_override = payload_obj.is_a?(Hash) ? payload_obj[:data] : nil
38
- error_hash = {
39
- code: error_code,
40
- message: message_override || default_details&.fetch(:message, "Unknown error") # Use override, default, or generic
41
- }
42
- error_hash[:data] = data_override if data_override
43
- JSON_RPC::Response.new(id: response_id, error: error_hash)
44
- when ->(ei) { ei } # Catch any other truthy value
45
- raise ArgumentError,
46
- "The :error option for render :jsonrpc must be a Symbol or an Integer, got: #{error_input.inspect}"
47
- # # Original logic (removed): Treat payload_obj as the error hash
48
- # JSON_RPC::Response.new(id: response_id, error: payload_obj)
49
- else # Falsy (nil, false)
50
- # Treat payload_obj as the result
51
- JSON_RPC::Response.new(id: response_id, result: payload_obj)
52
- end
53
- response_payload = response_obj.to_h
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
- # Handle cases where Response initialization fails (e.g., invalid id/result/error combo)
56
- # Respond with an Internal Error according to JSON-RPC spec
57
- internal_error = JSON_RPC::JsonRpcError.new(:internal_error,
58
- message: "Server error generating response: #{e.message}")
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
- # Handle specific JsonRpcError during Response processing (e.g., invalid error symbol)
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 = response_payload.to_json
94
+ self.response_body = payload.to_json
70
95
  end
71
96
  end
72
97
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JSONRPC_Rails
4
- VERSION = "0.4.0"
4
+ VERSION = "0.5.1"
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.4.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Abdelkader Boudih