jsonrpc-middleware 0.1.0

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.
Files changed (49) hide show
  1. checksums.yaml +7 -0
  2. data/.claude/settings.local.json +9 -0
  3. data/.editorconfig +11 -0
  4. data/.overcommit.yml +31 -0
  5. data/.rspec +3 -0
  6. data/.rubocop.yml +74 -0
  7. data/.tool-versions +1 -0
  8. data/.yardstick.yml +22 -0
  9. data/CHANGELOG.md +37 -0
  10. data/CODE_OF_CONDUCT.md +132 -0
  11. data/Guardfile +22 -0
  12. data/LICENSE.txt +21 -0
  13. data/README.md +248 -0
  14. data/Rakefile +41 -0
  15. data/Steepfile +7 -0
  16. data/docs/JSON-RPC-2.0-Specification.md +278 -0
  17. data/examples/procedures.rb +55 -0
  18. data/examples/rack/Gemfile +8 -0
  19. data/examples/rack/Gemfile.lock +68 -0
  20. data/examples/rack/README.md +7 -0
  21. data/examples/rack/app.rb +48 -0
  22. data/examples/rack/config.ru +19 -0
  23. data/examples/rack-echo/Gemfile +8 -0
  24. data/examples/rack-echo/Gemfile.lock +68 -0
  25. data/examples/rack-echo/README.md +7 -0
  26. data/examples/rack-echo/app.rb +43 -0
  27. data/examples/rack-echo/config.ru +18 -0
  28. data/lib/jsonrpc/batch_request.rb +102 -0
  29. data/lib/jsonrpc/batch_response.rb +85 -0
  30. data/lib/jsonrpc/configuration.rb +85 -0
  31. data/lib/jsonrpc/error.rb +96 -0
  32. data/lib/jsonrpc/errors/internal_error.rb +27 -0
  33. data/lib/jsonrpc/errors/invalid_params_error.rb +27 -0
  34. data/lib/jsonrpc/errors/invalid_request_error.rb +31 -0
  35. data/lib/jsonrpc/errors/method_not_found_error.rb +31 -0
  36. data/lib/jsonrpc/errors/parse_error.rb +29 -0
  37. data/lib/jsonrpc/helpers.rb +83 -0
  38. data/lib/jsonrpc/middleware.rb +190 -0
  39. data/lib/jsonrpc/notification.rb +94 -0
  40. data/lib/jsonrpc/parser.rb +176 -0
  41. data/lib/jsonrpc/request.rb +112 -0
  42. data/lib/jsonrpc/response.rb +127 -0
  43. data/lib/jsonrpc/validator.rb +140 -0
  44. data/lib/jsonrpc/version.rb +5 -0
  45. data/lib/jsonrpc.rb +25 -0
  46. data/sig/jsonrpc/middleware.rbs +6 -0
  47. data/sig/jsonrpc/parser.rbs +7 -0
  48. data/sig/jsonrpc.rbs +164 -0
  49. metadata +120 -0
@@ -0,0 +1,278 @@
1
+ # JSON-RPC 2.0 Specification
2
+
3
+ ## 1 Overview
4
+
5
+ JSON-RPC is a stateless, light-weight remote procedure call (RPC) protocol. Primarily this specification defines several
6
+ data structures and the rules around their processing. It is transport agnostic in that the concepts can be used within
7
+ the same process, over sockets, over http, or in many various message passing environments. It uses
8
+ [JSON](http://www.json.org)([RFC 4627](http://www.ietf.org/rfc/rfc4627.txt)) as data format.
9
+
10
+ It is designed to be simple!
11
+
12
+ ## 2 Conventions
13
+
14
+ The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and
15
+ "OPTIONAL" in this document are to be interpreted as described in [RFC 2119](http://www.ietf.org/rfc/rfc2119.txt).
16
+
17
+ Since JSON-RPC utilizes JSON, it has the same type system (see [http://www.json.org](http://www.json.org) or
18
+ [RFC 4627](http://www.ietf.org/rfc/rfc4627.txt)). JSON can represent four primitive types (Strings, Numbers, Booleans,
19
+ and Null) and two structured types (Objects and Arrays). The term "Primitive" in this specification references any of
20
+ those four primitive JSON types. The term "Structured" references either of the structured JSON types. Whenever this
21
+ document refers to any JSON type, the first letter is always capitalized: Object, Array, String, Number, Boolean, Null.
22
+ True and False are also capitalized.
23
+
24
+ All member names exchanged between the Client and the Server that are considered for matching of any kind should be
25
+ considered to be case-sensitive. The terms function, method, and procedure can be assumed to be interchangeable.
26
+
27
+ The Client is defined as the origin of Request objects and the handler of Response objects.
28
+ The Server is defined as the origin of Response objects and the handler of Request objects.
29
+
30
+ One implementation of this specification could easily fill both of those roles, even at the same time, to other
31
+ different clients or the same client. This specification does not address that layer of complexity.
32
+
33
+ ## 3 Compatibility
34
+
35
+ JSON-RPC 2.0 Request objects and Response objects may not work with existing JSON-RPC 1.0 clients or servers. However,
36
+ it is easy to distinguish between the two versions as 2.0 always has a member named "jsonrpc" with a String value of
37
+ "2.0"whereas 1.0 does not. Most 2.0 implementations should consider trying to handle 1.0 objects, even if not the
38
+ peer-to-peer and class hinting aspects of 1.0.
39
+
40
+ ## 4 Request object
41
+
42
+ A rpc call is represented by sending a Request object to a Server. The Request object has the following members:
43
+
44
+ **jsonrpc**
45
+ A String specifying the version of the JSON-RPC protocol. MUST be exactly "2.0".
46
+
47
+ **method**
48
+ A String containing the name of the method to be invoked. Method names that begin with the word rpc followed by a period
49
+ character (U+002E or ASCII 46) are reserved for rpc-internal methods and extensions and MUST NOT be used for anything
50
+ else.
51
+
52
+ **params**
53
+ A Structured value that holds the parameter values to be used during the invocation of the method. This member MAY be
54
+ omitted.
55
+
56
+ **id**
57
+ An identifier established by the Client that MUST contain a String, Number, or NULL value if included. If it is not
58
+ included it is assumed to be a notification. The value SHOULD normally not be Null [1] and Numbers SHOULD NOT contain
59
+ fractional parts [2]
60
+
61
+ The Server MUST reply with the same value in the Response object if included. This member is used to correlate the
62
+ context between the two objects.
63
+
64
+ [1] The use of Null as a value for the id member in a Request object is discouraged, because this specification uses a
65
+ value of Null for Responses with an unknown id. Also, because JSON-RPC 1.0 uses an id value of Null for Notifications
66
+ this could cause confusion in handling.
67
+
68
+ [2] Fractional parts may be problematic, since many decimal fractions cannot be represented exactly as binary fractions.
69
+
70
+ ### 4.1 Notification
71
+
72
+ A Notification is a Request object without an "id" member. A Request object that is a Notification signifies the
73
+ Client's lack of interest in the corresponding Response object, and as such no Response object needs to be returned to
74
+ the client. The Server MUST NOT reply to a Notification, including those that are within a batch request.
75
+
76
+ Notifications are not confirmable by definition, since they do not have a Response object to be returned. As such, the
77
+ Client would not be aware of any errors (like e.g. "Invalid params","Internal error").
78
+
79
+ ### 4.2 Parameter Structures
80
+
81
+ If present, parameters for the rpc call MUST be provided as a Structured value. Either by-position through an Array or
82
+ by-name through an Object.
83
+
84
+ - **by-position**: params MUST be an Array, containing the values in the Server expected order.
85
+ - **by-name**: params MUST be an Object, with member names that match the Server expected parameter names. The absence
86
+ of expected names MAY result in an error being generated. The names MUST match exactly, including case, to the
87
+ method's expected parameters.
88
+
89
+ ## 5 Response object
90
+
91
+ When a rpc call is made, the Server MUST reply with a Response, except for in the case of Notifications. The Response is
92
+ expressed as a single JSON Object, with the following members:
93
+
94
+ **jsonrpc**
95
+ A String specifying the version of the JSON-RPC protocol. MUST be exactly "2.0".
96
+
97
+ **result**
98
+ This member is REQUIRED on success.
99
+ This member MUST NOT exist if there was an error invoking the method.
100
+ The value of this member is determined by the method invoked on the Server.
101
+
102
+ **error**
103
+ This member is REQUIRED on error.
104
+ This member MUST NOT exist if there was no error triggered during invocation.
105
+ The value for this member MUST be an Object as defined in section 5.1.
106
+
107
+ **id**
108
+ This member is REQUIRED.
109
+ It MUST be the same as the value of the id member in the Request Object.
110
+ If there was an error in detecting the id in the Request object (e.g. Parse error/Invalid Request), it MUST be Null.
111
+
112
+ Either the result member or error member MUST be included, but both members MUST NOT be included.
113
+
114
+ ### 5.1 Error object
115
+
116
+ When a rpc call encounters an error, the Response Object MUST contain the error member with a value that is a Object
117
+ with the following members:
118
+
119
+ **code**
120
+ A Number that indicates the error type that occurred.
121
+ This MUST be an integer.
122
+
123
+ **message**
124
+ A String providing a short description of the error.
125
+ The message SHOULD be limited to a concise single sentence.
126
+
127
+ **data**
128
+ A Primitive or Structured value that contains additional information about the error.
129
+ This may be omitted.
130
+ The value of this member is defined by the Server (e.g. detailed error information, nested errors etc.).
131
+
132
+ The error codes from and including -32768 to -32000 are reserved for pre-defined errors. Any code within this range, but
133
+ not defined explicitly below is reserved for future use.
134
+
135
+ | code | message | meaning |
136
+ |------|---------|---------|
137
+ | -32700 | Parse error | Invalid JSON was received by the server.<br>An error occurred on the server while parsing the JSON text. |
138
+ | -32600 | Invalid Request | The JSON sent is not a valid Request object. |
139
+ | -32601 | Method not found | The method does not exist / is not available. |
140
+ | -32602 | Invalid params | Invalid method parameter(s). |
141
+ | -32603 | Internal error | Internal JSON-RPC error. |
142
+ | -32000 to -32099 | Server error | Reserved for implementation-defined server-errors. |
143
+
144
+ The remainder of the space is available for application defined errors.
145
+
146
+ ## 6 Batch
147
+
148
+ To send several Request objects at the same time, the Client MAY send an Array filled with Request objects.
149
+
150
+ The Server should respond with an Array containing the corresponding Response objects, after all of the batch Request
151
+ objects have been processed. A Response object SHOULD exist for each Request object, except that there SHOULD NOT be
152
+ any Response objects for notifications. The Server MAY process a batch rpc call as a set of concurrent tasks,
153
+ processing them in any order and with any width of parallelism.
154
+
155
+ The Response objects being returned from a batch call MAY be returned in any order within the Array. The Client SHOULD
156
+ match contexts between the set of Request objects and the resulting set of Response objects based on the id member
157
+ within each Object.
158
+
159
+ If the batch rpc call itself fails to be recognized as an valid JSON or as an Array with at least one value, the
160
+ response from the Server MUST be a single Response object. If there are no Response objects contained within the
161
+ Response array as it is to be sent to the client, the server MUST NOT return an empty Array and should return nothing
162
+ at all.
163
+
164
+ ## 7 Examples
165
+
166
+ Syntax:
167
+ ```
168
+ --> data sent to Server
169
+ <-- data sent to Client
170
+ ```
171
+
172
+ **rpc call with positional parameters:**
173
+ ```
174
+ --> {"jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "id": 1}
175
+ <-- {"jsonrpc": "2.0", "result": 19, "id": 1}
176
+
177
+ --> {"jsonrpc": "2.0", "method": "subtract", "params": [23, 42], "id": 2}
178
+ <-- {"jsonrpc": "2.0", "result": -19, "id": 2}
179
+ ```
180
+
181
+ **rpc call with named parameters:**
182
+ ```
183
+ --> {"jsonrpc": "2.0", "method": "subtract", "params": {"subtrahend": 23, "minuend": 42}, "id": 3}
184
+ <-- {"jsonrpc": "2.0", "result": 19, "id": 3}
185
+
186
+ --> {"jsonrpc": "2.0", "method": "subtract", "params": {"minuend": 42, "subtrahend": 23}, "id": 4}
187
+ <-- {"jsonrpc": "2.0", "result": 19, "id": 4}
188
+ ```
189
+
190
+ **a Notification:**
191
+ ```
192
+ --> {"jsonrpc": "2.0", "method": "update", "params": [1,2,3,4,5]}
193
+ --> {"jsonrpc": "2.0", "method": "foobar"}
194
+ ```
195
+
196
+ **rpc call of non-existent method:**
197
+ ```
198
+ --> {"jsonrpc": "2.0", "method": "foobar", "id": "1"}
199
+ <-- {"jsonrpc": "2.0", "error": {"code": -32601, "message": "Method not found"}, "id": "1"}
200
+ ```
201
+
202
+ **rpc call with invalid JSON:**
203
+ ```
204
+ --> {"jsonrpc": "2.0", "method": "foobar, "params": "bar", "baz]
205
+ <-- {"jsonrpc": "2.0", "error": {"code": -32700, "message": "Parse error"}, "id": null}
206
+ ```
207
+
208
+ **rpc call with invalid Request object:**
209
+ ```
210
+ --> {"jsonrpc": "2.0", "method": 1, "params": "bar"}
211
+ <-- {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null}
212
+ ```
213
+
214
+ **rpc call Batch, invalid JSON:**
215
+ ```
216
+ --> [
217
+ {"jsonrpc": "2.0", "method": "sum", "params": [1,2,4], "id": "1"},
218
+ {"jsonrpc": "2.0", "method"
219
+ ]
220
+ <-- {"jsonrpc": "2.0", "error": {"code": -32700, "message": "Parse error"}, "id": null}
221
+ ```
222
+
223
+ **rpc call with an empty Array:**
224
+ ```
225
+ --> []
226
+ <-- {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null}
227
+ ```
228
+
229
+ **rpc call with an invalid Batch (but not empty):**
230
+ ```
231
+ --> [1]
232
+ <-- [
233
+ {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null}
234
+ ]
235
+ ```
236
+
237
+ **rpc call with invalid Batch:**
238
+ ```
239
+ --> [1,2,3]
240
+ <-- [
241
+ {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null},
242
+ {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null},
243
+ {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null}
244
+ ]
245
+ ```
246
+
247
+ **rpc call Batch:**
248
+ ```
249
+ --> [
250
+ {"jsonrpc": "2.0", "method": "sum", "params": [1,2,4], "id": "1"},
251
+ {"jsonrpc": "2.0", "method": "notify_hello", "params": [7]},
252
+ {"jsonrpc": "2.0", "method": "subtract", "params": [42,23], "id": "2"},
253
+ {"foo": "boo"},
254
+ {"jsonrpc": "2.0", "method": "foo.get", "params": {"name": "myself"}, "id": "5"},
255
+ {"jsonrpc": "2.0", "method": "get_data", "id": "9"}
256
+ ]
257
+ <-- [
258
+ {"jsonrpc": "2.0", "result": 7, "id": "1"},
259
+ {"jsonrpc": "2.0", "result": 19, "id": "2"},
260
+ {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null},
261
+ {"jsonrpc": "2.0", "error": {"code": -32601, "message": "Method not found"}, "id": "5"},
262
+ {"jsonrpc": "2.0", "result": ["hello", 5], "id": "9"}
263
+ ]
264
+ ```
265
+
266
+ **rpc call Batch (all notifications):**
267
+ ```
268
+ --> [
269
+ {"jsonrpc": "2.0", "method": "notify_sum", "params": [1,2,4]},
270
+ {"jsonrpc": "2.0", "method": "notify_hello", "params": [7]}
271
+ ]
272
+ <-- //Nothing is returned for all notification batches
273
+ ```
274
+
275
+ ## 8 Extensions
276
+
277
+ Method names that begin with rpc. are reserved for system extensions, and MUST NOT be used for anything else. Each
278
+ system extension is defined in a related specification. All system extensions are OPTIONAL.
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ JSONRPC.configure do
4
+ # Allow positional and named arguments
5
+ procedure(:add, allow_positional_arguments: true) do
6
+ params do
7
+ required(:addends).filled(:array)
8
+ required(:addends).value(:array).each(type?: Numeric)
9
+ end
10
+
11
+ rule(:addends) do
12
+ key.failure('must contain at least one addend') if value.empty?
13
+ end
14
+ end
15
+
16
+ procedure(:subtract) do
17
+ params do
18
+ required(:minuend).filled(:integer)
19
+ required(:subtrahend).filled(:integer)
20
+ end
21
+ end
22
+
23
+ procedure(:multiply) do
24
+ params do
25
+ required(:multiplicand).filled
26
+ required(:multiplier).filled
27
+ end
28
+
29
+ rule(:multiplicand) do
30
+ key.failure('must be a number') unless value.is_a?(Numeric)
31
+ end
32
+
33
+ rule(:multiplier) do
34
+ key.failure('must be a number') unless value.is_a?(Numeric)
35
+ end
36
+ end
37
+
38
+ procedure(:divide) do
39
+ params do
40
+ required(:dividend).filled(:integer)
41
+ required(:divisor).filled(:integer)
42
+ end
43
+
44
+ rule(:divisor) do
45
+ key.failure('cannot be zero') if value.zero?
46
+ end
47
+ end
48
+
49
+ # Used only to test internal server errors
50
+ procedure(:explode) do
51
+ params do
52
+ # No params
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gem 'jsonrpc-middleware', path: '../..'
6
+ gem 'puma'
7
+ gem 'rack'
8
+ gem 'rackup'
@@ -0,0 +1,68 @@
1
+ PATH
2
+ remote: ../..
3
+ specs:
4
+ jsonrpc-middleware (0.1.0)
5
+ dry-validation (~> 1.11)
6
+ zeitwerk (~> 2.7)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ bigdecimal (3.1.9)
12
+ concurrent-ruby (1.3.5)
13
+ dry-configurable (1.3.0)
14
+ dry-core (~> 1.1)
15
+ zeitwerk (~> 2.6)
16
+ dry-core (1.1.0)
17
+ concurrent-ruby (~> 1.0)
18
+ logger
19
+ zeitwerk (~> 2.6)
20
+ dry-inflector (1.2.0)
21
+ dry-initializer (3.2.0)
22
+ dry-logic (1.6.0)
23
+ bigdecimal
24
+ concurrent-ruby (~> 1.0)
25
+ dry-core (~> 1.1)
26
+ zeitwerk (~> 2.6)
27
+ dry-schema (1.14.1)
28
+ concurrent-ruby (~> 1.0)
29
+ dry-configurable (~> 1.0, >= 1.0.1)
30
+ dry-core (~> 1.1)
31
+ dry-initializer (~> 3.2)
32
+ dry-logic (~> 1.5)
33
+ dry-types (~> 1.8)
34
+ zeitwerk (~> 2.6)
35
+ dry-types (1.8.2)
36
+ bigdecimal (~> 3.0)
37
+ concurrent-ruby (~> 1.0)
38
+ dry-core (~> 1.0)
39
+ dry-inflector (~> 1.0)
40
+ dry-logic (~> 1.4)
41
+ zeitwerk (~> 2.6)
42
+ dry-validation (1.11.1)
43
+ concurrent-ruby (~> 1.0)
44
+ dry-core (~> 1.1)
45
+ dry-initializer (~> 3.2)
46
+ dry-schema (~> 1.14)
47
+ zeitwerk (~> 2.6)
48
+ logger (1.7.0)
49
+ nio4r (2.7.4)
50
+ puma (6.6.0)
51
+ nio4r (~> 2.0)
52
+ rack (3.1.14)
53
+ rackup (2.2.1)
54
+ rack (>= 3)
55
+ zeitwerk (2.7.2)
56
+
57
+ PLATFORMS
58
+ arm64-darwin-24
59
+ ruby
60
+
61
+ DEPENDENCIES
62
+ jsonrpc-middleware!
63
+ puma
64
+ rack
65
+ rackup
66
+
67
+ BUNDLED WITH
68
+ 2.6.8
@@ -0,0 +1,7 @@
1
+ # Rack
2
+
3
+ ## Running
4
+
5
+ ```sh
6
+ bundle exec rackup
7
+ ```
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Complex example usage of json-middleware in a Rack application with helpers
4
+ class App
5
+ include JSONRPC::Helpers
6
+
7
+ def call(env)
8
+ @env = env # Set the @env instance variable to use the JSONRPC helpers below
9
+
10
+ if jsonrpc_request?
11
+ result = handle_single(jsonrpc_request)
12
+ jsonrpc_response(result)
13
+ elsif jsonrpc_notification?
14
+ handle_single(jsonrpc_notification)
15
+ jsonrpc_notification_response
16
+ else
17
+ responses = handle_batch(jsonrpc_batch)
18
+ jsonrpc_batch_response(responses)
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def handle_single(request_or_notification)
25
+ params = request_or_notification.params
26
+
27
+ case request_or_notification.method
28
+ when 'add'
29
+ addends = params.is_a?(Array) ? params : { addends: params } # Handle positional and named arguments
30
+ addends.sum
31
+ when 'subtract'
32
+ params['minuend'] - params['subtrahend']
33
+ when 'multiply'
34
+ params['multiplicand'] * params['multiplier']
35
+ when 'divide'
36
+ params['dividend'] / params['divisor']
37
+ when 'explode'
38
+ raise 'An internal error has occurred.'
39
+ end
40
+ end
41
+
42
+ def handle_batch(batch)
43
+ batch.flat_map do |request_or_notification|
44
+ result = handle_single(request_or_notification)
45
+ JSONRPC::Response.new(id: request_or_notification.id, result:) if request_or_notification.is_a?(JSONRPC::Request)
46
+ end.compact
47
+ end
48
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'jsonrpc'
4
+ require '../procedures'
5
+ require './app'
6
+
7
+ # Rack builder syntax
8
+ #
9
+ app = Rack::Builder.new do
10
+ use JSONRPC::Middleware
11
+ run App.new
12
+ end
13
+
14
+ run app
15
+
16
+ # Rack classic syntax
17
+ #
18
+ # use JSONRPC::Middleware
19
+ # run App.new
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gem 'jsonrpc-middleware', path: '../..'
6
+ gem 'puma'
7
+ gem 'rack'
8
+ gem 'rackup'
@@ -0,0 +1,68 @@
1
+ PATH
2
+ remote: ../..
3
+ specs:
4
+ jsonrpc-middleware (0.1.0)
5
+ dry-validation (~> 1.11)
6
+ zeitwerk (~> 2.7)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ bigdecimal (3.1.9)
12
+ concurrent-ruby (1.3.5)
13
+ dry-configurable (1.3.0)
14
+ dry-core (~> 1.1)
15
+ zeitwerk (~> 2.6)
16
+ dry-core (1.1.0)
17
+ concurrent-ruby (~> 1.0)
18
+ logger
19
+ zeitwerk (~> 2.6)
20
+ dry-inflector (1.2.0)
21
+ dry-initializer (3.2.0)
22
+ dry-logic (1.6.0)
23
+ bigdecimal
24
+ concurrent-ruby (~> 1.0)
25
+ dry-core (~> 1.1)
26
+ zeitwerk (~> 2.6)
27
+ dry-schema (1.14.1)
28
+ concurrent-ruby (~> 1.0)
29
+ dry-configurable (~> 1.0, >= 1.0.1)
30
+ dry-core (~> 1.1)
31
+ dry-initializer (~> 3.2)
32
+ dry-logic (~> 1.5)
33
+ dry-types (~> 1.8)
34
+ zeitwerk (~> 2.6)
35
+ dry-types (1.8.2)
36
+ bigdecimal (~> 3.0)
37
+ concurrent-ruby (~> 1.0)
38
+ dry-core (~> 1.0)
39
+ dry-inflector (~> 1.0)
40
+ dry-logic (~> 1.4)
41
+ zeitwerk (~> 2.6)
42
+ dry-validation (1.11.1)
43
+ concurrent-ruby (~> 1.0)
44
+ dry-core (~> 1.1)
45
+ dry-initializer (~> 3.2)
46
+ dry-schema (~> 1.14)
47
+ zeitwerk (~> 2.6)
48
+ logger (1.7.0)
49
+ nio4r (2.7.4)
50
+ puma (6.6.0)
51
+ nio4r (~> 2.0)
52
+ rack (3.1.14)
53
+ rackup (2.2.1)
54
+ rack (>= 3)
55
+ zeitwerk (2.7.2)
56
+
57
+ PLATFORMS
58
+ arm64-darwin-24
59
+ ruby
60
+
61
+ DEPENDENCIES
62
+ jsonrpc-middleware!
63
+ puma
64
+ rack
65
+ rackup
66
+
67
+ BUNDLED WITH
68
+ 2.6.8
@@ -0,0 +1,7 @@
1
+ # Rack
2
+
3
+ ## Running
4
+
5
+ ```sh
6
+ bundle exec rackup
7
+ ```
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Simplest usage of json-middleware in a Rack application with helpers
4
+ class App
5
+ include JSONRPC::Helpers
6
+
7
+ def call(env)
8
+ @env = env # Set the @env instance variable to use the JSONRPC helpers below
9
+
10
+ if jsonrpc_request?
11
+ result = handle_single(jsonrpc_request)
12
+ jsonrpc_response(result)
13
+ elsif jsonrpc_notification?
14
+ handle_single(jsonrpc_notification)
15
+ jsonrpc_notification_response
16
+ else
17
+ responses = handle_batch(jsonrpc_batch)
18
+ jsonrpc_batch_response(responses)
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def handle_single(request_or_notification)
25
+ params = request_or_notification.params
26
+ params['message']
27
+ end
28
+
29
+ def handle_batch(batch)
30
+ batch.flat_map do |request_or_notification|
31
+ result = handle_single(request_or_notification)
32
+ JSONRPC::Response.new(id: request_or_notification.id, result:) if request_or_notification.is_a?(JSONRPC::Request)
33
+ end.compact
34
+ end
35
+ end
36
+
37
+ JSONRPC.configure do
38
+ procedure(:echo) do
39
+ params do
40
+ required(:message).filled(:string)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'jsonrpc'
4
+ require './app'
5
+
6
+ # Rack builder syntax
7
+ #
8
+ app = Rack::Builder.new do
9
+ use JSONRPC::Middleware
10
+ run App.new
11
+ end
12
+
13
+ run app
14
+
15
+ # Rack classic syntax
16
+ #
17
+ # use JSONRPC::Middleware
18
+ # run App.new