Class: Riffer::Tools::Params Private
- Inherits:
-
Object
- Object
- Riffer::Tools::Params
- Defined in:
- lib/riffer/tools/params.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Riffer::Tools::Params provides a DSL for defining tool parameters.
Used within a Tool’s ‘params` block to define required and optional parameters.
Instance Attribute Summary collapse
- #parameters ⇒ Object readonly private
Instance Method Summary collapse
-
#initialize ⇒ Params
constructor
private
A new instance of Params.
-
#optional(name, type, description: nil, enum: nil, default: nil) ⇒ void
private
Defines an optional parameter.
-
#required(name, type, description: nil, enum: nil) ⇒ void
private
Defines a required parameter.
-
#to_json_schema ⇒ Hash
private
Converts all parameters to JSON Schema format.
-
#validate(arguments) ⇒ Hash
private
Validates arguments against parameter definitions.
Constructor Details
#initialize ⇒ Params
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Params.
17 18 19 |
# File 'lib/riffer/tools/params.rb', line 17 def initialize @parameters = [] end |
Instance Attribute Details
#parameters ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
15 16 17 |
# File 'lib/riffer/tools/params.rb', line 15 def parameters @parameters end |
Instance Method Details
#optional(name, type, description: nil, enum: nil, default: nil) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Defines an optional parameter
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/riffer/tools/params.rb', line 44 def optional(name, type, description: nil, enum: nil, default: nil) @parameters << Riffer::Tools::Param.new( name: name, type: type, required: false, description: description, enum: enum, default: default ) end |
#required(name, type, description: nil, enum: nil) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Defines a required parameter
27 28 29 30 31 32 33 34 35 |
# File 'lib/riffer/tools/params.rb', line 27 def required(name, type, description: nil, enum: nil) @parameters << Riffer::Tools::Param.new( name: name, type: type, required: true, description: description, enum: enum ) end |
#to_json_schema ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Converts all parameters to JSON Schema format
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/riffer/tools/params.rb', line 96 def to_json_schema properties = {} required_params = [] @parameters.each do |param| properties[param.name.to_s] = param.to_json_schema required_params << param.name.to_s if param.required end { type: "object", properties: properties, required: required_params, additionalProperties: false } end |
#validate(arguments) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Validates arguments against parameter definitions
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/riffer/tools/params.rb', line 59 def validate(arguments) validated = {} errors = [] @parameters.each do |param| value = arguments[param.name] if value.nil? && param.required errors << "#{param.name} is required" next end if value.nil? validated[param.name] = param.default next end unless param.valid_type?(value) errors << "#{param.name} must be a #{param.type_name}" next end if param.enum && !param.enum.include?(value) errors << "#{param.name} must be one of: #{param.enum.join(", ")}" next end validated[param.name] = value end raise Riffer::ValidationError, errors.join("; ") if errors.any? validated end |