Class: Oz::Model
Overview
A light-weight, read-only wrapper around the JSON returned by the API.
Rather than hand-maintaining a class for every response shape, the SDK wraps decoded JSON objects in Model. Attributes are reachable both as methods and via [], and nested objects/arrays are wrapped recursively:
run = client.agent.run(prompt: "Fix the bug")
run.run_id # => "abc123"
run.state # => "QUEUED"
run["task_id"] # => "abc123"
run.at_capacity? # => false (predicate form for booleans)
run.to_h # => plain Hash with string keys
Unknown attributes return nil instead of raising, because the API omits optional fields. Use #key? when you need to distinguish “absent” from “present but null”.
Class Method Summary collapse
-
.build(value) ⇒ Object
Recursively wraps
value: Hashes become Model, Arrays are mapped, and scalars are returned untouched.
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
-
#[](key) ⇒ Object?
The value stored under
key(symbol or string). -
#each ⇒ Object
Iterates over [key, value] pairs (values stay wrapped).
- #hash ⇒ Object
-
#initialize(attributes = {}) ⇒ Model
constructor
A new instance of Model.
- #inspect ⇒ Object (also: #to_s)
-
#key?(key) ⇒ Boolean
(also: #has_key?, #member?)
Whether
keyis present in the payload. -
#keys ⇒ Array<String>
The attribute names present in the payload.
- #method_missing(name, *args) ⇒ Object
- #respond_to_missing?(name, include_private = false) ⇒ Boolean
-
#to_h ⇒ Hash
(also: #to_hash)
A deep copy as plain Ruby Hashes/Arrays with string keys.
Constructor Details
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
93 94 95 96 97 98 99 |
# File 'lib/oz/model.rb', line 93 def method_missing(name, *args) method = name.to_s return !!@attributes[method.chomp('?')] if method.end_with?('?') && args.empty? return @attributes[method] if reader?(method) && args.empty? super end |
Class Method Details
.build(value) ⇒ Object
Recursively wraps value: Hashes become Oz::Model, Arrays are mapped, and scalars are returned untouched.
25 26 27 28 29 30 31 |
# File 'lib/oz/model.rb', line 25 def self.build(value) case value when Hash then new(value) when Array then value.map { |item| build(item) } else value # scalars and existing Models pass through unchanged end end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
70 71 72 |
# File 'lib/oz/model.rb', line 70 def ==(other) other.is_a?(Model) && other.to_h == to_h end |
#[](key) ⇒ Object?
Returns the value stored under key (symbol or string).
41 42 43 |
# File 'lib/oz/model.rb', line 41 def [](key) @attributes[key.to_s] end |
#each ⇒ Object
Iterates over [key, value] pairs (values stay wrapped).
58 59 60 61 62 |
# File 'lib/oz/model.rb', line 58 def each(&) return enum_for(:each) unless block_given? @attributes.each(&) end |
#hash ⇒ Object
75 76 77 |
# File 'lib/oz/model.rb', line 75 def hash to_h.hash end |
#inspect ⇒ Object Also known as: to_s
79 80 81 82 |
# File 'lib/oz/model.rb', line 79 def inspect pairs = @attributes.map { |key, value| "#{key}=#{value.inspect}" } "#<Oz::Model #{pairs.join(' ')}>" end |
#key?(key) ⇒ Boolean Also known as: has_key?, member?
Returns whether key is present in the payload.
46 47 48 |
# File 'lib/oz/model.rb', line 46 def key?(key) @attributes.key?(key.to_s) end |
#keys ⇒ Array<String>
Returns the attribute names present in the payload.
53 54 55 |
# File 'lib/oz/model.rb', line 53 def keys @attributes.keys end |
#respond_to_missing?(name, include_private = false) ⇒ Boolean
85 86 87 88 89 90 91 |
# File 'lib/oz/model.rb', line 85 def respond_to_missing?(name, include_private = false) method = name.to_s return true if method.end_with?('?') return true if reader?(method) super end |
#to_h ⇒ Hash Also known as: to_hash
Returns a deep copy as plain Ruby Hashes/Arrays with string keys.
65 66 67 |
# File 'lib/oz/model.rb', line 65 def to_h @attributes.transform_values { |value| unwrap(value) } end |