Class: RubyClaude::Response

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

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cost_usdFloat (readonly)

Returns total cost in USD (often 0.0 on a subscription).

Returns:

  • (Float)

    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_msInteger (readonly)

Returns wall-clock duration in milliseconds.

Returns:

  • (Integer)

    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

#errorBoolean (readonly)

Returns whether the CLI reported an error.

Returns:

  • (Boolean)

    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_turnsInteger (readonly)

Returns number of agentic turns.

Returns:

  • (Integer)

    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

#rawHash (readonly)

Returns the full parsed result object.

Returns:

  • (Hash)

    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_idString? (readonly)

Returns the session id of this conversation.

Returns:

  • (String, nil)

    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

#textString (readonly)

Returns the assistant's final text result.

Returns:

  • (String)

    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

#usageHash (readonly)

Returns token usage counts, when present.

Returns:

  • (Hash)

    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.

Parameters:

  • data (Hash, nil)

    the parsed result object

Returns:



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.

Returns:

  • (Boolean)

    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.

Returns:

  • (Boolean)

    whether the result was successful



50
# File 'lib/ruby_claude/response.rb', line 50

def success? = !error?

#to_sString

Returns the assistant text, so puts response prints the answer.

Returns:

  • (String)


55
# File 'lib/ruby_claude/response.rb', line 55

def to_s = text