Class: JSONRPC::Response
- Inherits:
-
Dry::Struct
- Object
- Dry::Struct
- JSONRPC::Response
- Defined in:
- lib/jsonrpc/response.rb,
sig/jsonrpc/response.rbs
Overview
A JSON-RPC 2.0 Response object
Represents the outcome of a method invocation, either containing a result for successful calls or an error for failed ones. This follows the JSON-RPC 2.0 specification.
When a rpc call is made, the Server must reply with a Response, except for notifications. A Response object can contain either a result (for success) or an error (for failure), but never both.
Instance Attribute Summary collapse
-
#error ⇒ JSONRPC::Error?
readonly
The error object (for failure).
-
#id ⇒ String, ...
readonly
The request identifier.
-
#jsonrpc ⇒ String
readonly
JSON-RPC protocol version.
-
#result ⇒ Object?
readonly
The result of the method invocation (for success).
Instance Method Summary collapse
-
#error? ⇒ Boolean
Checks if the response is an error.
-
#initialize ⇒ Response
constructor
A new instance of Response.
-
#success? ⇒ Boolean
Checks if the response is successful.
-
#to_h ⇒ Hash
(also: #to_response)
Converts the response to a JSON-compatible Hash.
-
#to_json ⇒ String
Converts the response to a JSON string.
- #validate_id ⇒ void
- #validate_result_and_error ⇒ void
Constructor Details
#initialize ⇒ Response
Returns a new instance of Response.
8 |
# File 'sig/jsonrpc/response.rbs', line 8 def initialize: (?result: untyped, ?error: Error?, id: id_type) -> void |
Instance Attribute Details
#error ⇒ JSONRPC::Error? (readonly)
The error object (for failure)
56 |
# File 'lib/jsonrpc/response.rb', line 56 attribute? :error, Types.Instance(JSONRPC::Error).optional |
#id ⇒ String, ... (readonly)
The request identifier
67 |
# File 'lib/jsonrpc/response.rb', line 67 attribute? :id, Types::String | Types::Integer | Types::Nil |
#jsonrpc ⇒ String (readonly)
JSON-RPC protocol version
34 |
# File 'lib/jsonrpc/response.rb', line 34 attribute :jsonrpc, Types::String.default('2.0') |
#result ⇒ Object? (readonly)
The result of the method invocation (for success)
45 |
# File 'lib/jsonrpc/response.rb', line 45 attribute? :result, Types::Any |
Instance Method Details
#error? ⇒ Boolean
Checks if the response is an error
111 112 113 |
# File 'lib/jsonrpc/response.rb', line 111 def error? !error.nil? end |
#success? ⇒ Boolean
Checks if the response is successful
98 99 100 |
# File 'lib/jsonrpc/response.rb', line 98 def success? !result.nil? end |
#to_h ⇒ Hash Also known as: to_response
Converts the response to a JSON-compatible Hash
124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/jsonrpc/response.rb', line 124 def to_h hash = { jsonrpc: jsonrpc, id: id } if success? hash[:result] = result else hash[:error] = error.to_h end hash end |
#to_json ⇒ String
Converts the response to a JSON string
148 149 150 |
# File 'lib/jsonrpc/response.rb', line 148 def to_json(*) MultiJson.dump(to_h, *) end |
#validate_id ⇒ void
This method returns an undefined value.
17 |
# File 'sig/jsonrpc/response.rbs', line 17 def validate_id: (id_type) -> void |
#validate_result_and_error ⇒ void
This method returns an undefined value.
16 |
# File 'sig/jsonrpc/response.rbs', line 16 def validate_result_and_error: (untyped, Error?) -> void |