Class: JSONRPC::Response
- Inherits:
-
Dry::Struct
- Object
- Dry::Struct
- JSONRPC::Response
- Defined in:
- lib/jsonrpc/response.rb
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 Method Summary collapse
-
#error ⇒ JSONRPC::Error?
The error object (for failure).
-
#error? ⇒ Boolean
Checks if the response is an error.
-
#id ⇒ String, ...
The request identifier.
-
#jsonrpc ⇒ String
JSON-RPC protocol version.
-
#result ⇒ Object?
The result of the method invocation (for success).
-
#success? ⇒ Boolean
Checks if the response is successful.
-
#to_h ⇒ Hash
Converts the response to a JSON-compatible Hash.
-
#to_json ⇒ String
Converts the response to a JSON string.
Instance Method Details
#error ⇒ JSONRPC::Error?
The error object (for failure)
56 |
# File 'lib/jsonrpc/response.rb', line 56 attribute? :error, Types.Instance(JSONRPC::Error).optional |
#error? ⇒ Boolean
Checks if the response is an error
113 114 115 |
# File 'lib/jsonrpc/response.rb', line 113 def error? !error.nil? end |
#id ⇒ String, ...
The request identifier
67 |
# File 'lib/jsonrpc/response.rb', line 67 attribute? :id, Types::String | Types::Integer | Types::Nil |
#jsonrpc ⇒ String
JSON-RPC protocol version
34 |
# File 'lib/jsonrpc/response.rb', line 34 attribute :jsonrpc, Types::String.default('2.0') |
#result ⇒ Object?
The result of the method invocation (for success)
45 |
# File 'lib/jsonrpc/response.rb', line 45 attribute? :result, Types::Any |
#success? ⇒ Boolean
Checks if the response is successful
100 101 102 |
# File 'lib/jsonrpc/response.rb', line 100 def success? !result.nil? end |
#to_h ⇒ Hash
Converts the response to a JSON-compatible Hash
126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/jsonrpc/response.rb', line 126 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
150 151 152 |
# File 'lib/jsonrpc/response.rb', line 150 def to_json(*) MultiJson.dump(to_h, *) end |