Module: AgentClientProtocol::RPC
- Defined in:
- lib/agent_client_protocol/rpc.rb
Defined Under Namespace
Modules: RequestId
Classes: Notification, Request, Response
Constant Summary
collapse
- JSONRPC_VERSION =
"2.0"
Class Method Summary
collapse
Class Method Details
.deep_stringify_keys(value) ⇒ Object
55
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/agent_client_protocol/rpc.rb', line 55
def deep_stringify_keys(value)
case value
when Hash
value.each_with_object({}) do |(k, v), result|
result[k.to_s] = deep_stringify_keys(v)
end
when Array
value.map { |item| deep_stringify_keys(item) }
else
value
end
end
|
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/agent_client_protocol/rpc.rb', line 44
def normalize_input(message)
case message
when String
JSON.parse(message)
when Hash
deep_stringify_keys(message)
else
raise ArgumentError, "message must be a Hash or JSON String"
end
end
|
.parse(message) ⇒ Object
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/agent_client_protocol/rpc.rb', line 11
def parse(message)
normalized = normalize_input(message)
if normalized.key?("jsonrpc") && normalized["jsonrpc"] != JSONRPC_VERSION
raise Error.invalid_request("unsupported jsonrpc version: #{normalized['jsonrpc']}")
end
if normalized.key?("method")
return Request.new(
id: normalized["id"],
method: normalized["method"],
params: normalized["params"]
) if normalized.key?("id")
return Notification.new(method: normalized["method"], params: normalized["params"])
end
if normalized.key?("result") || normalized.key?("error")
response_kwargs = { id: normalized["id"] }
response_kwargs[:result] = normalized["result"] if normalized.key?("result")
response_kwargs[:error] = normalized["error"] if normalized.key?("error")
return Response.new(**response_kwargs)
end
raise Error.invalid_request("message is neither request, response, nor notification")
end
|
.parse_json(json) ⇒ Object
38
39
40
41
42
|
# File 'lib/agent_client_protocol/rpc.rb', line 38
def parse_json(json)
parse(JSON.parse(json))
rescue JSON::ParserError => e
raise Error.parse_error(e.message)
end
|