Class: Heroics::Schema

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

Overview

A wrapper around a bare JSON schema to make it easier to use.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema) ⇒ Schema

Instantiate a schema.

Parameters:

  • schema (Hash)

    The bare JSON schema to wrap.



10
11
12
13
14
15
16
# File 'lib/heroics/schema.rb', line 10

def initialize(schema)
  @schema = schema
  @resources = {}
  @schema['properties'].each do |key, value|
    @resources[key] = ResourceSchema.new(@schema, key)
  end
end

Instance Attribute Details

#schemaObject (readonly)

Returns the value of attribute schema.



5
6
7
# File 'lib/heroics/schema.rb', line 5

def schema
  @schema
end

Instance Method Details

#descriptionObject

A description of the API.



19
20
21
# File 'lib/heroics/schema.rb', line 19

def description
  @schema['description']
end

#inspectObject Also known as: to_s

Get a simple human-readable representation of this client instance.



43
44
45
# File 'lib/heroics/schema.rb', line 43

def inspect
  "#<Heroics::Schema description=\"#{@schema['description']}\">"
end

#resource(name) ⇒ Object

Get a schema for a named resource.

Parameters:

  • name (String)

    The name of the resource.

Raises:

  • (SchemaError)

    Raised if an unknown resource name is provided.



27
28
29
30
31
32
33
# File 'lib/heroics/schema.rb', line 27

def resource(name)
  if @schema['definitions'].has_key?(name)
    ResourceSchema.new(@schema, name)
  else
    raise SchemaError.new("Unknown resource '#{name}'.")
  end
end

#resourcesArray<ResourceSchema>

The resource schema children that are part of this schema.

Returns:



38
39
40
# File 'lib/heroics/schema.rb', line 38

def resources
  @resources.values
end