Class: OmniAI::Schema::Format

Inherits:
Object
  • Object
show all
Defined in:
lib/omniai/schema/format.rb

Overview

Examples:

format = OmniAI::Schema::Format.deserialize({
  name: "example",
  schema: {
    type: "object",
    properties: {
      name: { type: "string" },
    },
    required: ["name"],
  }
})
format.serialize # => { name: "example", schema: { ... } }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, schema:) ⇒ Format

Returns a new instance of Format.

Parameters:



44
45
46
47
# File 'lib/omniai/schema/format.rb', line 44

def initialize(name:, schema:)
  @name = name
  @schema = schema
end

Instance Attribute Details

#nameString

Returns:

  • (String)


20
21
22
# File 'lib/omniai/schema/format.rb', line 20

def name
  @name
end

#schemaOmniAI::Schema::Object



24
25
26
# File 'lib/omniai/schema/format.rb', line 24

def schema
  @schema
end

Class Method Details

.deserialize(data) ⇒ OmniAI::Schema::Format

Examples:

array = OmniAI::Schema::Format.deserialize({
  name: "Contact",
  schema: { ... },
}) # => OmniAI::Schema::Format

Parameters:

  • data (Hash)

Returns:



35
36
37
38
39
40
# File 'lib/omniai/schema/format.rb', line 35

def self.deserialize(data)
  name = data["name"] || data[:name]
  schema = OmniAI::Schema.deserialize(data["schema"] || data[:schema])

  new(name:, schema:)
end

Instance Method Details

#parse(text) ⇒ Hash

Examples:

format.parse("{ "name": "Ringo Starr" }"") # => { name: "Ringo Starr" }

Parameters:

  • text (String)

Returns:

  • (Hash)


66
67
68
# File 'lib/omniai/schema/format.rb', line 66

def parse(text)
  schema.parse(JSON.parse(text))
end

#serializeHash

Examples:

format.serialize # => { name: "...", schema: { ... } }

Returns:

  • (Hash)


53
54
55
56
57
58
# File 'lib/omniai/schema/format.rb', line 53

def serialize
  {
    name:,
    schema: schema.serialize,
  }
end