Class: AgentClientProtocol::RPC::Response
- Inherits:
-
Object
- Object
- AgentClientProtocol::RPC::Response
- Defined in:
- lib/agent_client_protocol/rpc.rb
Instance Attribute Summary collapse
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
Instance Method Summary collapse
- #failure? ⇒ Boolean
-
#initialize(id:, result: :__undefined__, error: :__undefined__) ⇒ Response
constructor
A new instance of Response.
- #success? ⇒ Boolean
- #to_h(include_jsonrpc: true) ⇒ Object
Constructor Details
#initialize(id:, result: :__undefined__, error: :__undefined__) ⇒ Response
Returns a new instance of Response.
105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/agent_client_protocol/rpc.rb', line 105 def initialize(id:, result: :__undefined__, error: :__undefined__) @id = RequestId.coerce(id) has_result = result != :__undefined__ has_error = error != :__undefined__ if has_result == has_error raise ArgumentError, "response must have exactly one of result or error" end @result = has_result ? result : nil @error = normalize_error(error) if has_error end |
Instance Attribute Details
#error ⇒ Object (readonly)
Returns the value of attribute error.
103 104 105 |
# File 'lib/agent_client_protocol/rpc.rb', line 103 def error @error end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
103 104 105 |
# File 'lib/agent_client_protocol/rpc.rb', line 103 def id @id end |
#result ⇒ Object (readonly)
Returns the value of attribute result.
103 104 105 |
# File 'lib/agent_client_protocol/rpc.rb', line 103 def result @result end |
Instance Method Details
#failure? ⇒ Boolean
122 123 124 |
# File 'lib/agent_client_protocol/rpc.rb', line 122 def failure? !success? end |
#success? ⇒ Boolean
118 119 120 |
# File 'lib/agent_client_protocol/rpc.rb', line 118 def success? error.nil? end |
#to_h(include_jsonrpc: true) ⇒ Object
126 127 128 129 130 131 132 133 134 135 |
# File 'lib/agent_client_protocol/rpc.rb', line 126 def to_h(include_jsonrpc: true) payload = { "id" => id } if success? payload["result"] = result else payload["error"] = error.is_a?(Error) ? error.to_h : error end include_jsonrpc ? { "jsonrpc" => JSONRPC_VERSION }.merge(payload) : payload end |