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

Returns the value of attribute cost_usd

Returns:

  • (Object)

    the current value of 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_msObject (readonly)

Returns the value of attribute duration_ms

Returns:

  • (Object)

    the current value of 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

#errorObject (readonly)

Returns the value of attribute error

Returns:

  • (Object)

    the current value of 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_turnsObject (readonly)

Returns the value of attribute num_turns

Returns:

  • (Object)

    the current value of 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

#rawObject (readonly)

Returns the value of attribute raw

Returns:

  • (Object)

    the current value of 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_idObject (readonly)

Returns the value of attribute session_id

Returns:

  • (Object)

    the current value of 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

#textObject (readonly)

Returns the value of attribute text

Returns:

  • (Object)

    the current value of 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

#usageObject (readonly)

Returns the value of attribute usage

Returns:

  • (Object)

    the current value of 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.

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