Class: AgentClientProtocol::RPC::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/agent_client_protocol/rpc.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#errorObject (readonly)

Returns the value of attribute error.



103
104
105
# File 'lib/agent_client_protocol/rpc.rb', line 103

def error
  @error
end

#idObject (readonly)

Returns the value of attribute id.



103
104
105
# File 'lib/agent_client_protocol/rpc.rb', line 103

def id
  @id
end

#resultObject (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

Returns:

  • (Boolean)


122
123
124
# File 'lib/agent_client_protocol/rpc.rb', line 122

def failure?
  !success?
end

#success?Boolean

Returns:

  • (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