jsonrpc-rails 0.5.2 → 0.5.4

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: 017eda83610eb76324999392d1f824fbc274f5182a3a1412ac3aaef19e9beaa8
4
- data.tar.gz: 22f7ee6229d94a2f1dce2f03c7205d6c855b37b359a31b3ff6402e6c2fb40e4f
3
+ metadata.gz: 3c170f3b4c1b8b5d8903fc6202f317d98340c0939dae84117c062009f4128de3
4
+ data.tar.gz: 047dfecbe033df5e49a41a03d4c471452953358cb41bc95c0e2add3b9666ff81
5
5
  SHA512:
6
- metadata.gz: dac03145eeddd7906005c9ad1770b745b6f6e8a4fd5185b7146b9adc9787cc21fa18697922063ba94e2ae5b3ab64839da27f4fa88fdfbe47c6cd88cb86b93b8e
7
- data.tar.gz: 298e2f0955364e3175332f0288cffea4646afc410f73393c97573d1f3054fac105563af655a78a768e6ca3f69a920454dd6c3e7a859232df30fe93b9827e4fbc
6
+ metadata.gz: 2346088ba624f426d7f26df3f1d3d46faec883d2b45ef929ccd53e4bb1085132e8c0d2491b0f322ade4592ef0ffc182b0e39f15c795a490c7f0e4d2a2a646de1
7
+ data.tar.gz: c76d9df6e6b1e5c7eab1498ac9cf4a544f00c796cab13ae487ff302ac77a500ed488bd920ce9f010391de96430ee888fa68d3388f898646ebd82045e9d919dfd
data/README.md CHANGED
@@ -184,6 +184,58 @@ class MyApiController < ApplicationController
184
184
  end
185
185
  ```
186
186
 
187
+ ## Error Codes
188
+
189
+ The gem implements the complete JSON-RPC 2.0 error specification. All error codes are defined in `lib/json_rpc/json_rpc_error.rb` and can be used either as numeric codes or symbols:
190
+
191
+ | Code | Symbol | Name | Description |
192
+ |------|--------|------|-------------|
193
+ | -32700 | `:parse_error` | Parse Error | Invalid JSON was received by the server |
194
+ | -32600 | `:invalid_request` | Invalid Request | The JSON sent is not a valid Request object |
195
+ | -32601 | `:method_not_found` | Method Not Found | The method does not exist / is not available |
196
+ | -32602 | `:invalid_params` | Invalid Params | Invalid method parameter(s) |
197
+ | -32603 | `:internal_error` | Internal Error | Internal JSON-RPC error |
198
+ | -32000 | `:server_error` | Server Error | Implementation-defined server error |
199
+
200
+ ### Usage Examples
201
+
202
+ ```ruby
203
+ # Using numeric codes
204
+ render jsonrpc: {}, error: -32601, id: 1
205
+
206
+ # Using symbols
207
+ render jsonrpc: {}, error: :method_not_found, id: 1
208
+
209
+ # Creating error objects
210
+ error = JSON_RPC::JsonRpcError.build(:invalid_params)
211
+ render jsonrpc: JSON_RPC::Response.new(id: 1, error: error)
212
+ ```
213
+
214
+ ## JSONRPC Namespace
215
+
216
+ The gem uses two main namespaces to separate core JSON-RPC functionality from Rails integration:
217
+
218
+ ### JSON_RPC Module (Core Specification)
219
+
220
+ Contains the core JSON-RPC 2.0 specification objects:
221
+
222
+ - **`JSON_RPC::Request`** - Represents a JSON-RPC request with method, params, and id
223
+ - **`JSON_RPC::Response`** - Represents a JSON-RPC response with result or error
224
+ - **`JSON_RPC::Notification`** - Represents a JSON-RPC notification (request without id)
225
+ - **`JSON_RPC::JsonRpcError`** - Exception class for JSON-RPC errors with standard error codes
226
+ - **`JSON_RPC::Parser`** - Utility for converting hashes to typed JSON-RPC objects
227
+
228
+ ### JSONRPC_Rails Module (Rails Integration)
229
+
230
+ Contains Rails-specific integration components:
231
+
232
+ - **`JSONRPC_Rails::Railtie`** - Rails initialization, middleware setup, and renderer registration
233
+ - **`JSONRPC_Rails::Middleware::Validator`** - Rack middleware for validating JSON-RPC requests
234
+ - **`JSONRPC_Rails::ControllerHelpers`** - Helper methods like `jsonrpc_params` and `jsonrpc_params_batch?`
235
+ - **`JSONRPC_Rails::VERSION`** - Current gem version
236
+
237
+ This separation ensures clean architecture where core JSON-RPC logic is independent of Rails, while Rails-specific features are properly isolated.
238
+
187
239
  ## Testing
188
240
 
189
241
  A dummy Rails application is included within the gem (located in `test/dummy`) to facilitate testing. You can run the tests from the **project root directory** by executing:
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "json"
4
4
  require "active_support/json"
5
+ require "stringio"
5
6
 
6
7
  module JSONRPC_Rails
7
8
  module Middleware
@@ -31,11 +32,16 @@ module JSONRPC_Rails
31
32
  env["CONTENT_TYPE"]&.start_with?(CONTENT_TYPE)
32
33
 
33
34
  body = env["rack.input"].read
34
- env["rack.input"].rewind
35
+ # Replace consumed input with fresh StringIO for downstream middleware
36
+ # This is Rack 3.0+ compatible and works with all input stream types
37
+ env["rack.input"] = StringIO.new(body)
35
38
 
36
39
  raw_payload = parse_json(body)
37
40
 
38
- return jsonrpc_error_response(:invalid_request) unless raw_payload.is_a?(Hash) || raw_payload.is_a?(Array)
41
+ unless raw_payload.is_a?(Hash) || raw_payload.is_a?(Array)
42
+ id = extract_id_from_raw_payload(raw_payload)
43
+ return jsonrpc_error_response(:invalid_request, id: id)
44
+ end
39
45
 
40
46
  validity, = if raw_payload.is_a?(Array)
41
47
  validate_batch(raw_payload)
@@ -89,9 +95,10 @@ module JSONRPC_Rails
89
95
 
90
96
  def validate_single(obj)
91
97
  if validate_single_structure(obj)
92
- [ :valid, nil ]
98
+ [ :valid, obj["id"] ]
93
99
  else
94
- [ jsonrpc_error_response(:invalid_request), nil ]
100
+ id = obj.is_a?(Hash) ? obj["id"] : nil
101
+ [ jsonrpc_error_response(:invalid_request, id: id), id ]
95
102
  end
96
103
  end
97
104
 
@@ -119,12 +126,21 @@ module JSONRPC_Rails
119
126
 
120
127
  # @param error_sym [Symbol]
121
128
  # @param status [Integer]
129
+ # @param id [String, Integer, nil]
122
130
  # @return [Array] Rack triplet
123
- def jsonrpc_error_response(error_sym, status: 400)
131
+ def jsonrpc_error_response(error_sym, status: 400, id: nil)
124
132
  error_obj = JSON_RPC::JsonRpcError.build(error_sym)
125
- payload = JSON_RPC::Response.new(id: nil, error: error_obj).to_json
133
+ payload = JSON_RPC::Response.new(id: id, error: error_obj).to_json
134
+
135
+ [ status, { "content-type" => CONTENT_TYPE }, [ payload ] ]
136
+ end
126
137
 
127
- [ status, { "Content-Type" => CONTENT_TYPE }, [ payload ] ]
138
+ # Extract ID from raw payload if possible
139
+ def extract_id_from_raw_payload(raw)
140
+ return nil unless raw.is_a?(Hash)
141
+ raw["id"]
142
+ rescue
143
+ nil
128
144
  end
129
145
  end
130
146
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JSONRPC_Rails
4
- VERSION = "0.5.2"
4
+ VERSION = "0.5.4"
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.2
4
+ version: 0.5.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Abdelkader Boudih