Skip to content

Commit c2bb7b4

Browse files
committed
Update copied code to match repo style
1 parent 7a43e1c commit c2bb7b4

File tree

2 files changed

+34
-36
lines changed

2 files changed

+34
-36
lines changed

lib/json_rpc_handler.rb

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# frozen_string_literal: true
22

3-
require "json_rpc_handler/version"
43
require "json"
54

65
module JsonRpcHandler
@@ -10,21 +9,21 @@ class Version
109
end
1110

1211
class ErrorCode
13-
InvalidRequest = -32600
14-
MethodNotFound = -32601
15-
InvalidParams = -32602
16-
InternalError = -32603
17-
ParseError = -32700
12+
INVALID_REQUEST = -32600
13+
METHOD_NOT_FOUND = -32601
14+
INVALID_PARAMS = -32602
15+
INTERNAL_ERROR = -32603
16+
PARSE_ERROR = -32700
1817
end
1918

2019
DEFAULT_ALLOWED_ID_CHARACTERS = /\A[a-zA-Z0-9_-]+\z/
2120

22-
module_function
21+
extend self
2322

2423
def handle(request, id_validation_pattern: DEFAULT_ALLOWED_ID_CHARACTERS, &method_finder)
2524
if request.is_a?(Array)
2625
return error_response(id: :unknown_id, id_validation_pattern:, error: {
27-
code: ErrorCode::InvalidRequest,
26+
code: ErrorCode::INVALID_REQUEST,
2827
message: "Invalid Request",
2928
data: "Request is an empty array",
3029
}) if request.empty?
@@ -42,7 +41,7 @@ def handle(request, id_validation_pattern: DEFAULT_ALLOWED_ID_CHARACTERS, &metho
4241
process_request(request, id_validation_pattern:, &method_finder)
4342
else
4443
error_response(id: :unknown_id, id_validation_pattern:, error: {
45-
code: ErrorCode::InvalidRequest,
44+
code: ErrorCode::INVALID_REQUEST,
4645
message: "Invalid Request",
4746
data: "Request must be an array or a hash",
4847
})
@@ -55,13 +54,13 @@ def handle_json(request_json, id_validation_pattern: DEFAULT_ALLOWED_ID_CHARACTE
5554
response = handle(request, id_validation_pattern:, &method_finder)
5655
rescue JSON::ParserError
5756
response = error_response(id: :unknown_id, id_validation_pattern:, error: {
58-
code: ErrorCode::ParseError,
57+
code: ErrorCode::PARSE_ERROR,
5958
message: "Parse error",
6059
data: "Invalid JSON",
6160
})
6261
end
6362

64-
response.to_json if response
63+
response&.to_json
6564
end
6665

6766
def process_request(request, id_validation_pattern:, &method_finder)
@@ -76,7 +75,7 @@ def process_request(request, id_validation_pattern:, &method_finder)
7675
end
7776

7877
return error_response(id: :unknown_id, id_validation_pattern:, error: {
79-
code: ErrorCode::InvalidRequest,
78+
code: ErrorCode::INVALID_REQUEST,
8079
message: "Invalid Request",
8180
data: error,
8281
}) if error
@@ -86,7 +85,7 @@ def process_request(request, id_validation_pattern:, &method_finder)
8685

8786
unless valid_params?(params)
8887
return error_response(id:, id_validation_pattern:, error: {
89-
code: ErrorCode::InvalidParams,
88+
code: ErrorCode::INVALID_PARAMS,
9089
message: "Invalid params",
9190
data: "Method parameters must be an array or an object or null",
9291
})
@@ -97,7 +96,7 @@ def process_request(request, id_validation_pattern:, &method_finder)
9796

9897
if method.nil?
9998
return error_response(id:, id_validation_pattern:, error: {
100-
code: ErrorCode::MethodNotFound,
99+
code: ErrorCode::METHOD_NOT_FOUND,
101100
message: "Method not found",
102101
data: method_name,
103102
})
@@ -108,7 +107,7 @@ def process_request(request, id_validation_pattern:, &method_finder)
108107
success_response(id:, result:)
109108
rescue StandardError => e
110109
error_response(id:, id_validation_pattern:, error: {
111-
code: ErrorCode::InternalError,
110+
code: ErrorCode::INTERNAL_ERROR,
112111
message: "Internal error",
113112
data: e.message,
114113
})

test/json_rpc_handler_test.rb

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

3-
require 'test_helper'
3+
require "test_helper"
44

55
describe JsonRpcHandler do
66
before do
@@ -9,7 +9,7 @@
99
@response_json = nil
1010
end
1111

12-
describe '#handle' do
12+
describe "#handle" do
1313
# Comments verbatim from https://www.jsonrpc.org/specification
1414
#
1515
# JSON-RPC 2.0 Specification
@@ -41,7 +41,7 @@
4141
assert_rpc_error expected_error: {
4242
code: -32600,
4343
message: "Invalid Request",
44-
data: "JSON-RPC version must be 2.0"
44+
data: "JSON-RPC version must be 2.0",
4545
}
4646
end
4747

@@ -229,7 +229,7 @@
229229
# method's expected parameters.
230230

231231
it "with array params returns a result" do
232-
register("sum") { |params| params.sum }
232+
register("sum", &:sum)
233233

234234
handle jsonrpc: "2.0", id: 1, method: "sum", params: [1, 2, 3]
235235

@@ -346,7 +346,7 @@
346346
assert_rpc_error expected_error: {
347347
code: -32700,
348348
message: "Parse error",
349-
data: "Invalid JSON"
349+
data: "Invalid JSON",
350350
}
351351
end
352352

@@ -360,14 +360,13 @@
360360
}
361361
end
362362

363-
364363
it "returns an error with the code set to -32601 when the method does not exist" do
365364
handle jsonrpc: "2.0", id: 1, method: "add", params: { a: 1, b: 2 }
366365

367366
assert_rpc_error expected_error: {
368367
code: -32601,
369368
message: "Method not found",
370-
data: "add"
369+
data: "add",
371370
}
372371
end
373372

@@ -395,7 +394,7 @@
395394
assert_rpc_error expected_error: {
396395
code: -32603,
397396
message: "Internal error",
398-
data: "Something bad happened"
397+
data: "Something bad happened",
399398
}
400399
end
401400

@@ -438,7 +437,7 @@
438437
]
439438

440439
assert @response.is_a?(Array)
441-
assert @response.all? { |result| result[:jsonrpc] == "2.0"}
440+
assert @response.all? { |result| result[:jsonrpc] == "2.0" }
442441
assert_equal [100, 200], @response.map { |result| result[:id] }
443442
assert_equal [3, 12], @response.map { |result| result[:result] }
444443
assert @response.all? { |result| result[:error].nil? }
@@ -455,7 +454,7 @@
455454
]
456455

457456
assert @response.is_a?(Array)
458-
assert @response.all? { |result| result[:jsonrpc] == "2.0"}
457+
assert @response.all? { |result| result[:jsonrpc] == "2.0" }
459458
assert_equal [100, 200], @response.map { |result| result[:id] }
460459
assert_equal [3, 5], @response.map { |result| result[:result] }
461460
assert @response.all? { |result| result[:error].nil? }
@@ -531,7 +530,7 @@
531530

532531
@response = JsonRpcHandler.handle(
533532
{ jsonrpc: "2.0", id: "[email protected]", method: "add", params: { a: 1, b: 2 } },
534-
id_validation_pattern: custom_pattern
533+
id_validation_pattern: custom_pattern,
535534
) { |method_name| @registry[method_name] }
536535

537536
assert_rpc_success expected_result: 3
@@ -543,7 +542,7 @@
543542

544543
@response = JsonRpcHandler.handle(
545544
{ jsonrpc: "2.0", id: "id<script>", method: "add", params: { a: 1, b: 2 } },
546-
id_validation_pattern: custom_pattern
545+
id_validation_pattern: custom_pattern,
547546
) { |method_name| @registry[method_name] }
548547

549548
assert_rpc_error expected_error: {
@@ -559,7 +558,7 @@
559558

560559
@response_json = JsonRpcHandler.handle_json(
561560
{ jsonrpc: "2.0", id: "[email protected]", method: "add", params: { a: 1, b: 2 } }.to_json,
562-
id_validation_pattern: custom_pattern
561+
id_validation_pattern: custom_pattern,
563562
) { |method_name| @registry[method_name] }
564563
@response = JSON.parse(@response_json, symbolize_names: true)
565564

@@ -577,7 +576,7 @@
577576
{ jsonrpc: "2.0", id: "req@1", method: "add", params: { a: 1, b: 2 } },
578577
{ jsonrpc: "2.0", id: "req@2", method: "mul", params: { a: 3, b: 4 } },
579578
],
580-
id_validation_pattern: custom_pattern
579+
id_validation_pattern: custom_pattern,
581580
) { |method_name| @registry[method_name] }
582581

583582
assert @response.is_a?(Array)
@@ -592,7 +591,7 @@
592591

593592
@response = JsonRpcHandler.handle(
594593
{ jsonrpc: "2.0", id: "[email protected]", method: "add", params: { a: 1, b: 2 } },
595-
id_validation_pattern: custom_pattern
594+
id_validation_pattern: custom_pattern,
596595
) { |method_name| @registry[method_name] }
597596

598597
assert_rpc_success expected_result: 3
@@ -604,7 +603,7 @@
604603

605604
@response = JsonRpcHandler.handle(
606605
{ jsonrpc: "2.0", id: "<script>alert('xss')</script>", method: "add", params: { a: 1, b: 2 } },
607-
id_validation_pattern: nil
606+
id_validation_pattern: nil,
608607
) { |method_name| @registry[method_name] }
609608

610609
assert_rpc_success expected_result: 3
@@ -613,7 +612,7 @@
613612
end
614613
end
615614

616-
describe '#handle_json' do
615+
describe "#handle_json" do
617616
it "returns a Response object when the request is valid and not a notification" do
618617
register("add") { |params| params[:a] + params[:b] }
619618

@@ -653,12 +652,12 @@ def handle_json(request_json)
653652
end
654653

655654
def assert_rpc_success(expected_result:)
656-
assert_equal expected_result, @response[:result]
657-
assert_nil @response[:error]
655+
assert_equal(expected_result, @response[:result])
656+
assert_nil(@response[:error])
658657
end
659658

660659
def assert_rpc_error(expected_error:)
661-
assert_equal expected_error, @response[:error]
662-
assert_nil @response[:result]
660+
assert_equal(expected_error, @response[:error])
661+
assert_nil(@response[:result])
663662
end
664663
end

0 commit comments

Comments
 (0)