jsonrpc-rails 0.1.1 → 0.2.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: 441449ded5e2318b8781addfbef037112f6db49595a3cb4ae3cddb35826ee02f
4
- data.tar.gz: 0a4729694ca406fb564b9dbe5ba715f37d072f68a20a9f1e509121849050f18a
3
+ metadata.gz: 7c7eaabe38b79dd41bac2752a456d5278e6cc780c3d2ebbe0493c349ff24ee10
4
+ data.tar.gz: 51797ce9c50cd5e51b248f8a4376880f7ffcf19245daa8ae2ef1a4b9d5d17cdf
5
5
  SHA512:
6
- metadata.gz: dcc63ff788dc3952db0974ae8acde6c363d6d61ce358e645b736ea53c709000f47ae3af11ba4ba9ae344a5b03bc7d9382fef229e58bbfe92925dbf790ea134f9
7
- data.tar.gz: edbaaea3853e4f24bc63fe6c6261bf0386494b553f3559dafe722a9ab4ee02903b4004dbfc4874cdf310aa8a1e50ddc54d20db2ce0be8cbd050f835a030f8d1b
6
+ metadata.gz: 3a55b1b369ff4d277b3351c6632a858bd00244cd0799108fbeaf4e64ba2e5b39c4687be0f1855e96df90343541e8ac0ab137684cad4f7af7394a75612b367395
7
+ data.tar.gz: 9883f5c131ab3437cde5410f82a21f072824a045e1680ad27d81ef1f4d10d471149a7db639c18d8e2d194586979919224c4e921bae263236023d71934d2a6cc5
data/README.md CHANGED
@@ -10,7 +10,7 @@ It integrates into Rails, allowing you to render JSON-RPC responses and validate
10
10
  - **Rails Integration:** Easily integrate JSON-RPC 2.0 support via a Rails Railtie.
11
11
  - **Custom Renderer:** Render responses with `render jsonrpc:`, automatically wrapping data in the JSON-RPC 2.0 envelope.
12
12
  - **Error Handling:** Built-in support for both success and error responses according to the JSON-RPC 2.0 specification.
13
- - **Request Validation:** Includes middleware (`JSON_RPC_Rails::Middleware::Validator`) to strictly validate incoming JSON-RPC 2.0 requests (single and batch) against the specification structure.
13
+ - **Request Validation:** Includes middleware (`JSONRPC_Rails::Middleware::Validator`) to strictly validate incoming JSON-RPC 2.0 requests (single and batch) against the specification structure.
14
14
  - **Rails 8+ Compatibility:** Designed specifically for Rails 8 and later versions.
15
15
 
16
16
  ## Installation
@@ -81,7 +81,7 @@ You can override the default `message` or add `data` for either method by provid
81
81
 
82
82
  ### Handling Requests
83
83
 
84
- The gem automatically inserts `JSON_RPC_Rails::Middleware::Validator` into your application's middleware stack. This middleware performs the following actions for incoming **POST** requests with `Content-Type: application/json`:
84
+ The gem automatically inserts `JSONRPC_Rails::Middleware::Validator` into your application's middleware stack. This middleware performs the following actions for incoming **POST** requests with `Content-Type: application/json`:
85
85
 
86
86
  1. **Parses** the JSON body. Returns a JSON-RPC `Parse error (-32700)` if parsing fails.
87
87
  2. **Validates** the structure against the JSON-RPC 2.0 specification (single or batch). It performs strict validation, ensuring `jsonrpc: "2.0"`, a string `method`, optional `params` (array/object), optional `id` (string/number/null), and **no extraneous keys**. Returns a JSON-RPC `Invalid Request (-32600)` error if validation fails. **Note:** For batch requests, if *any* individual request within the batch is structurally invalid, the entire batch is rejected with a single `Invalid Request (-32600)` error.
data/lib/jsonrpc-rails.rb CHANGED
@@ -7,7 +7,5 @@ require_relative "json_rpc/request"
7
7
  require_relative "json_rpc/response"
8
8
  require_relative "json_rpc/notification"
9
9
 
10
- # Define the top-level module for the gem (optional, but good practice)
11
- module JSON_RPC_Rails
12
- # You might add gem-level configuration or methods here if needed later.
10
+ module JSONRPC_Rails
13
11
  end
@@ -1,9 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "json"
4
- require "active_support/json" # Add this require
4
+ require "active_support/json"
5
5
 
6
- module JSON_RPC_Rails
6
+ module JSONRPC_Rails
7
7
  module Middleware
8
8
  # Rack middleware to strictly validate incoming JSON-RPC 2.0 requests.
9
9
  # It checks for correct Content-Type, parses JSON, and validates the structure
@@ -18,23 +18,21 @@ module JSON_RPC_Rails
18
18
  # Other valid JSON payloads (e.g., strings, numbers, booleans, null) are passed through.
19
19
  class Validator
20
20
  CONTENT_TYPE = "application/json"
21
- ENV_PAYLOAD_KEY = "jsonrpc.payload"
21
+ ENV_PAYLOAD_KEY = :"jsonrpc.payload"
22
22
 
23
23
  def initialize(app)
24
24
  @app = app
25
25
  end
26
26
 
27
27
  def call(env)
28
- request = Rack::Request.new(env)
29
-
30
28
  # Only process POST requests with the correct Content-Type
31
- unless request.post? && request.content_type&.start_with?(CONTENT_TYPE)
32
- return @app.call(env)
33
- end
29
+ return @app.call(env) unless env["REQUEST_METHOD"] == "POST" &&
30
+ env["CONTENT_TYPE"]&.start_with?(CONTENT_TYPE)
34
31
 
35
32
  # Read and parse the request body
36
- body = request.body.read
37
- request.body.rewind # Rewind body for potential downstream middleware/apps
33
+ # Safely read and rewind
34
+ body = env["rack.input"].read
35
+ env["rack.input"].rewind
38
36
  payload = parse_json(body)
39
37
 
40
38
  # Handle JSON parsing errors
@@ -108,8 +106,8 @@ module JSON_RPC_Rails
108
106
  return false
109
107
  end
110
108
 
111
- # Optional 'id' must be a String, Number (Integer/Float), or Null if present
112
- if obj.key?("id") && ![ String, Integer, Float, NilClass ].include?(obj["id"].class)
109
+ # Optional 'id' must be a String, Number (Integer), or Null if present
110
+ if obj.key?("id") && ![ String, Integer, NilClass ].include?(obj["id"].class)
113
111
  return false
114
112
  end
115
113
 
@@ -1,12 +1,12 @@
1
1
  require_relative "middleware/validator"
2
2
 
3
- module JSON_RPC_Rails
3
+ module JSONRPC_Rails
4
4
  # Use Rails::Railtie to integrate with the Rails application
5
5
  class Railtie < Rails::Railtie
6
6
  # Insert the JSON-RPC Validator middleware early in the stack.
7
7
  # Inserting before Rack::Sendfile, which is typically present early in the stack.
8
8
  initializer "jsonrpc-rails.add_validator_middleware" do |app|
9
- app.middleware.use JSON_RPC_Rails::Middleware::Validator
9
+ app.middleware.use JSONRPC_Rails::Middleware::Validator
10
10
  end
11
11
 
12
12
  initializer "jsonrpc-rails.add_renderers" do
@@ -1,3 +1,3 @@
1
- module JSON_RPC_Rails
2
- VERSION = "0.1.1"
1
+ module JSONRPC_Rails
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonrpc-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Abdelkader Boudih
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-03-28 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: railties
@@ -65,7 +65,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
65
65
  - !ruby/object:Gem::Version
66
66
  version: '0'
67
67
  requirements: []
68
- rubygems_version: 3.6.5
68
+ rubygems_version: 3.6.7
69
69
  specification_version: 4
70
70
  summary: A Railtie-based gem that brings JSON-RPC 2.0 support to your Rails application.
71
71
  test_files: []