Class: Riffer::Tools::Params Private

Inherits:
Object
  • Object
show all
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.

Examples:

params do
  required :city, String, description: "The city name"
  optional :units, String, default: "celsius", enum: ["celsius", "fahrenheit"]
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeParams

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

#parametersObject (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

Parameters:

  • name (Symbol)

    the parameter name

  • type (Class)

    the expected Ruby type

  • description (String, nil) (defaults to: nil)

    optional description

  • enum (Array, nil) (defaults to: nil)

    optional list of allowed values

  • default (Object, nil) (defaults to: nil)

    default value when not provided



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

Parameters:

  • name (Symbol)

    the parameter name

  • type (Class)

    the expected Ruby type

  • description (String, nil) (defaults to: nil)

    optional description

  • enum (Array, nil) (defaults to: nil)

    optional list of allowed values



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_schemaHash

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

Returns:

  • (Hash)

    the JSON Schema representation



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

Parameters:

  • arguments (Hash)

    the arguments to validate

Returns:

  • (Hash)

    validated arguments with defaults applied

Raises:



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