Class: Riffer::Tools::Response
- Inherits:
-
Object
- Object
- Riffer::Tools::Response
- Defined in:
- lib/riffer/tools/response.rb
Overview
Direct Known Subclasses
Constant Summary collapse
- VALID_FORMATS =
%i[text json].freeze
Instance Attribute Summary collapse
-
#content ⇒ Object
readonly
The response content.
-
#error_message ⇒ Object
readonly
The error message, or
nilon success. -
#error_type ⇒ Object
readonly
The error type, or
nilon success.
Class Method Summary collapse
-
.error(message, type: :execution_error) ⇒ Object
Creates an error response.
-
.json(result) ⇒ Object
Creates a success response with JSON format.
-
.success(result, format: :text) ⇒ Object
Creates a success response.
-
.text(result) ⇒ Object
Creates a success response with text format.
Instance Method Summary collapse
-
#error? ⇒ Boolean
Returns true if the tool execution failed.
-
#success? ⇒ Boolean
Returns true if the tool execution succeeded.
-
#to_h ⇒ Object
Returns a hash representation of the response.
Instance Attribute Details
#content ⇒ Object (readonly)
The response content.
23 24 25 |
# File 'lib/riffer/tools/response.rb', line 23 def content @content end |
#error_message ⇒ Object (readonly)
The error message, or nil on success.
26 27 28 |
# File 'lib/riffer/tools/response.rb', line 26 def @error_message end |
#error_type ⇒ Object (readonly)
The error type, or nil on success.
29 30 31 |
# File 'lib/riffer/tools/response.rb', line 29 def error_type @error_type end |
Class Method Details
.error(message, type: :execution_error) ⇒ Object
Creates an error response.
– : (String, ?type: Symbol) -> Riffer::Tools::Response
66 67 68 |
# File 'lib/riffer/tools/response.rb', line 66 def self.error(, type: :execution_error) new(content: , success: false, error_message: , error_type: type) end |
.json(result) ⇒ Object
Creates a success response with JSON format.
– : (untyped) -> Riffer::Tools::Response
58 59 60 |
# File 'lib/riffer/tools/response.rb', line 58 def self.json(result) success(result, format: :json) end |
.success(result, format: :text) ⇒ Object
Creates a success response.
Raises Riffer::ArgumentError if format is invalid.
– : (untyped, ?format: Symbol) -> Riffer::Tools::Response
37 38 39 40 41 42 43 44 |
# File 'lib/riffer/tools/response.rb', line 37 def self.success(result, format: :text) unless VALID_FORMATS.include?(format) raise Riffer::ArgumentError, "Invalid format: #{format}. Must be one of: #{VALID_FORMATS.join(", ")}" end content = (format == :json) ? result.to_json : result.to_s new(content: content, success: true) end |
.text(result) ⇒ Object
Creates a success response with text format.
– : (untyped) -> Riffer::Tools::Response
50 51 52 |
# File 'lib/riffer/tools/response.rb', line 50 def self.text(result) success(result, format: :text) end |
Instance Method Details
#error? ⇒ Boolean
Returns true if the tool execution failed. – : () -> bool
78 |
# File 'lib/riffer/tools/response.rb', line 78 def error? = !@success |
#success? ⇒ Boolean
Returns true if the tool execution succeeded. – : () -> bool
73 |
# File 'lib/riffer/tools/response.rb', line 73 def success? = @success |
#to_h ⇒ Object
Returns a hash representation of the response.
– : () -> Hash[Symbol, untyped]
84 85 86 |
# File 'lib/riffer/tools/response.rb', line 84 def to_h {content: @content, error: @error_message, error_type: @error_type} end |