Class: AgentClientProtocol::Codec
- Inherits:
-
Object
- Object
- AgentClientProtocol::Codec
- Defined in:
- lib/agent_client_protocol/codec.rb
Instance Attribute Summary collapse
-
#side ⇒ Object
readonly
Returns the value of attribute side.
-
#unstable ⇒ Object
readonly
Returns the value of attribute unstable.
Instance Method Summary collapse
- #decode_response_for(method:, message:) ⇒ Object
- #decode_rpc(message) ⇒ Object
- #encode_error(id:, error:) ⇒ Object
- #encode_notification(method:, params:) ⇒ Object
- #encode_request(method:, id:, params:) ⇒ Object
- #encode_result(id:, method:, result:) ⇒ Object
-
#initialize(side:, unstable: false, validate_schema: true) ⇒ Codec
constructor
A new instance of Codec.
Constructor Details
#initialize(side:, unstable: false, validate_schema: true) ⇒ Codec
Returns a new instance of Codec.
7 8 9 10 11 12 |
# File 'lib/agent_client_protocol/codec.rb', line 7 def initialize(side:, unstable: false, validate_schema: true) @decoder = Decoder.new(side: side, unstable: unstable, validate_schema: validate_schema) @protocol_decoder = Decoder.new(side: :protocol, unstable: unstable, validate_schema: validate_schema) @side = @decoder.side @unstable = @decoder.unstable end |
Instance Attribute Details
#side ⇒ Object (readonly)
Returns the value of attribute side.
5 6 7 |
# File 'lib/agent_client_protocol/codec.rb', line 5 def side @side end |
#unstable ⇒ Object (readonly)
Returns the value of attribute unstable.
5 6 7 |
# File 'lib/agent_client_protocol/codec.rb', line 5 def unstable @unstable end |
Instance Method Details
#decode_response_for(method:, message:) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/agent_client_protocol/codec.rb', line 31 def decode_response_for(method:, message:) response = parse_response() return response if response.failure? decoded = @decoder.decode_response(method: method, result: response.result) RPC::Response.new(id: response.id, result: decoded.typed_payload || decoded.payload) end |
#decode_rpc(message) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/agent_client_protocol/codec.rb', line 14 def decode_rpc() parsed = () case parsed when RPC::Request decoded = @decoder.decode_request(method: parsed.method, params: parsed.params) decoded.id = parsed.id decoded when RPC::Notification decode_notification(parsed) when RPC::Response parsed else raise Error.invalid_request("unsupported rpc message") end end |
#encode_error(id:, error:) ⇒ Object
60 61 62 |
# File 'lib/agent_client_protocol/codec.rb', line 60 def encode_error(id:, error:) RPC::Response.new(id: id, error: normalize_error(error)).to_h end |
#encode_notification(method:, params:) ⇒ Object
46 47 48 49 50 51 |
# File 'lib/agent_client_protocol/codec.rb', line 46 def encode_notification(method:, params:) payload = serialize_payload(params) decoded = notification_decoder_for(method).decode_notification(method: method, params: payload) encoded_params = decoded.typed_payload.nil? ? payload : decoded.typed_payload.to_h RPC::Notification.new(method: method, params: encoded_params).to_h end |
#encode_request(method:, id:, params:) ⇒ Object
39 40 41 42 43 44 |
# File 'lib/agent_client_protocol/codec.rb', line 39 def encode_request(method:, id:, params:) payload = serialize_payload(params) decoded = @decoder.decode_request(method: method, params: payload) encoded_params = decoded.typed_payload.nil? ? payload : decoded.typed_payload.to_h RPC::Request.new(id: id, method: method, params: encoded_params).to_h end |
#encode_result(id:, method:, result:) ⇒ Object
53 54 55 56 57 58 |
# File 'lib/agent_client_protocol/codec.rb', line 53 def encode_result(id:, method:, result:) payload = serialize_payload(result) decoded = @decoder.decode_response(method: method, result: payload) encoded_result = decoded.typed_payload.nil? ? payload : decoded.typed_payload.to_h RPC::Response.new(id: id, result: encoded_result).to_h end |