Class: RubyClaude::Event

Inherits:
Data
  • Object
show all
Defined in:
lib/ruby_claude/event.rb

Overview

Immutable streaming event parsed from one line of –output-format stream-json output. The #type mirrors the CLI’s type field as a Symbol (:system, :assistant, :user, :result, …).

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



20
21
22
23
24
25
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/ruby_claude/event.rb', line 20

Event = Data.define(:type, :text, :session_id, :cost_usd, :duration_ms, :raw) do
  # Build an Event from one parsed NDJSON line.
  #
  # @param data [Hash, nil] the parsed line
  # @return [Event]
  def self.from_hash(data)
    data ||= {}
    new(
      type: (data["type"] || "unknown").to_sym,
      text: extract_text(data),
      session_id: data["session_id"],
      cost_usd: data["total_cost_usd"],
      duration_ms: data["duration_ms"],
      raw: data
    )
  end

  # Pull human-readable text out of a parsed line, if any.
  #
  # @param data [Hash]
  # @return [String, nil]
  def self.extract_text(data)
    case data["type"]
    when "assistant", "user"
      message = data["message"] || data
      text_from_content(message["content"])
    when "result"
      data["result"]
    end
  end

  # Join the text from a content array (or pass a bare string through).
  #
  # @param content [String, Array, nil]
  # @return [String, nil]
  def self.text_from_content(content)
    return content if content.is_a?(String)
    return nil unless content.is_a?(Array)

    texts = content
            .select { |block| block.is_a?(Hash) && block["type"] == "text" }
            .filter_map { |block| block["text"] }
    texts.empty? ? nil : texts.join
  end

  # @return [Boolean] whether this is the final result event
  def result? = type == :result

  # @return [Boolean] whether this is an assistant message event
  def assistant? = type == :assistant

  # @return [Boolean] whether this is a system event
  def system? = type == :system

  # @return [Boolean] whether this is a user message event
  def user? = type == :user
end

#duration_msObject (readonly)

Returns the value of attribute duration_ms

Returns:

  • (Object)

    the current value of duration_ms



20
21
22
23
24
25
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/ruby_claude/event.rb', line 20

Event = Data.define(:type, :text, :session_id, :cost_usd, :duration_ms, :raw) do
  # Build an Event from one parsed NDJSON line.
  #
  # @param data [Hash, nil] the parsed line
  # @return [Event]
  def self.from_hash(data)
    data ||= {}
    new(
      type: (data["type"] || "unknown").to_sym,
      text: extract_text(data),
      session_id: data["session_id"],
      cost_usd: data["total_cost_usd"],
      duration_ms: data["duration_ms"],
      raw: data
    )
  end

  # Pull human-readable text out of a parsed line, if any.
  #
  # @param data [Hash]
  # @return [String, nil]
  def self.extract_text(data)
    case data["type"]
    when "assistant", "user"
      message = data["message"] || data
      text_from_content(message["content"])
    when "result"
      data["result"]
    end
  end

  # Join the text from a content array (or pass a bare string through).
  #
  # @param content [String, Array, nil]
  # @return [String, nil]
  def self.text_from_content(content)
    return content if content.is_a?(String)
    return nil unless content.is_a?(Array)

    texts = content
            .select { |block| block.is_a?(Hash) && block["type"] == "text" }
            .filter_map { |block| block["text"] }
    texts.empty? ? nil : texts.join
  end

  # @return [Boolean] whether this is the final result event
  def result? = type == :result

  # @return [Boolean] whether this is an assistant message event
  def assistant? = type == :assistant

  # @return [Boolean] whether this is a system event
  def system? = type == :system

  # @return [Boolean] whether this is a user message event
  def user? = type == :user
end

#rawObject (readonly)

Returns the value of attribute raw

Returns:

  • (Object)

    the current value of raw



20
21
22
23
24
25
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/ruby_claude/event.rb', line 20

Event = Data.define(:type, :text, :session_id, :cost_usd, :duration_ms, :raw) do
  # Build an Event from one parsed NDJSON line.
  #
  # @param data [Hash, nil] the parsed line
  # @return [Event]
  def self.from_hash(data)
    data ||= {}
    new(
      type: (data["type"] || "unknown").to_sym,
      text: extract_text(data),
      session_id: data["session_id"],
      cost_usd: data["total_cost_usd"],
      duration_ms: data["duration_ms"],
      raw: data
    )
  end

  # Pull human-readable text out of a parsed line, if any.
  #
  # @param data [Hash]
  # @return [String, nil]
  def self.extract_text(data)
    case data["type"]
    when "assistant", "user"
      message = data["message"] || data
      text_from_content(message["content"])
    when "result"
      data["result"]
    end
  end

  # Join the text from a content array (or pass a bare string through).
  #
  # @param content [String, Array, nil]
  # @return [String, nil]
  def self.text_from_content(content)
    return content if content.is_a?(String)
    return nil unless content.is_a?(Array)

    texts = content
            .select { |block| block.is_a?(Hash) && block["type"] == "text" }
            .filter_map { |block| block["text"] }
    texts.empty? ? nil : texts.join
  end

  # @return [Boolean] whether this is the final result event
  def result? = type == :result

  # @return [Boolean] whether this is an assistant message event
  def assistant? = type == :assistant

  # @return [Boolean] whether this is a system event
  def system? = type == :system

  # @return [Boolean] whether this is a user message event
  def user? = type == :user
end

#session_idObject (readonly)

Returns the value of attribute session_id

Returns:

  • (Object)

    the current value of session_id



20
21
22
23
24
25
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/ruby_claude/event.rb', line 20

Event = Data.define(:type, :text, :session_id, :cost_usd, :duration_ms, :raw) do
  # Build an Event from one parsed NDJSON line.
  #
  # @param data [Hash, nil] the parsed line
  # @return [Event]
  def self.from_hash(data)
    data ||= {}
    new(
      type: (data["type"] || "unknown").to_sym,
      text: extract_text(data),
      session_id: data["session_id"],
      cost_usd: data["total_cost_usd"],
      duration_ms: data["duration_ms"],
      raw: data
    )
  end

  # Pull human-readable text out of a parsed line, if any.
  #
  # @param data [Hash]
  # @return [String, nil]
  def self.extract_text(data)
    case data["type"]
    when "assistant", "user"
      message = data["message"] || data
      text_from_content(message["content"])
    when "result"
      data["result"]
    end
  end

  # Join the text from a content array (or pass a bare string through).
  #
  # @param content [String, Array, nil]
  # @return [String, nil]
  def self.text_from_content(content)
    return content if content.is_a?(String)
    return nil unless content.is_a?(Array)

    texts = content
            .select { |block| block.is_a?(Hash) && block["type"] == "text" }
            .filter_map { |block| block["text"] }
    texts.empty? ? nil : texts.join
  end

  # @return [Boolean] whether this is the final result event
  def result? = type == :result

  # @return [Boolean] whether this is an assistant message event
  def assistant? = type == :assistant

  # @return [Boolean] whether this is a system event
  def system? = type == :system

  # @return [Boolean] whether this is a user message event
  def user? = type == :user
end

#textObject (readonly)

Returns the value of attribute text

Returns:

  • (Object)

    the current value of text



20
21
22
23
24
25
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/ruby_claude/event.rb', line 20

Event = Data.define(:type, :text, :session_id, :cost_usd, :duration_ms, :raw) do
  # Build an Event from one parsed NDJSON line.
  #
  # @param data [Hash, nil] the parsed line
  # @return [Event]
  def self.from_hash(data)
    data ||= {}
    new(
      type: (data["type"] || "unknown").to_sym,
      text: extract_text(data),
      session_id: data["session_id"],
      cost_usd: data["total_cost_usd"],
      duration_ms: data["duration_ms"],
      raw: data
    )
  end

  # Pull human-readable text out of a parsed line, if any.
  #
  # @param data [Hash]
  # @return [String, nil]
  def self.extract_text(data)
    case data["type"]
    when "assistant", "user"
      message = data["message"] || data
      text_from_content(message["content"])
    when "result"
      data["result"]
    end
  end

  # Join the text from a content array (or pass a bare string through).
  #
  # @param content [String, Array, nil]
  # @return [String, nil]
  def self.text_from_content(content)
    return content if content.is_a?(String)
    return nil unless content.is_a?(Array)

    texts = content
            .select { |block| block.is_a?(Hash) && block["type"] == "text" }
            .filter_map { |block| block["text"] }
    texts.empty? ? nil : texts.join
  end

  # @return [Boolean] whether this is the final result event
  def result? = type == :result

  # @return [Boolean] whether this is an assistant message event
  def assistant? = type == :assistant

  # @return [Boolean] whether this is a system event
  def system? = type == :system

  # @return [Boolean] whether this is a user message event
  def user? = type == :user
end

#typeObject (readonly)

Returns the value of attribute type

Returns:

  • (Object)

    the current value of type



20
21
22
23
24
25
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/ruby_claude/event.rb', line 20

Event = Data.define(:type, :text, :session_id, :cost_usd, :duration_ms, :raw) do
  # Build an Event from one parsed NDJSON line.
  #
  # @param data [Hash, nil] the parsed line
  # @return [Event]
  def self.from_hash(data)
    data ||= {}
    new(
      type: (data["type"] || "unknown").to_sym,
      text: extract_text(data),
      session_id: data["session_id"],
      cost_usd: data["total_cost_usd"],
      duration_ms: data["duration_ms"],
      raw: data
    )
  end

  # Pull human-readable text out of a parsed line, if any.
  #
  # @param data [Hash]
  # @return [String, nil]
  def self.extract_text(data)
    case data["type"]
    when "assistant", "user"
      message = data["message"] || data
      text_from_content(message["content"])
    when "result"
      data["result"]
    end
  end

  # Join the text from a content array (or pass a bare string through).
  #
  # @param content [String, Array, nil]
  # @return [String, nil]
  def self.text_from_content(content)
    return content if content.is_a?(String)
    return nil unless content.is_a?(Array)

    texts = content
            .select { |block| block.is_a?(Hash) && block["type"] == "text" }
            .filter_map { |block| block["text"] }
    texts.empty? ? nil : texts.join
  end

  # @return [Boolean] whether this is the final result event
  def result? = type == :result

  # @return [Boolean] whether this is an assistant message event
  def assistant? = type == :assistant

  # @return [Boolean] whether this is a system event
  def system? = type == :system

  # @return [Boolean] whether this is a user message event
  def user? = type == :user
end

Class Method Details

.extract_text(data) ⇒ String?

Pull human-readable text out of a parsed line, if any.

Parameters:

  • data (Hash)

Returns:

  • (String, nil)


41
42
43
44
45
46
47
48
49
# File 'lib/ruby_claude/event.rb', line 41

def self.extract_text(data)
  case data["type"]
  when "assistant", "user"
    message = data["message"] || data
    text_from_content(message["content"])
  when "result"
    data["result"]
  end
end

.from_hash(data) ⇒ Event

Build an Event from one parsed NDJSON line.

Parameters:

  • data (Hash, nil)

    the parsed line

Returns:



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ruby_claude/event.rb', line 25

def self.from_hash(data)
  data ||= {}
  new(
    type: (data["type"] || "unknown").to_sym,
    text: extract_text(data),
    session_id: data["session_id"],
    cost_usd: data["total_cost_usd"],
    duration_ms: data["duration_ms"],
    raw: data
  )
end

.text_from_content(content) ⇒ String?

Join the text from a content array (or pass a bare string through).

Parameters:

  • content (String, Array, nil)

Returns:

  • (String, nil)


55
56
57
58
59
60
61
62
63
# File 'lib/ruby_claude/event.rb', line 55

def self.text_from_content(content)
  return content if content.is_a?(String)
  return nil unless content.is_a?(Array)

  texts = content
          .select { |block| block.is_a?(Hash) && block["type"] == "text" }
          .filter_map { |block| block["text"] }
  texts.empty? ? nil : texts.join
end

Instance Method Details

#assistant?Boolean

Returns whether this is an assistant message event.

Returns:

  • (Boolean)

    whether this is an assistant message event



69
# File 'lib/ruby_claude/event.rb', line 69

def assistant? = type == :assistant

#result?Boolean

Returns whether this is the final result event.

Returns:

  • (Boolean)

    whether this is the final result event



66
# File 'lib/ruby_claude/event.rb', line 66

def result? = type == :result

#system?Boolean

Returns whether this is a system event.

Returns:

  • (Boolean)

    whether this is a system event



72
# File 'lib/ruby_claude/event.rb', line 72

def system? = type == :system

#user?Boolean

Returns whether this is a user message event.

Returns:

  • (Boolean)

    whether this is a user message event



75
# File 'lib/ruby_claude/event.rb', line 75

def user? = type == :user