Class: JSON_RPC::Request

Inherits:
Data
  • Object
show all
Defined in:
lib/json_rpc/request.rb

Overview

Represents a JSON-RPC request object.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Initializes a new Request.

Parameters:

  • id (String, Numeric, nil)

    The request identifier. Should be String or Numeric according to spec for requests needing a response. Can be nil.

  • method (String)

    The method name.

  • params (Hash, Array, nil) (defaults to: nil)

    The parameters (optional). Structured value.

Raises:



12
13
14
15
16
17
# File 'lib/json_rpc/request.rb', line 12

def initialize(id:, method:, params: nil)
  # Basic validation for ID type (String, Numeric, or null allowed by spec)
  validate_id_type(id)
  # Basic validation for method (e.g., non-empty string) could be added.
  super
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id

Returns:

  • (Object)

    the current value of id



5
6
7
# File 'lib/json_rpc/request.rb', line 5

def id
  @id
end

#methodObject (readonly)

Returns the value of attribute method

Returns:

  • (Object)

    the current value of method



5
6
7
# File 'lib/json_rpc/request.rb', line 5

def method
  @method
end

#paramsObject (readonly)

Returns the value of attribute params

Returns:

  • (Object)

    the current value of params



5
6
7
# File 'lib/json_rpc/request.rb', line 5

def params
  @params
end

Class Method Details

.from_h(h) ⇒ Object



19
20
21
# File 'lib/json_rpc/request.rb', line 19

def self.from_h(h)
  new(id: h["id"], method: h["method"], params: h["params"])
end

Instance Method Details

#as_jsonObject



37
# File 'lib/json_rpc/request.rb', line 37

def as_json(*) = to_h

#to_hHash

Returns a hash representation of the request, ready for JSON serialization.

Returns:

  • (Hash)

    The hash representation.



26
27
28
29
30
31
32
33
34
35
# File 'lib/json_rpc/request.rb', line 26

def to_h
  hash = {
    "jsonrpc" => "2.0",
    id: id, # Include id even if null, spec allows null id
    method: method
  }
  # Include params only if it's not nil
  hash[:params] = params unless params.nil?
  hash
end