Class: JsonTableSchema::Schema
- Inherits:
-
Hash
- Object
- Hash
- JsonTableSchema::Schema
- Defined in:
- lib/jsontableschema/schema.rb
Constant Summary
Constants included from Model
Instance Attribute Summary
Attributes included from Data
Attributes included from Validate
Instance Method Summary collapse
-
#initialize(descriptor, opts = {}) ⇒ Schema
constructor
A new instance of Schema.
- #parse_schema(descriptor) ⇒ Object
Methods included from Helpers
#convert_to_boolean, #false_values, #get_class_for_type, #true_values, #type_class_lookup
Methods included from Data
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! 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.}") 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 |