Skip to content

Commit 74c1d57

Browse files
committed
Add batch JSON-RPC request handling to example app
Introduces a new route and controller action to handle batch JSON-RPC requests in the Rails single-file routing example. Updates the README with documentation and usage examples for batch requests, alongside individual 'echo' and 'ping' methods.
1 parent 161a5d0 commit 74c1d57

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

examples/rails-single-file-routing/README.md

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ Uses constraints to route JSON-RPC requests to different Rails controller action
1010
class App < Rails::Application
1111
# ...
1212
routes.append do
13+
# Handle batch requests
14+
post '/', to: 'jsonrpc#ping_or_echo', constraints: JSONRPC::BatchConstraint.new
15+
16+
# Handle individual method requests
1317
post '/', to: 'jsonrpc#echo', constraints: JSONRPC::MethodConstraint.new('echo')
1418
post '/', to: 'jsonrpc#ping', constraints: JSONRPC::MethodConstraint.new('ping')
1519
end
@@ -25,6 +29,20 @@ class JsonrpcController < ActionController::Base
2529
def ping
2630
render jsonrpc: 'pong'
2731
end
32+
33+
# POST /
34+
def ping_or_echo
35+
results = jsonrpc_batch.process_each do |request_or_notification|
36+
case request_or_notification.method
37+
when 'echo'
38+
request_or_notification.params
39+
when 'ping'
40+
'pong'
41+
end
42+
end
43+
44+
render jsonrpc: results
45+
end
2846
end
2947
```
3048

@@ -36,18 +54,35 @@ bundle exec rackup
3654

3755
## API
3856

39-
The server implements an echo API with these procedures:
57+
The server implements these procedures:
4058

4159
- `echo` - Returns the input message
42-
- `ping` - Returns "pong"
60+
- `ping` - Returns `'pong'`
4361

4462
## Example Requests
4563

64+
Echo request:
4665
```sh
4766
curl -X POST http://localhost:9292 \
4867
-H "Content-Type: application/json" \
4968
-d '{"jsonrpc": "2.0", "method": "echo", "params": {"message": "Hello, World!"}, "id": 1}'
69+
```
70+
71+
Ping request:
72+
```sh
73+
curl -X POST http://localhost:9292 \
74+
-H "Content-Type: application/json" \
75+
-d '{"jsonrpc": "2.0", "method": "ping", "params": {}, "id": 2}'
76+
```
77+
78+
Batch request with multiple methods:
79+
```sh
80+
# Batch request with multiple methods
5081
curl -X POST http://localhost:9292 \
5182
-H "Content-Type: application/json" \
52-
-d '{"jsonrpc": "2.0""method": "ping", "params": {}, "id": 2}'
83+
-d '[
84+
{"jsonrpc": "2.0", "method": "echo", "params": {"message": "Hello from batch!"}, "id": 1},
85+
{"jsonrpc": "2.0", "method": "ping", "params": {}, "id": 2},
86+
{"jsonrpc": "2.0", "method": "echo", "params": {"message": "Another echo"}, "id": 3}
87+
]'
5388
```

examples/rails-single-file-routing/config.ru

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ class App < Rails::Application
3737
config.hosts.clear
3838

3939
routes.append do
40+
# Handle batch requests
41+
post '/', to: 'jsonrpc#ping_or_echo', constraints: JSONRPC::BatchConstraint.new
42+
43+
# Handle individual method requests
4044
post '/', to: 'jsonrpc#echo', constraints: JSONRPC::MethodConstraint.new('echo')
4145
post '/', to: 'jsonrpc#ping', constraints: JSONRPC::MethodConstraint.new('ping')
4246
end
@@ -51,6 +55,19 @@ class JsonrpcController < ActionController::Base
5155
def ping
5256
render jsonrpc: 'pong'
5357
end
58+
59+
def ping_or_echo
60+
results = jsonrpc_batch.process_each do |request_or_notification|
61+
case request_or_notification.method
62+
when 'echo'
63+
request_or_notification.params
64+
when 'ping'
65+
'pong'
66+
end
67+
end
68+
69+
render jsonrpc: results
70+
end
5471
end
5572

5673
App.initialize!

0 commit comments

Comments
 (0)