Class: JSON_RPC::Notification

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

Overview

Represents a JSON-RPC notification.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method:, params: nil) ⇒ Notification

Initializes a new Notification.

Parameters:

  • method (String)

    The method name.

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

    The parameters (optional). Structured value.



10
11
12
13
# File 'lib/json_rpc/notification.rb', line 10

def initialize(method:, params: nil)
  # Basic validation could be added here if needed, e.g., method is a non-empty string.
  super
end

Instance Attribute Details

#methodObject (readonly)

Returns the value of attribute method

Returns:

  • (Object)

    the current value of method



5
6
7
# File 'lib/json_rpc/notification.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/notification.rb', line 5

def params
  @params
end

Class Method Details

.from_h(h) ⇒ Object



15
16
17
# File 'lib/json_rpc/notification.rb', line 15

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

Instance Method Details

#as_jsonObject



32
# File 'lib/json_rpc/notification.rb', line 32

def as_json(*) = to_h

#to_hHash

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

Returns:

  • (Hash)

    The hash representation.



22
23
24
25
26
27
28
29
30
# File 'lib/json_rpc/notification.rb', line 22

def to_h
  hash = {
    "jsonrpc" => "2.0",
    method: method
  }
  # Include params only if it's not nil, as per JSON-RPC spec
  hash[:params] = params unless params.nil?
  hash
end