Class: A2A::Protocol::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/a2a/protocol/json_rpc.rb

Overview

Represents a JSON-RPC 2.0 request

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(jsonrpc:, method:, params: {}, id: nil) ⇒ Request

Initialize a new request

Parameters:

  • The JSON-RPC version (should be "2.0")

  • The method name

  • (defaults to: {})

    The method parameters

  • (defaults to: nil)

    The request ID (nil for notifications)



213
214
215
216
217
218
# File 'lib/a2a/protocol/json_rpc.rb', line 213

def initialize(jsonrpc:, method:, params: {}, id: nil)
  @jsonrpc = jsonrpc
  @method = method
  @params = params
  @id = id
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



204
205
206
# File 'lib/a2a/protocol/json_rpc.rb', line 204

def id
  @id
end

#jsonrpcObject (readonly)

Returns the value of attribute jsonrpc.



204
205
206
# File 'lib/a2a/protocol/json_rpc.rb', line 204

def jsonrpc
  @jsonrpc
end

#methodObject (readonly)

Returns the value of attribute method.



204
205
206
# File 'lib/a2a/protocol/json_rpc.rb', line 204

def method
  @method
end

#paramsObject (readonly)

Returns the value of attribute params.



204
205
206
# File 'lib/a2a/protocol/json_rpc.rb', line 204

def params
  @params
end

Instance Method Details

#notification?Boolean

Check if this is a notification (no response expected)

Returns:

  • True if this is a notification



224
225
226
# File 'lib/a2a/protocol/json_rpc.rb', line 224

def notification?
  @id.nil?
end

#to_hHash

Convert to hash representation

Returns:

  • The request as a hash



232
233
234
235
236
237
238
239
240
241
242
# File 'lib/a2a/protocol/json_rpc.rb', line 232

def to_h
  hash = {
    jsonrpc: @jsonrpc,
    method: @method
  }

  hash[:params] = @params unless @params.nil? || (@params.respond_to?(:empty?) && @params.empty?)
  hash[:id] = @id unless @id.nil?

  hash
end

#to_json(*_args) ⇒ String

Convert to JSON string

Returns:

  • The request as JSON



248
249
250
251
# File 'lib/a2a/protocol/json_rpc.rb', line 248

def to_json(*_args)
  # Use optimized JSON generator if available
  JsonRpc.generate_json(to_h)
end