Class: Riffer::Tools::Params
- Inherits:
-
Object
- Object
- Riffer::Tools::Params
- Defined in:
- lib/riffer/tools/params.rb
Overview
Riffer::Tools::Params provides a DSL for defining tool parameters.
Used within a Tool’s params block to define required and optional parameters.
params do
required :city, String, description: "The city name"
optional :units, String, default: "celsius", enum: ["celsius", "fahrenheit"]
end
Instance Attribute Summary collapse
-
#parameters ⇒ Object
readonly
: Array.
Instance Method Summary collapse
-
#initialize ⇒ Params
constructor
: () -> void.
-
#optional(name, type, description: nil, enum: nil, default: nil) ⇒ Object
Defines an optional parameter.
-
#required(name, type, description: nil, enum: nil) ⇒ Object
Defines a required parameter.
-
#to_json_schema ⇒ Object
Converts all parameters to JSON Schema format.
-
#validate(arguments) ⇒ Object
Validates arguments against parameter definitions.
Constructor Details
#initialize ⇒ Params
: () -> void
17 18 19 |
# File 'lib/riffer/tools/params.rb', line 17 def initialize @parameters = [] end |
Instance Attribute Details
#parameters ⇒ Object (readonly)
: Array
14 15 16 |
# File 'lib/riffer/tools/params.rb', line 14 def parameters @parameters end |
Instance Method Details
#optional(name, type, description: nil, enum: nil, default: nil) ⇒ Object
Defines an optional parameter.
: (Symbol, Class, ?description: String?, ?enum: Array?, ?default: untyped) -> void
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/riffer/tools/params.rb', line 37 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) ⇒ Object
Defines a required parameter.
: (Symbol, Class, ?description: String?, ?enum: Array?) -> void
24 25 26 27 28 29 30 31 32 |
# File 'lib/riffer/tools/params.rb', line 24 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 ⇒ Object
Converts all parameters to JSON Schema format.
: () -> Hash[Symbol, untyped]
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/riffer/tools/params.rb', line 91 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) ⇒ Object
Validates arguments against parameter definitions.
Raises Riffer::ValidationError if validation fails.
: (Hash[Symbol, untyped]) -> Hash[Symbol, untyped]
53 54 55 56 57 58 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 |
# File 'lib/riffer/tools/params.rb', line 53 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 |