Class: Oz::Model

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/oz/model.rb

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

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Model

Returns a new instance of Model.



33
34
35
36
37
38
# File 'lib/oz/model.rb', line 33

def initialize(attributes = {})
  @attributes = {}
  (attributes || {}).each do |key, value|
    @attributes[key.to_s] = Model.build(value)
  end
end

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).

Returns:

  • (Object, nil)

    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

#eachObject

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

#hashObject



75
76
77
# File 'lib/oz/model.rb', line 75

def hash
  to_h.hash
end

#inspectObject 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.

Returns:

  • (Boolean)

    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

#keysArray<String>

Returns the attribute names present in the payload.

Returns:

  • (Array<String>)

    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

Returns:

  • (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_hHash Also known as: to_hash

Returns a deep copy as plain Ruby Hashes/Arrays with string keys.

Returns:

  • (Hash)

    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