Class: A2A::Streaming::Response
- Inherits:
-
Object
- Object
- A2A::Streaming::Response
- Defined in:
- lib/a2a/streaming/response.rb
Constant Summary collapse
- PAYLOAD_TYPES =
{ task: Task, message: Message, status_update: StatusUpdateEvent, artifact_update: ArtifactUpdateEvent }.freeze
- BUILDERS =
{ "task" => ->(v) { new(:task, Task.from_h(v)) }, "message" => ->(v) { new(:message, Message.from_h(v)) }, "statusUpdate" => ->(v) { new(:status_update, StatusUpdateEvent.from_h(v)) }, "artifactUpdate" => ->(v) { new(:artifact_update, ArtifactUpdateEvent.from_h(v)) } }.freeze
- WIRE_KEYS =
{ task: "task", message: "message", status_update: "statusUpdate", artifact_update: "artifactUpdate" }.freeze
Instance Attribute Summary collapse
-
#payload ⇒ Object
readonly
Returns the value of attribute payload.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
- #artifact_update? ⇒ Boolean
-
#initialize(type, payload) ⇒ Response
constructor
A new instance of Response.
- #message? ⇒ Boolean
- #status_update? ⇒ Boolean
- #task? ⇒ Boolean
- #to_h ⇒ Object
Constructor Details
#initialize(type, payload) ⇒ Response
Returns a new instance of Response.
22 23 24 25 26 27 28 |
# File 'lib/a2a/streaming/response.rb', line 22 def initialize(type, payload) expected = PAYLOAD_TYPES.fetch(type) { raise ArgumentError, "unknown type: #{type.inspect}" } raise TypeError, "payload must be a #{expected}" unless payload.is_a?(expected) @type = type @payload = payload end |
Instance Attribute Details
#payload ⇒ Object (readonly)
Returns the value of attribute payload.
20 21 22 |
# File 'lib/a2a/streaming/response.rb', line 20 def payload @payload end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
20 21 22 |
# File 'lib/a2a/streaming/response.rb', line 20 def type @type end |
Class Method Details
.from_h(hash) ⇒ Object
30 31 32 33 34 35 |
# File 'lib/a2a/streaming/response.rb', line 30 def self.from_h(hash) key, builder = BUILDERS.find { |k, _| hash.key?(k) } raise ArgumentError, "unrecognised StreamResponse keys: #{hash.keys.inspect}" unless key builder.call(hash[key]) end |
Instance Method Details
#artifact_update? ⇒ Boolean
60 61 62 |
# File 'lib/a2a/streaming/response.rb', line 60 def artifact_update? type == :artifact_update end |
#message? ⇒ Boolean
52 53 54 |
# File 'lib/a2a/streaming/response.rb', line 52 def type == :message end |
#status_update? ⇒ Boolean
56 57 58 |
# File 'lib/a2a/streaming/response.rb', line 56 def status_update? type == :status_update end |
#task? ⇒ Boolean
48 49 50 |
# File 'lib/a2a/streaming/response.rb', line 48 def task? type == :task end |
#to_h ⇒ Object
44 45 46 |
# File 'lib/a2a/streaming/response.rb', line 44 def to_h { WIRE_KEYS.fetch(type) => payload.to_h } end |