Class: JsonTableSchema::Schema

Inherits:
Hash
  • Object
show all
Includes:
Data, Helpers, Model, Validate
Defined in:
lib/jsontableschema/schema.rb

Constant Summary

Constants included from Model

Model::DEFAULTS

Instance Attribute Summary

Attributes included from Data

#errors

Attributes included from Validate

#messages

Instance Method Summary collapse

Methods included from Helpers

#convert_to_boolean, #false_values, #get_class_for_type, #true_values, #type_class_lookup

Methods included from Data

#cast_row, #cast_rows

Methods included from Model

#fields, #foreign_keys, #get_constraints, #get_field, #get_fields_by_type, #get_type, #has_field?, #headers, #primary_keys, #required_headers

Methods included from Validate

#load_validator!, #valid?, #validate

Constructor Details

#initialize(descriptor, opts = {}) ⇒ Schema

Returns a new instance of Schema.



8
9
10
11
12
13
14
15
# File 'lib/jsontableschema/schema.rb', line 8

def initialize(descriptor, opts = {})
  self.merge! parse_schema(descriptor)
  @messages = []
  @opts = opts
  load_fields!
  load_validator!
  expand!
end

Instance Method Details

#parse_schema(descriptor) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/jsontableschema/schema.rb', line 17

def parse_schema(descriptor)
  if descriptor.class == Hash
    descriptor
  elsif descriptor.class == String
    begin
      JSON.parse open(descriptor).read
    rescue Errno::ENOENT
      raise SchemaException.new("File not found at `#{descriptor}`")
    rescue OpenURI::HTTPError => e
      raise SchemaException.new("URL `#{descriptor}` returned #{e.message}")
    rescue JSON::ParserError
      raise SchemaException.new("File at `#{descriptor}` is not valid JSON")
    end
  else
    raise SchemaException.new("A schema must be a hash, path or URL")
  end
end