Class: Gutsy::Schema

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json_schema) ⇒ Schema

Returns a new instance of Schema.



17
18
19
# File 'lib/gutsy/schema.rb', line 17

def initialize(json_schema)
  @schema = json_schema
end

Class Method Details

.load_from_file!(schema_path) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/gutsy/schema.rb', line 3

def self.load_from_file!(schema_path)
  draft04_uri = URI.parse("http://json-schema.org/draft-04/hyper-schema")
  draft04 = JsonSchema.parse!(JSON.parse(draft04_uri.read))

  schema_json = JSON.parse(File.read(schema_path))

  schema = JsonSchema.parse!(schema_json)
  schema.expand_references!

  draft04.validate!(schema)

  new(schema)
end

Instance Method Details

#resourcesObject



21
22
23
24
25
26
27
28
29
30
# File 'lib/gutsy/schema.rb', line 21

def resources
  @resources ||= Hash[schema.definitions.map do |key, resource|
    links = Hash[resource.links.map do |link|
      link.schema.expand_references! if link.schema
      properties = link.schema.try(:properties) || {}
      [link.title.downcase.to_sym, OpenStruct.new(properties: properties)]
    end]
    [key.to_sym, OpenStruct.new(title: key.camelize, links: links)]
  end]
end