jsonrpc-middleware 0.2.0 → 0.3.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ea48adea36c2a7f311bb0b5c8d1868999f9ec7b6530a2b933600f8433655b63c
4
- data.tar.gz: 8e4abe926bb02da6155650d2296d5df0b07562de6ce08163ad433df09ac40e1d
3
+ metadata.gz: be698b3797653f7d1139c034b97f4be0895cc181f840c4fe3d9edfb9624fb0a2
4
+ data.tar.gz: 3276fd9d13964cf6fe92f9a64ffe5bfac21bef767b3fde94328bc744ca2c8731
5
5
  SHA512:
6
- metadata.gz: 90b63747650ce423ae7f5f881db75c0c8fceabc05e627fa0fd6647cd6b67c1445eab3a09dfc19cac3b5d62d9814e9a692b96e50e0bbdc41bd8b83d9d3582b6bc
7
- data.tar.gz: 64c40bf854999ce3c0cdc8261a921d603ed8ec664c46a6e0d4578afbc9404e693f7b8ff5456385fdcb671867049d4e07028efa51ac48276a5adfd5c4a24b194d
6
+ metadata.gz: e4da3b7c0bd1fedc61baa9b8c4e9dc7fbd90c60c3e8509a676bd72b35d224a5928280bb56c5c4a7c92a40e5958544b81544e8461a551be818b8dd41b433416c0
7
+ data.tar.gz: cd7f389f4bf622cd9191c19dbba17890fa260259bd590d8d4bf9629dfc49f7573a05b772f25050c283fedc3b7f1d3899828ae1009a3b5deb52bc9911264c94ab
data/.aiexclude CHANGED
@@ -1,3 +1,4 @@
1
+ .kiro
1
2
  .ruby-lsp
2
3
  .vscode
3
4
  coverage
data/.tool-versions CHANGED
@@ -1 +1 @@
1
- ruby 3.4.4
1
+ ruby 3.4.5
data/CHANGELOG.md CHANGED
@@ -7,6 +7,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.3.0] - 2025-07-17
11
+
12
+ ### Added
13
+ - Rails routing support with method constraints for JSON-RPC procedures
14
+ - Rails single-file routing example application
15
+ - Method constraint helper for Rails routing integration
16
+
17
+ ### Changed
18
+ - Updated Ruby to v3.4.5 across all environments
19
+ - Updated development dependencies (bundler, overcommit, rubocop, rubocop-yard)
20
+ - Updated examples dependencies to use latest versions
21
+ - Improved documentation for Rails single-file applications
22
+
23
+ ### Fixed
24
+ - Standardized Ruby version usage across the project
25
+
10
26
  ## [0.2.0] - 2025-07-10
11
27
 
12
28
  ### Added
data/examples/README.md CHANGED
@@ -7,8 +7,9 @@ This directory contains example implementations of JSON-RPC servers using the js
7
7
  - [**rack-echo**](./rack-echo/) - Echo server using Rack with helpers
8
8
  - [**rack-single-file**](./rack-single-file/) - Minimal single-file example with bundler/inline
9
9
  - [**rack**](./rack/) - Calculator server using pure Rack
10
- - [**rails**](./rails-single-file/) - Calculator server using Rails
10
+ - [**rails**](./rails/) - Calculator server using Rails
11
11
  - [**rails-single-file**](./rails-single-file/) - Echo server using Rails with bundler/inline
12
+ - [**rails-single-file-routing**](./rails-single-file-routing/) - Echo server using Rails with method-specific routing
12
13
  - [**sinatra-classic**](./sinatra-classic/) - Calculator server using classic Sinatra
13
14
  - [**sinatra-modular**](./sinatra-modular/) - Calculator server using modular Sinatra
14
15
 
@@ -1,14 +1,14 @@
1
1
  PATH
2
2
  remote: ../..
3
3
  specs:
4
- jsonrpc-middleware (0.1.0)
4
+ jsonrpc-middleware (0.2.0)
5
5
  dry-validation (~> 1.11)
6
6
  zeitwerk (~> 2.7)
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
- bigdecimal (3.1.9)
11
+ bigdecimal (3.2.2)
12
12
  concurrent-ruby (1.3.5)
13
13
  dry-configurable (1.3.0)
14
14
  dry-core (~> 1.1)
@@ -32,7 +32,7 @@ GEM
32
32
  dry-logic (~> 1.5)
33
33
  dry-types (~> 1.8)
34
34
  zeitwerk (~> 2.6)
35
- dry-types (1.8.2)
35
+ dry-types (1.8.3)
36
36
  bigdecimal (~> 3.0)
37
37
  concurrent-ruby (~> 1.0)
38
38
  dry-core (~> 1.0)
@@ -49,10 +49,10 @@ GEM
49
49
  nio4r (2.7.4)
50
50
  puma (6.6.0)
51
51
  nio4r (~> 2.0)
52
- rack (3.1.14)
52
+ rack (3.1.16)
53
53
  rackup (2.2.1)
54
54
  rack (>= 3)
55
- zeitwerk (2.7.2)
55
+ zeitwerk (2.7.3)
56
56
 
57
57
  PLATFORMS
58
58
  arm64-darwin-24
@@ -65,4 +65,4 @@ DEPENDENCIES
65
65
  rackup
66
66
 
67
67
  BUNDLED WITH
68
- 2.6.8
68
+ 2.7.0
@@ -1,14 +1,14 @@
1
1
  PATH
2
2
  remote: ../..
3
3
  specs:
4
- jsonrpc-middleware (0.1.0)
4
+ jsonrpc-middleware (0.2.0)
5
5
  dry-validation (~> 1.11)
6
6
  zeitwerk (~> 2.7)
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
- bigdecimal (3.1.9)
11
+ bigdecimal (3.2.2)
12
12
  concurrent-ruby (1.3.5)
13
13
  dry-configurable (1.3.0)
14
14
  dry-core (~> 1.1)
@@ -32,7 +32,7 @@ GEM
32
32
  dry-logic (~> 1.5)
33
33
  dry-types (~> 1.8)
34
34
  zeitwerk (~> 2.6)
35
- dry-types (1.8.2)
35
+ dry-types (1.8.3)
36
36
  bigdecimal (~> 3.0)
37
37
  concurrent-ruby (~> 1.0)
38
38
  dry-core (~> 1.0)
@@ -49,10 +49,10 @@ GEM
49
49
  nio4r (2.7.4)
50
50
  puma (6.6.0)
51
51
  nio4r (~> 2.0)
52
- rack (3.1.14)
52
+ rack (3.1.16)
53
53
  rackup (2.2.1)
54
54
  rack (>= 3)
55
- zeitwerk (2.7.2)
55
+ zeitwerk (2.7.3)
56
56
 
57
57
  PLATFORMS
58
58
  arm64-darwin-24
@@ -65,4 +65,4 @@ DEPENDENCIES
65
65
  rackup
66
66
 
67
67
  BUNDLED WITH
68
- 2.6.8
68
+ 2.7.0
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../..
3
3
  specs:
4
- jsonrpc-middleware (0.1.0)
4
+ jsonrpc-middleware (0.2.0)
5
5
  dry-validation (~> 1.11)
6
6
  zeitwerk (~> 2.7)
7
7
 
@@ -126,13 +126,13 @@ GEM
126
126
  dry-initializer (~> 3.2)
127
127
  dry-schema (~> 1.14)
128
128
  zeitwerk (~> 2.6)
129
- erb (5.0.1)
129
+ erb (5.0.2)
130
130
  erubi (1.13.1)
131
131
  globalid (1.2.1)
132
132
  activesupport (>= 6.1)
133
133
  i18n (1.14.7)
134
134
  concurrent-ruby (~> 1.0)
135
- io-console (0.8.0)
135
+ io-console (0.8.1)
136
136
  irb (1.15.2)
137
137
  pp (>= 0.6.0)
138
138
  rdoc (>= 4.0.0)
@@ -258,4 +258,4 @@ DEPENDENCIES
258
258
  rails (~> 8.0.2)
259
259
 
260
260
  BUNDLED WITH
261
- 2.6.9
261
+ 2.7.0
@@ -0,0 +1,23 @@
1
+ # Rails Single File
2
+
3
+ An echo server using Rails with bundler/inline in a single file.
4
+
5
+ ## Running
6
+
7
+ ```sh
8
+ bundle exec rackup
9
+ ```
10
+
11
+ ## API
12
+
13
+ The server implements an echo API with this procedure:
14
+
15
+ - `echo` - Returns the input message
16
+
17
+ ## Example Requests
18
+
19
+ ```sh
20
+ curl -X POST http://localhost:9292 \
21
+ -H "Content-Type: application/json" \
22
+ -d '{"jsonrpc": "2.0", "method": "echo", "params": {"message": "Hello, World!"}, "id": 1}'
23
+ ```
@@ -0,0 +1,53 @@
1
+ # Rails Single File Routing
2
+
3
+ Demonstrates routing JSON-RPC methods to different Rails controller actions.
4
+
5
+ ## Highlights
6
+
7
+ Uses constraints to route JSON-RPC requests to different Rails controller actions:
8
+
9
+ ```ruby
10
+ class App < Rails::Application
11
+ # ...
12
+ routes.append do
13
+ post '/', to: 'jsonrpc#echo', constraints: JSONRPC::MethodConstraint.new('echo')
14
+ post '/', to: 'jsonrpc#ping', constraints: JSONRPC::MethodConstraint.new('ping')
15
+ end
16
+ end
17
+
18
+ class JsonrpcController < ActionController::Base
19
+ # POST /
20
+ def echoc
21
+ render jsonrpc: jsonrpc_request.params
22
+ end
23
+
24
+ # POST /
25
+ def ping
26
+ render jsonrpc: 'pong'
27
+ end
28
+ end
29
+ ```
30
+
31
+ ## Running
32
+
33
+ ```sh
34
+ bundle exec rackup
35
+ ```
36
+
37
+ ## API
38
+
39
+ The server implements an echo API with these procedures:
40
+
41
+ - `echo` - Returns the input message
42
+ - `ping` - Returns "pong"
43
+
44
+ ## Example Requests
45
+
46
+ ```sh
47
+ curl -X POST http://localhost:9292 \
48
+ -H "Content-Type: application/json" \
49
+ -d '{"jsonrpc": "2.0", "method": "echo", "params": {"message": "Hello, World!"}, "id": 1}'
50
+ curl -X POST http://localhost:9292 \
51
+ -H "Content-Type: application/json" \
52
+ -d '{"jsonrpc": "2.0""method": "ping", "params": {}, "id": 2}'
53
+ ```
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/inline'
4
+
5
+ gemfile(true) do
6
+ source 'https://rubygems.org'
7
+
8
+ gem 'rails', '~> 8.0.2'
9
+ gem 'puma', '~> 6.6.0'
10
+ gem 'jsonrpc-middleware', path: '../../', require: 'jsonrpc'
11
+ end
12
+
13
+ require 'rails'
14
+ require 'action_controller/railtie'
15
+
16
+ JSONRPC.configure do |config|
17
+ config.rescue_internal_errors = true # set to +false+ if you want to raise JSONRPC::InternalError manually
18
+
19
+ procedure(:echo) do
20
+ params do
21
+ required(:message).filled(:string)
22
+ end
23
+ end
24
+
25
+ procedure(:ping) do
26
+ params do
27
+ # no params
28
+ end
29
+ end
30
+ end
31
+
32
+ # Define the application
33
+ class App < Rails::Application
34
+ config.root = __dir__
35
+ config.cache_classes = true
36
+ config.eager_load = true
37
+ config.active_support.deprecation = :stderr
38
+ config.consider_all_requests_local = true
39
+ config.active_support.to_time_preserves_timezone = :zone
40
+ config.logger = nil
41
+ config.hosts.clear
42
+
43
+ routes.append do
44
+ post '/', to: 'jsonrpc#echo', constraints: JSONRPC::MethodConstraint.new('echo')
45
+ post '/', to: 'jsonrpc#ping', constraints: JSONRPC::MethodConstraint.new('ping')
46
+ end
47
+ end
48
+
49
+ # Define the JSONRPC controller
50
+ class JsonrpcController < ActionController::Base
51
+ def echo
52
+ render jsonrpc: jsonrpc_request.params
53
+ end
54
+
55
+ def ping
56
+ render jsonrpc: 'pong'
57
+ end
58
+ end
59
+
60
+ App.initialize!
61
+
62
+ run App
@@ -1,14 +1,14 @@
1
1
  PATH
2
2
  remote: ../..
3
3
  specs:
4
- jsonrpc-middleware (0.1.0)
4
+ jsonrpc-middleware (0.2.0)
5
5
  dry-validation (~> 1.11)
6
6
  zeitwerk (~> 2.7)
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
- base64 (0.2.0)
11
+ base64 (0.3.0)
12
12
  bigdecimal (3.2.2)
13
13
  concurrent-ruby (1.3.5)
14
14
  dry-configurable (1.3.0)
@@ -47,18 +47,18 @@ GEM
47
47
  dry-schema (~> 1.14)
48
48
  zeitwerk (~> 2.6)
49
49
  logger (1.7.0)
50
- multi_json (1.15.0)
50
+ multi_json (1.16.0)
51
51
  mustermann (3.0.3)
52
52
  ruby2_keywords (~> 0.0.1)
53
53
  nio4r (2.7.4)
54
54
  puma (6.6.0)
55
55
  nio4r (~> 2.0)
56
- rack (3.1.13)
56
+ rack (3.1.16)
57
57
  rack-protection (4.1.1)
58
58
  base64 (>= 0.1.0)
59
59
  logger (>= 1.6.0)
60
60
  rack (>= 3.0.0, < 4)
61
- rack-session (2.1.0)
61
+ rack-session (2.1.1)
62
62
  base64 (>= 0.1.0)
63
63
  rack (>= 3.0.0)
64
64
  rackup (2.2.1)
@@ -77,8 +77,8 @@ GEM
77
77
  rack-protection (= 4.1.1)
78
78
  sinatra (= 4.1.1)
79
79
  tilt (~> 2.0)
80
- tilt (2.6.0)
81
- zeitwerk (2.7.2)
80
+ tilt (2.6.1)
81
+ zeitwerk (2.7.3)
82
82
 
83
83
  PLATFORMS
84
84
  arm64-darwin-24
@@ -92,4 +92,4 @@ DEPENDENCIES
92
92
  sinatra-contrib
93
93
 
94
94
  BUNDLED WITH
95
- 2.6.8
95
+ 2.7.0
@@ -26,8 +26,6 @@ post '/' do
26
26
  end
27
27
  end
28
28
 
29
- private
30
-
31
29
  def handle_single(request_or_notification)
32
30
  params = request_or_notification.params
33
31
 
@@ -1,14 +1,14 @@
1
1
  PATH
2
2
  remote: ../..
3
3
  specs:
4
- jsonrpc-middleware (0.1.0)
4
+ jsonrpc-middleware (0.2.0)
5
5
  dry-validation (~> 1.11)
6
6
  zeitwerk (~> 2.7)
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
- base64 (0.2.0)
11
+ base64 (0.3.0)
12
12
  bigdecimal (3.2.2)
13
13
  concurrent-ruby (1.3.5)
14
14
  dry-configurable (1.3.0)
@@ -47,18 +47,18 @@ GEM
47
47
  dry-schema (~> 1.14)
48
48
  zeitwerk (~> 2.6)
49
49
  logger (1.7.0)
50
- multi_json (1.15.0)
50
+ multi_json (1.16.0)
51
51
  mustermann (3.0.3)
52
52
  ruby2_keywords (~> 0.0.1)
53
53
  nio4r (2.7.4)
54
54
  puma (6.6.0)
55
55
  nio4r (~> 2.0)
56
- rack (3.1.13)
56
+ rack (3.1.16)
57
57
  rack-protection (4.1.1)
58
58
  base64 (>= 0.1.0)
59
59
  logger (>= 1.6.0)
60
60
  rack (>= 3.0.0, < 4)
61
- rack-session (2.1.0)
61
+ rack-session (2.1.1)
62
62
  base64 (>= 0.1.0)
63
63
  rack (>= 3.0.0)
64
64
  rackup (2.2.1)
@@ -77,8 +77,8 @@ GEM
77
77
  rack-protection (= 4.1.1)
78
78
  sinatra (= 4.1.1)
79
79
  tilt (~> 2.0)
80
- tilt (2.6.0)
81
- zeitwerk (2.7.2)
80
+ tilt (2.6.1)
81
+ zeitwerk (2.7.3)
82
82
 
83
83
  PLATFORMS
84
84
  arm64-darwin-24
@@ -92,4 +92,4 @@ DEPENDENCIES
92
92
  sinatra-contrib
93
93
 
94
94
  BUNDLED WITH
95
- 2.6.8
95
+ 2.7.0
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JSONRPC
4
+ # This constraint allows Rails routes to be matched based on the JSON-RPC
5
+ # method name in the request, enabling method-specific routing.
6
+ #
7
+ # @example Using in Rails routes
8
+ # post '/', to: 'jsonrpc#echo', constraints: JSONRPC::Railtie::MethodConstraint.new('echo')
9
+ # post '/', to: 'jsonrpc#ping', constraints: JSONRPC::Railtie::MethodConstraint.new('ping')
10
+ #
11
+ # @api private
12
+ #
13
+ class MethodConstraint
14
+ # Initialize a new method constraint
15
+ #
16
+ # @param jsonrpc_method_name [String] The JSON-RPC method name to match against
17
+ #
18
+ def initialize(jsonrpc_method_name)
19
+ @jsonrpc_method_name = jsonrpc_method_name
20
+ end
21
+
22
+ # Check if the request matches the configured method name
23
+ #
24
+ # @param request [ActionDispatch::Request] The Rails request object
25
+ # @return [Boolean] true if the JSON-RPC method matches, false otherwise
26
+ #
27
+ def matches?(request)
28
+ jsonrpc_request = request.env['jsonrpc.request']
29
+
30
+ return false unless jsonrpc_request
31
+
32
+ jsonrpc_request.method == @jsonrpc_method_name
33
+ end
34
+ end
35
+ end
@@ -3,6 +3,7 @@
3
3
  module JSONRPC
4
4
  # @api private
5
5
  class Railtie < ::Rails::Railtie
6
+ # Rails routing constraint for matching JSON-RPC method names
6
7
  initializer 'jsonrpc.middleware' do |app|
7
8
  app.middleware.use JSONRPC::Middleware
8
9
  end
@@ -11,5 +11,5 @@ module JSONRPC
11
11
  #
12
12
  # @return [String] The current version number
13
13
  #
14
- VERSION = '0.2.0'
14
+ VERSION = '0.3.0'
15
15
  end
data/lib/jsonrpc.rb CHANGED
@@ -68,10 +68,13 @@ loader = Zeitwerk::Loader.for_gem
68
68
  loader.log! if ENV['DEBUG_ZEITWERK'] == 'true'
69
69
  loader.enable_reloading
70
70
  loader.collapse("#{__dir__}/jsonrpc/errors")
71
- loader.ignore("#{__dir__}/jsonrpc/railtie.rb")
71
+ loader.collapse("#{__dir__}/jsonrpc/railtie")
72
+
73
+ unless defined?(Rails)
74
+ loader.ignore("#{__dir__}/jsonrpc/railtie.rb")
75
+ loader.ignore("#{__dir__}/jsonrpc/railtie/method_constraint.rb")
76
+ end
77
+
72
78
  loader.inflector.inflect('jsonrpc' => 'JSONRPC')
73
79
  loader.setup
74
80
  loader.eager_load
75
-
76
- # Only load Rails integration if Rails is available
77
- require_relative 'jsonrpc/railtie' if defined?(Rails)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonrpc-middleware
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wilson Silva
@@ -79,9 +79,11 @@ files:
79
79
  - examples/rack/README.md
80
80
  - examples/rack/app.rb
81
81
  - examples/rack/config.ru
82
+ - examples/rails-single-file-routing/README.md
83
+ - examples/rails-single-file-routing/config.ru
84
+ - examples/rails-single-file/README.md
82
85
  - examples/rails-single-file/config.ru
83
86
  - examples/rails/.gitignore
84
- - examples/rails/.ruby-version
85
87
  - examples/rails/Gemfile
86
88
  - examples/rails/Gemfile.lock
87
89
  - examples/rails/README.md
@@ -133,6 +135,7 @@ files:
133
135
  - lib/jsonrpc/notification.rb
134
136
  - lib/jsonrpc/parser.rb
135
137
  - lib/jsonrpc/railtie.rb
138
+ - lib/jsonrpc/railtie/method_constraint.rb
136
139
  - lib/jsonrpc/request.rb
137
140
  - lib/jsonrpc/response.rb
138
141
  - lib/jsonrpc/validator.rb
@@ -162,7 +165,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
162
165
  - !ruby/object:Gem::Version
163
166
  version: '0'
164
167
  requirements: []
165
- rubygems_version: 3.6.9
168
+ rubygems_version: 3.7.0
166
169
  specification_version: 4
167
170
  summary: Implementation of the JSON-RPC protocol.
168
171
  test_files: []
@@ -1 +0,0 @@
1
- ruby-3.4.4