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, #convert, #convert_row

Methods included from Model

#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(schema, opts = {}) ⇒ Schema

Returns a new instance of Schema.



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

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

Instance Method Details

#parse_schema(schema) ⇒ Object



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

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