Class: JSONRPC::Configuration

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

Overview

Configuration class for JSON-RPC procedure management and validation.

This class provides functionality to register, retrieve, and validate JSON-RPC procedures. It acts as a central registry for method definitions and their parameter constraints.

Examples:

Registering a procedure

JSONRPC::Configuration.instance.procedure('sum') do
  params do
    required(:numbers).value(:array, min_size?: 1)
  end
end

Constant Summary collapse

Procedure =

Represents a registered JSON-RPC procedure with its validation contract and configuration

Data.define(:allow_positional_arguments, :contract, :parameter_name)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(log_internal_errors: true, log_request_validation_errors: false, rescue_internal_errors: true, render_internal_errors: false, validate_procedure_signatures: true) ⇒ Configuration

Initializes a new Configuration instance

Examples:

config = JSONRPC::Configuration.new

With custom options

config = JSONRPC::Configuration.new(
  log_request_validation_errors: true,
  render_internal_errors: true
)

Parameters:

  • log_internal_errors (Boolean) (defaults to: true)

    whether to log detailed internal error information in the terminal

  • log_request_validation_errors (Boolean) (defaults to: false)

    whether to log validation errors during JSON-RPC request processing

  • rescue_internal_errors (Boolean) (defaults to: true)

    whether internal errors should be rescued and converted to JSON-RPC errors

  • render_internal_errors (Boolean) (defaults to: false)

    whether to render detailed internal error information in responses

  • validate_procedure_signatures (Boolean) (defaults to: true)

    whether procedure signatures are validated



138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/jsonrpc/configuration.rb', line 138

def initialize(
  log_internal_errors: true,
  log_request_validation_errors: false,
  rescue_internal_errors: true,
  render_internal_errors: false,
  validate_procedure_signatures: true
)
  @procedures = {}
  @log_internal_errors = log_internal_errors
  @log_request_validation_errors = log_request_validation_errors
  @rescue_internal_errors = rescue_internal_errors
  @render_internal_errors = render_internal_errors
  @validate_procedure_signatures = validate_procedure_signatures
end

Instance Attribute Details

#log_internal_errorsBoolean

Whether to log detailed internal error information in the terminal

Examples:

config.log_internal_errors # => true

Returns:

  • (Boolean)

    whether to log internal error details



58
59
60
# File 'lib/jsonrpc/configuration.rb', line 58

def log_internal_errors
  @log_internal_errors
end

#log_request_validation_errorsBoolean

Whether to log validation errors during JSON-RPC request processing

Examples:

config.log_request_validation_errors # => false

Returns:

  • (Boolean)

    whether to log JSON-RPC request validation errors



69
70
71
# File 'lib/jsonrpc/configuration.rb', line 69

def log_request_validation_errors
  @log_request_validation_errors
end

#render_internal_errorsBoolean

Whether to render detailed internal error information in responses

Examples:

config.render_internal_errors # => true

Returns:

  • (Boolean)

    whether to log internal error details



80
81
82
# File 'lib/jsonrpc/configuration.rb', line 80

def render_internal_errors
  @render_internal_errors
end

#rescue_internal_errorsBoolean

Whether internal errors should be rescued and converted to JSON-RPC errors

Examples:

config.rescue_internal_errors # => true

Returns:

  • (Boolean)

    whether internal errors are rescued



91
92
93
# File 'lib/jsonrpc/configuration.rb', line 91

def rescue_internal_errors
  @rescue_internal_errors
end

#validate_procedure_signaturesBoolean (readonly)

Whether procedure signatures are validated

Examples:

config.validate_procedure_signatures # => true

Returns:

  • (Boolean)

    whether procedure signatures are validated



102
103
104
# File 'lib/jsonrpc/configuration.rb', line 102

def validate_procedure_signatures
  @validate_procedure_signatures
end

Class Method Details

.instanceConfiguration

Returns the singleton instance of the Configuration class

Examples:

config = JSONRPC::Configuration.instance

Returns:



162
163
164
# File 'lib/jsonrpc/configuration.rb', line 162

def self.instance
  @instance ||= new
end

Instance Method Details

#allow_positional_argumentsBoolean

Indicates if the procedure accepts positional arguments

Examples:

procedure.allow_positional_arguments # => true

Returns:

  • (Boolean)

    whether the procedure accepts positional arguments



47
# File 'lib/jsonrpc/configuration.rb', line 47

Procedure = Data.define(:allow_positional_arguments, :contract, :parameter_name)

#contractDry::Validation::Contract

The validation contract for procedure parameters

Examples:

procedure.contract # => #<Dry::Validation::Contract...>

Returns:

  • (Dry::Validation::Contract)

    the validation contract for procedure parameters



47
# File 'lib/jsonrpc/configuration.rb', line 47

Procedure = Data.define(:allow_positional_arguments, :contract, :parameter_name)

#get_procedure(method_name) ⇒ Procedure?

Retrieves a procedure by its method name

Examples:

procedure = config.get_procedure('add')

Parameters:

  • method_name (String, Symbol)

    the name of the procedure to retrieve

Returns:

  • (Procedure, nil)

    the procedure if found, nil otherwise



217
218
219
# File 'lib/jsonrpc/configuration.rb', line 217

def get_procedure(method_name)
  @procedures[method_name.to_s]
end

#json_adapter=(adapter) ⇒ Symbol?

JSON adapter to use (optional)

Examples:

config.json_adapter = :oj

Returns:

  • (Symbol, nil)

    the JSON adapter to use



113
114
115
# File 'lib/jsonrpc/configuration.rb', line 113

def json_adapter=(adapter)
  MultiJson.use(adapter)
end

#parameter_nameSymbol?

The name of the first parameter in the contract schema

Examples:

procedure.parameter_name # => :numbers

Returns:

  • (Symbol, nil)

    the name of the first parameter in the contract schema



47
# File 'lib/jsonrpc/configuration.rb', line 47

Procedure = Data.define(:allow_positional_arguments, :contract, :parameter_name)

#procedure(method_name, allow_positional_arguments: false) {|optional| ... } ⇒ Procedure

Registers a new procedure with the given method name and validation contract

Examples:

Register a simple procedure

config.procedure('add') do
  params do
    required(:a).value(:integer)
    required(:b).value(:integer)
  end
end

Register a procedure without validation

config.procedure('ping')

Parameters:

  • method_name (String, Symbol)

    the name of the procedure

  • allow_positional_arguments (Boolean) (defaults to: false)

    whether the procedure accepts positional arguments

Yields:

  • (optional)

    A block that defines the validation contract using Dry::Validation DSL

Returns:



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/jsonrpc/configuration.rb', line 188

def procedure(method_name, allow_positional_arguments: false, &block)
  contract_class = if block
                     Class.new(Dry::Validation::Contract, &block)
                   else
                     Class.new(Dry::Validation::Contract) do
                       params {} # rubocop:disable Lint/EmptyBlock
                     end
                   end
  contract_class.class_eval { import_predicates_as_macros }
  contract = contract_class.new

  @procedures[method_name.to_s] = Procedure.new(
    allow_positional_arguments:,
    contract:,
    parameter_name: contract.schema.key_map.keys.first&.name
  )
end

#procedure?(method_name) ⇒ Boolean

Checks if a procedure with the given method name exists

Examples:

config.procedure?('add') # => true

Parameters:

  • method_name (String, Symbol)

    the name of the procedure to check

Returns:

  • (Boolean)

    true if the procedure exists, false otherwise



232
233
234
# File 'lib/jsonrpc/configuration.rb', line 232

def procedure?(method_name)
  @procedures.key?(method_name.to_s)
end

#reset!void

This method returns an undefined value.

Clears all registered procedures

Examples:

config.reset!


245
246
247
# File 'lib/jsonrpc/configuration.rb', line 245

def reset!
  @procedures.clear
end