Class: Aries::Schema

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

Instance Method Summary collapse

Constructor Details

#initialize(schema) ⇒ Schema

Returns a new instance of Schema.

Parameters:

  • Hash (JSON)

    Json Schema Object

Raises:

  • (JsonSchema::SchemaError)


9
10
11
# File 'lib/aries/schema.rb', line 9

def initialize schema
  @schema = ::JsonSchema.parse!(schema).tap(&:expand_references!)
end

Instance Method Details

#base_urlString

Find base url of api

Returns:

  • (String)

    url

Raises:



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

def base_url
  root_link.try(:href) or raise BaseUrlNotFound
end

#inspectObject



49
50
51
# File 'lib/aries/schema.rb', line 49

def inspect
  "#<Aries::Schema base_url=#{base_url}>"
end

#property_by(href) ⇒ JsonSchema::Schema

Examples:

property_by "#/definitions/apps/definitions/identiry" #=> JsonSchema::Schema

Parameters:

  • href (String)

Returns:

  • (JsonSchema::Schema)


40
41
42
43
44
45
46
47
# File 'lib/aries/schema.rb', line 40

def property_by href
  accessor = href.gsub(/([a-zA-Z0-9\-\_]+)/).to_a
  result = @schema
  accessor.each_with_index do |a, i|
    result = (i % 2) == 0 ? result.send(a.to_sym) : result[a]
  end
  result
end

#resourcesObject

Resources of json schema

Parameters:



15
16
17
18
19
# File 'lib/aries/schema.rb', line 15

def resources
  @resources ||= @schema.properties.map do |name, schema|
    Resource.new name, schema, self
  end
end

Finds link that has “self” rel to resolve API base URL

Returns:

  • (JsonSchema::Schema::Link, nil)


30
31
32
33
34
# File 'lib/aries/schema.rb', line 30

def root_link
  @schema.links.find do |link|
    link.rel == "self"
  end
end