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 ⇒ Float
readonly
Total cost in USD (often
0.0on a subscription). -
#duration_ms ⇒ Integer
readonly
Wall-clock duration in milliseconds.
-
#error ⇒ Boolean
readonly
Whether the CLI reported an error.
-
#num_turns ⇒ Integer
readonly
Number of agentic turns.
-
#raw ⇒ Hash
readonly
The full parsed result object.
-
#session_id ⇒ String?
readonly
The session id of this conversation.
-
#text ⇒ String
readonly
The assistant's final text result.
-
#usage ⇒ Hash
readonly
Token usage counts, when present.
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 responseprints the answer.
Instance Attribute Details
#cost_usd ⇒ Float (readonly)
Returns total cost in USD (often 0.0 on a subscription).
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 ⇒ Integer (readonly)
Returns wall-clock duration in milliseconds.
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 ⇒ Boolean (readonly)
Returns whether the CLI reported an 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 ⇒ Integer (readonly)
Returns number of agentic 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 ⇒ Hash (readonly)
Returns the full parsed result object.
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 ⇒ String? (readonly)
Returns the session id of this conversation.
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 ⇒ String (readonly)
Returns the assistant's final text result.
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 ⇒ Hash (readonly)
Returns token usage counts, when present.
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 |