Class: JSONRPC::Configuration
- Inherits:
-
Object
- Object
- JSONRPC::Configuration
- 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.
Defined Under Namespace
Classes: Procedure
Instance Attribute Summary collapse
-
#allow_positional_arguments ⇒ Boolean
readonly
Indicates if the procedure accepts positional arguments.
-
#contract ⇒ Dry::Validation::Contract
readonly
The validation contract for procedure parameters.
-
#log_internal_errors ⇒ Boolean
Whether to log detailed internal error information in the terminal.
-
#log_request_validation_errors ⇒ Boolean
Whether to log validation errors during JSON-RPC request processing.
-
#logger ⇒ Logger
The logger instance used for error and diagnostic output.
-
#parameter_name ⇒ Symbol?
readonly
The name of the first parameter in the contract schema.
-
#render_internal_errors ⇒ Boolean
Whether to render detailed internal error information in responses.
-
#rescue_internal_errors ⇒ Boolean
Whether internal errors should be rescued and converted to JSON-RPC errors.
-
#validate_procedure_signatures ⇒ Boolean
readonly
Whether procedure signatures are validated.
Class Method Summary collapse
-
.instance ⇒ Configuration
Returns the singleton instance of the Configuration class.
Instance Method Summary collapse
-
#get_procedure(method_name) ⇒ Procedure?
Retrieves a procedure by its method name.
-
#initialize(logger: Logger.new($stdout, progname: 'JSONRPC'), log_internal_errors: true, log_request_validation_errors: false, rescue_internal_errors: true, render_internal_errors: false, validate_procedure_signatures: true) ⇒ Configuration
constructor
Initializes a new Configuration instance.
-
#json_adapter=(adapter) ⇒ Symbol?
JSON adapter to use (optional).
-
#procedure(method_name, allow_positional_arguments: false, &block) { ... } ⇒ Procedure
Registers a new procedure with the given method name and validation contract.
-
#procedure?(method_name) ⇒ Boolean
Checks if a procedure with the given method name exists.
-
#reset! ⇒ void
Clears all registered procedures.
Constructor Details
#initialize(logger: Logger.new($stdout, progname: 'JSONRPC'), 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
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/jsonrpc/configuration.rb', line 161 def initialize( logger: Logger.new($stdout, progname: 'JSONRPC'), log_internal_errors: true, log_request_validation_errors: false, rescue_internal_errors: true, render_internal_errors: false, validate_procedure_signatures: true ) @procedures = {} @logger = logger @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
#allow_positional_arguments ⇒ Boolean (readonly)
Indicates if the procedure accepts positional arguments
53 |
# File 'lib/jsonrpc/configuration.rb', line 53 Procedure = Data.define(:allow_positional_arguments, :contract, :parameter_name) |
#contract ⇒ Dry::Validation::Contract (readonly)
The validation contract for procedure parameters
53 |
# File 'lib/jsonrpc/configuration.rb', line 53 Procedure = Data.define(:allow_positional_arguments, :contract, :parameter_name) |
#log_internal_errors ⇒ Boolean
Whether to log detailed internal error information in the terminal
78 79 80 |
# File 'lib/jsonrpc/configuration.rb', line 78 def log_internal_errors @log_internal_errors end |
#log_request_validation_errors ⇒ Boolean
Whether to log validation errors during JSON-RPC request processing
89 90 91 |
# File 'lib/jsonrpc/configuration.rb', line 89 def log_request_validation_errors @log_request_validation_errors end |
#logger ⇒ Logger
The logger instance used for error and diagnostic output
67 68 69 |
# File 'lib/jsonrpc/configuration.rb', line 67 def logger @logger end |
#parameter_name ⇒ Symbol? (readonly)
The name of the first parameter in the contract schema
53 |
# File 'lib/jsonrpc/configuration.rb', line 53 Procedure = Data.define(:allow_positional_arguments, :contract, :parameter_name) |
#render_internal_errors ⇒ Boolean
Whether to render detailed internal error information in responses
100 101 102 |
# File 'lib/jsonrpc/configuration.rb', line 100 def render_internal_errors @render_internal_errors end |
#rescue_internal_errors ⇒ Boolean
Whether internal errors should be rescued and converted to JSON-RPC errors
111 112 113 |
# File 'lib/jsonrpc/configuration.rb', line 111 def rescue_internal_errors @rescue_internal_errors end |
#validate_procedure_signatures ⇒ Boolean (readonly)
Whether procedure signatures are validated
122 123 124 |
# File 'lib/jsonrpc/configuration.rb', line 122 def validate_procedure_signatures @validate_procedure_signatures end |
Class Method Details
.instance ⇒ Configuration
Returns the singleton instance of the Configuration class
187 188 189 |
# File 'lib/jsonrpc/configuration.rb', line 187 def self.instance @instance ||= new end |
Instance Method Details
#get_procedure(method_name) ⇒ Procedure?
Retrieves a procedure by its method name
243 244 245 |
# File 'lib/jsonrpc/configuration.rb', line 243 def get_procedure(method_name) @procedures[method_name.to_s] end |
#json_adapter=(adapter) ⇒ Symbol?
JSON adapter to use (optional)
135 136 137 |
# File 'lib/jsonrpc/configuration.rb', line 135 def json_adapter=(adapter) MultiJson.use(adapter) end |
#procedure(method_name, allow_positional_arguments: false, &block) { ... } ⇒ Procedure
Registers a new procedure with the given method name and validation contract
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/jsonrpc/configuration.rb', line 214 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
258 259 260 |
# File 'lib/jsonrpc/configuration.rb', line 258 def procedure?(method_name) @procedures.key?(method_name.to_s) end |
#reset! ⇒ void
This method returns an undefined value.
Clears all registered procedures
271 272 273 |
# File 'lib/jsonrpc/configuration.rb', line 271 def reset! @procedures.clear end |