Class: E2B::Models::ProcessResult
- Inherits:
-
Object
- Object
- E2B::Models::ProcessResult
- Defined in:
- lib/e2b/models/process_result.rb
Overview
Result of a process execution
Instance Attribute Summary collapse
-
#error ⇒ String?
readonly
Error message if any.
-
#exit_code ⇒ Integer
readonly
Exit code.
-
#stderr ⇒ String
readonly
Standard error.
-
#stdout ⇒ String
(also: #result)
readonly
Standard output.
Class Method Summary collapse
- .decode_base64_safe(data) ⇒ Object
-
.from_connect_response(data) ⇒ ProcessResult
Create from Connect RPC response.
-
.from_hash(data) ⇒ ProcessResult
Create from API response hash.
-
.parse_exit_code(value) ⇒ Object
Parse exit code from various formats Handles: integer 0, string “0”, string “exit status 0”.
Instance Method Summary collapse
-
#initialize(stdout: "", stderr: "", exit_code: 0, error: nil) ⇒ ProcessResult
constructor
A new instance of ProcessResult.
-
#output ⇒ String
Combined output (stdout + stderr).
-
#success? ⇒ Boolean
Check if the process succeeded.
Constructor Details
#initialize(stdout: "", stderr: "", exit_code: 0, error: nil) ⇒ ProcessResult
Returns a new instance of ProcessResult.
102 103 104 105 106 107 |
# File 'lib/e2b/models/process_result.rb', line 102 def initialize(stdout: "", stderr: "", exit_code: 0, error: nil) @stdout = stdout @stderr = stderr @exit_code = exit_code @error = error end |
Instance Attribute Details
#error ⇒ String? (readonly)
Returns Error message if any.
19 20 21 |
# File 'lib/e2b/models/process_result.rb', line 19 def error @error end |
#exit_code ⇒ Integer (readonly)
Returns Exit code.
16 17 18 |
# File 'lib/e2b/models/process_result.rb', line 16 def exit_code @exit_code end |
#stderr ⇒ String (readonly)
Returns Standard error.
13 14 15 |
# File 'lib/e2b/models/process_result.rb', line 13 def stderr @stderr end |
#stdout ⇒ String (readonly) Also known as: result
Returns Standard output.
10 11 12 |
# File 'lib/e2b/models/process_result.rb', line 10 def stdout @stdout end |
Class Method Details
.decode_base64_safe(data) ⇒ Object
82 83 84 |
# File 'lib/e2b/models/process_result.rb', line 82 def self.decode_base64_safe(data) E2B::Services::EnvdBase64.decode_process_output(data) end |
.from_connect_response(data) ⇒ ProcessResult
Create from Connect RPC response
Connect RPC responses for process.Process/Start contain:
-
events: Array of streaming events
-
stdout: Accumulated stdout
-
stderr: Accumulated stderr
-
exit_code: Process exit code
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 77 78 79 80 |
# File 'lib/e2b/models/process_result.rb', line 44 def self.from_connect_response(data) # Defensive: any non-Hash input (nil, String, Array) cannot be a Connect # response, so return an empty result rather than crashing in from_hash. return new unless data.is_a?(Hash) stdout = data[:stdout] || data["stdout"] || "" stderr = data[:stderr] || data["stderr"] || "" exit_code = data[:exit_code] || data["exit_code"] || data["exitCode"] || 0 error = data[:error] || data["error"] # If no stdout but we have events, try to extract from events if stdout.empty? && data[:events].is_a?(Array) data[:events].each do |event| next unless event.is_a?(Hash) # Handle nested event structure next unless event["event"] ev = event["event"] if ev["Stdout"] stdout += decode_base64_safe(ev["Stdout"]["data"]) elsif ev["stdout"] stdout += decode_base64_safe(ev["stdout"]["data"]) elsif ev["Stderr"] stderr += decode_base64_safe(ev["Stderr"]["data"]) elsif ev["stderr"] stderr += decode_base64_safe(ev["stderr"]["data"]) elsif ev["Exit"] exit_code = ev["Exit"]["exitCode"] || ev["Exit"]["exit_code"] || exit_code elsif ev["exit"] exit_code = ev["exit"]["exitCode"] || ev["exit"]["exit_code"] || exit_code end end end new(stdout: stdout, stderr: stderr, exit_code: parse_exit_code(exit_code), error: error) end |
.from_hash(data) ⇒ ProcessResult
Create from API response hash
25 26 27 28 29 30 31 32 |
# File 'lib/e2b/models/process_result.rb', line 25 def self.from_hash(data) new( stdout: data["stdout"] || data[:stdout] || "", stderr: data["stderr"] || data[:stderr] || "", exit_code: data["exitCode"] || data["exit_code"] || data[:exitCode] || 0, error: data["error"] || data[:error] ) end |
.parse_exit_code(value) ⇒ Object
Parse exit code from various formats Handles: integer 0, string “0”, string “exit status 0”
88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/e2b/models/process_result.rb', line 88 def self.parse_exit_code(value) return 0 if value.nil? return value if value.is_a?(Integer) str = value.to_s if str =~ /exit status (\d+)/i ::Regexp.last_match(1).to_i elsif str =~ /(\d+)/ ::Regexp.last_match(1).to_i else str.include?("0") ? 0 : 1 end end |
Instance Method Details
#output ⇒ String
Combined output (stdout + stderr)
119 120 121 |
# File 'lib/e2b/models/process_result.rb', line 119 def output "#{@stdout}#{@stderr}" end |
#success? ⇒ Boolean
Check if the process succeeded
112 113 114 |
# File 'lib/e2b/models/process_result.rb', line 112 def success? @exit_code.zero? && @error.nil? end |