Class: JSONRPC::Notification

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

Overview

A JSON-RPC 2.0 Notification object

Represents a method call that does not expect a response. Unlike a Request, a Notification omits the “id” field, indicating that no response should be sent.

A Notification is a Request object without an “id” member. Notifications are not confirmable by definition since they do not have a Response object.

Examples:

Create a notification with parameters

notification = JSONRPC::Notification.new(method: "update", params: [1, 2, 3, 4, 5])

Create a notification without parameters

notification = JSONRPC::Notification.new(method: "heartbeat")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Creates a new JSON-RPC 2.0 Notification object

Examples:

Create a notification with array parameters

JSONRPC::Notification.new(method: "update", params: [1, 2, 3])

Create a notification with named parameters

JSONRPC::Notification.new(method: "log", params: { level: "info", message: "Hello" })

Parameters:

  • method (String)

    the name of the method to be invoked

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

    the parameters to be used during method invocation

Raises:

  • (ArgumentError)

    if method is not a String or is reserved

  • (ArgumentError)

    if params is not a Hash, Array, or nil



72
73
74
75
76
77
78
79
80
# File 'lib/jsonrpc/notification.rb', line 72

def initialize(method:, params: nil)
  @jsonrpc = '2.0'

  validate_method(method)
  validate_params(params)

  @method = method
  @params = params
end

Instance Attribute Details

#jsonrpcString (readonly)

JSON-RPC protocol version

Examples:

notification.jsonrpc # => "2.0"

Returns:

  • (String)


31
32
33
# File 'lib/jsonrpc/notification.rb', line 31

def jsonrpc
  @jsonrpc
end

#methodString (readonly)

The method name to invoke

Examples:

notification.method # => "update"

Returns:

  • (String)


42
43
44
# File 'lib/jsonrpc/notification.rb', line 42

def method
  @method
end

#paramsHash, ... (readonly)

Parameters to pass to the method

Examples:

notification.params # => [1, 2, 3, 4, 5]

Returns:

  • (Hash, Array, nil)


53
54
55
# File 'lib/jsonrpc/notification.rb', line 53

def params
  @params
end

Instance Method Details

#to_hHash

Converts the notification to a JSON-compatible Hash

Examples:

notification.to_h # => { jsonrpc: "2.0", method: "update", params: [1, 2, 3] }

Returns:

  • (Hash)

    the notification as a JSON-compatible Hash



91
92
93
94
95
96
97
98
99
# File 'lib/jsonrpc/notification.rb', line 91

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

  hash[:params] = params unless params.nil?
  hash
end

#to_jsonString

Converts the notification to a JSON string

Examples:

notification.to_json # => '{"jsonrpc":"2.0","method":"update","params":[1,2,3]}'

Returns:

  • (String)

    the notification as a JSON string



110
111
112
# File 'lib/jsonrpc/notification.rb', line 110

def to_json(*)
  MultiJson.dump(to_h, *)
end