Class: RubyClaude::Response
- Inherits:
-
Data
- Object
- Data
- RubyClaude::Response
- Defined in:
- lib/ruby_claude/response.rb
Overview
Immutable value object describing the final result of a query.
Built from the CLI’s –output-format json result object (or from the final result line of a stream). Missing keys map to sensible defaults rather than raising.
Instance Attribute Summary collapse
-
#cost_usd ⇒ Object
readonly
Returns the value of attribute cost_usd.
-
#duration_ms ⇒ Object
readonly
Returns the value of attribute duration_ms.
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#num_turns ⇒ Object
readonly
Returns the value of attribute num_turns.
-
#raw ⇒ Object
readonly
Returns the value of attribute raw.
-
#session_id ⇒ Object
readonly
Returns the value of attribute session_id.
-
#text ⇒ Object
readonly
Returns the value of attribute text.
-
#usage ⇒ Object
readonly
Returns the value of attribute usage.
Class Method Summary collapse
-
.from_result(data) ⇒ Response
Build a Response from a parsed CLI result hash.
Instance Method Summary collapse
-
#error? ⇒ Boolean
Whether the result represents an error.
-
#success? ⇒ Boolean
Whether the result was successful.
-
#to_s ⇒ String
Returns the assistant text, so puts response prints the answer.
Instance Attribute Details
#cost_usd ⇒ Object (readonly)
Returns the value of attribute cost_usd
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/ruby_claude/response.rb', line 26 Response = Data.define(:text, :session_id, :cost_usd, :usage, :num_turns, :duration_ms, :error, :raw) do # Build a Response from a parsed CLI result hash. # # @param data [Hash, nil] the parsed result object # @return [Response] def self.from_result(data) data ||= {} new( text: data["result"] || "", session_id: data["session_id"], cost_usd: (data["total_cost_usd"] || data["cost_usd"] || 0.0).to_f, usage: data["usage"] || {}, num_turns: (data["num_turns"] || 0).to_i, duration_ms: (data["duration_ms"] || 0).to_i, error: data.fetch("is_error", false) ? true : false, raw: data ) end # @return [Boolean] whether the result represents an error def error? = !!error # @return [Boolean] whether the result was successful def success? = !error? # Returns the assistant text, so +puts response+ prints the answer. # # @return [String] def to_s = text end |
#duration_ms ⇒ Object (readonly)
Returns the value of attribute duration_ms
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/ruby_claude/response.rb', line 26 Response = Data.define(:text, :session_id, :cost_usd, :usage, :num_turns, :duration_ms, :error, :raw) do # Build a Response from a parsed CLI result hash. # # @param data [Hash, nil] the parsed result object # @return [Response] def self.from_result(data) data ||= {} new( text: data["result"] || "", session_id: data["session_id"], cost_usd: (data["total_cost_usd"] || data["cost_usd"] || 0.0).to_f, usage: data["usage"] || {}, num_turns: (data["num_turns"] || 0).to_i, duration_ms: (data["duration_ms"] || 0).to_i, error: data.fetch("is_error", false) ? true : false, raw: data ) end # @return [Boolean] whether the result represents an error def error? = !!error # @return [Boolean] whether the result was successful def success? = !error? # Returns the assistant text, so +puts response+ prints the answer. # # @return [String] def to_s = text end |
#error ⇒ Object (readonly)
Returns the value of attribute error
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/ruby_claude/response.rb', line 26 Response = Data.define(:text, :session_id, :cost_usd, :usage, :num_turns, :duration_ms, :error, :raw) do # Build a Response from a parsed CLI result hash. # # @param data [Hash, nil] the parsed result object # @return [Response] def self.from_result(data) data ||= {} new( text: data["result"] || "", session_id: data["session_id"], cost_usd: (data["total_cost_usd"] || data["cost_usd"] || 0.0).to_f, usage: data["usage"] || {}, num_turns: (data["num_turns"] || 0).to_i, duration_ms: (data["duration_ms"] || 0).to_i, error: data.fetch("is_error", false) ? true : false, raw: data ) end # @return [Boolean] whether the result represents an error def error? = !!error # @return [Boolean] whether the result was successful def success? = !error? # Returns the assistant text, so +puts response+ prints the answer. # # @return [String] def to_s = text end |
#num_turns ⇒ Object (readonly)
Returns the value of attribute num_turns
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/ruby_claude/response.rb', line 26 Response = Data.define(:text, :session_id, :cost_usd, :usage, :num_turns, :duration_ms, :error, :raw) do # Build a Response from a parsed CLI result hash. # # @param data [Hash, nil] the parsed result object # @return [Response] def self.from_result(data) data ||= {} new( text: data["result"] || "", session_id: data["session_id"], cost_usd: (data["total_cost_usd"] || data["cost_usd"] || 0.0).to_f, usage: data["usage"] || {}, num_turns: (data["num_turns"] || 0).to_i, duration_ms: (data["duration_ms"] || 0).to_i, error: data.fetch("is_error", false) ? true : false, raw: data ) end # @return [Boolean] whether the result represents an error def error? = !!error # @return [Boolean] whether the result was successful def success? = !error? # Returns the assistant text, so +puts response+ prints the answer. # # @return [String] def to_s = text end |
#raw ⇒ Object (readonly)
Returns the value of attribute raw
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/ruby_claude/response.rb', line 26 Response = Data.define(:text, :session_id, :cost_usd, :usage, :num_turns, :duration_ms, :error, :raw) do # Build a Response from a parsed CLI result hash. # # @param data [Hash, nil] the parsed result object # @return [Response] def self.from_result(data) data ||= {} new( text: data["result"] || "", session_id: data["session_id"], cost_usd: (data["total_cost_usd"] || data["cost_usd"] || 0.0).to_f, usage: data["usage"] || {}, num_turns: (data["num_turns"] || 0).to_i, duration_ms: (data["duration_ms"] || 0).to_i, error: data.fetch("is_error", false) ? true : false, raw: data ) end # @return [Boolean] whether the result represents an error def error? = !!error # @return [Boolean] whether the result was successful def success? = !error? # Returns the assistant text, so +puts response+ prints the answer. # # @return [String] def to_s = text end |
#session_id ⇒ Object (readonly)
Returns the value of attribute session_id
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/ruby_claude/response.rb', line 26 Response = Data.define(:text, :session_id, :cost_usd, :usage, :num_turns, :duration_ms, :error, :raw) do # Build a Response from a parsed CLI result hash. # # @param data [Hash, nil] the parsed result object # @return [Response] def self.from_result(data) data ||= {} new( text: data["result"] || "", session_id: data["session_id"], cost_usd: (data["total_cost_usd"] || data["cost_usd"] || 0.0).to_f, usage: data["usage"] || {}, num_turns: (data["num_turns"] || 0).to_i, duration_ms: (data["duration_ms"] || 0).to_i, error: data.fetch("is_error", false) ? true : false, raw: data ) end # @return [Boolean] whether the result represents an error def error? = !!error # @return [Boolean] whether the result was successful def success? = !error? # Returns the assistant text, so +puts response+ prints the answer. # # @return [String] def to_s = text end |
#text ⇒ Object (readonly)
Returns the value of attribute text
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/ruby_claude/response.rb', line 26 Response = Data.define(:text, :session_id, :cost_usd, :usage, :num_turns, :duration_ms, :error, :raw) do # Build a Response from a parsed CLI result hash. # # @param data [Hash, nil] the parsed result object # @return [Response] def self.from_result(data) data ||= {} new( text: data["result"] || "", session_id: data["session_id"], cost_usd: (data["total_cost_usd"] || data["cost_usd"] || 0.0).to_f, usage: data["usage"] || {}, num_turns: (data["num_turns"] || 0).to_i, duration_ms: (data["duration_ms"] || 0).to_i, error: data.fetch("is_error", false) ? true : false, raw: data ) end # @return [Boolean] whether the result represents an error def error? = !!error # @return [Boolean] whether the result was successful def success? = !error? # Returns the assistant text, so +puts response+ prints the answer. # # @return [String] def to_s = text end |
#usage ⇒ Object (readonly)
Returns the value of attribute usage
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/ruby_claude/response.rb', line 26 Response = Data.define(:text, :session_id, :cost_usd, :usage, :num_turns, :duration_ms, :error, :raw) do # Build a Response from a parsed CLI result hash. # # @param data [Hash, nil] the parsed result object # @return [Response] def self.from_result(data) data ||= {} new( text: data["result"] || "", session_id: data["session_id"], cost_usd: (data["total_cost_usd"] || data["cost_usd"] || 0.0).to_f, usage: data["usage"] || {}, num_turns: (data["num_turns"] || 0).to_i, duration_ms: (data["duration_ms"] || 0).to_i, error: data.fetch("is_error", false) ? true : false, raw: data ) end # @return [Boolean] whether the result represents an error def error? = !!error # @return [Boolean] whether the result was successful def success? = !error? # Returns the assistant text, so +puts response+ prints the answer. # # @return [String] def to_s = text end |
Class Method Details
.from_result(data) ⇒ Response
Build a Response from a parsed CLI result hash.
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/ruby_claude/response.rb', line 32 def self.from_result(data) data ||= {} new( text: data["result"] || "", session_id: data["session_id"], cost_usd: (data["total_cost_usd"] || data["cost_usd"] || 0.0).to_f, usage: data["usage"] || {}, num_turns: (data["num_turns"] || 0).to_i, duration_ms: (data["duration_ms"] || 0).to_i, error: data.fetch("is_error", false) ? true : false, raw: data ) end |
Instance Method Details
#error? ⇒ Boolean
Returns whether the result represents an error.
47 |
# File 'lib/ruby_claude/response.rb', line 47 def error? = !!error |
#success? ⇒ Boolean
Returns whether the result was successful.
50 |
# File 'lib/ruby_claude/response.rb', line 50 def success? = !error? |
#to_s ⇒ String
Returns the assistant text, so puts response prints the answer.
55 |
# File 'lib/ruby_claude/response.rb', line 55 def to_s = text |