jsonrpc-rails 0.5.3 → 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 +4 -4
- data/README.md +52 -0
- data/lib/jsonrpc_rails/middleware/validator.rb +5 -2
- data/lib/jsonrpc_rails/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3c170f3b4c1b8b5d8903fc6202f317d98340c0939dae84117c062009f4128de3
|
4
|
+
data.tar.gz: 047dfecbe033df5e49a41a03d4c471452953358cb41bc95c0e2add3b9666ff81
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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,7 +32,9 @@ module JSONRPC_Rails
|
|
31
32
|
env["CONTENT_TYPE"]&.start_with?(CONTENT_TYPE)
|
32
33
|
|
33
34
|
body = env["rack.input"].read
|
34
|
-
|
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
|
|
@@ -129,7 +132,7 @@ module JSONRPC_Rails
|
|
129
132
|
error_obj = JSON_RPC::JsonRpcError.build(error_sym)
|
130
133
|
payload = JSON_RPC::Response.new(id: id, error: error_obj).to_json
|
131
134
|
|
132
|
-
[ status, { "
|
135
|
+
[ status, { "content-type" => CONTENT_TYPE }, [ payload ] ]
|
133
136
|
end
|
134
137
|
|
135
138
|
# Extract ID from raw payload if possible
|