Module: Hdo::StortingImporter::HasJsonSchema::ClassMethods

Defined in:
lib/hdo/storting_importer/has_json_schema.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#schemaObject (readonly)

Returns the value of attribute schema.



41
42
43
# File 'lib/hdo/storting_importer/has_json_schema.rb', line 41

def schema
  @schema
end

Instance Method Details

#descriptionObject



60
61
62
# File 'lib/hdo/storting_importer/has_json_schema.rb', line 60

def description
  @description ||= schema['description']
end

#from_json(str) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/hdo/storting_importer/has_json_schema.rb', line 75

def from_json(str)
  data = MultiJson.decode(str)

  case data
  when Array
    data.map { |e| from_hash validate!(e) }
  when Hash
    from_hash validate!(data)
  else
    raise TypeError, "expected Array or Hash, got #{data.inspect}:#{data.class}"
  end
end

#json_exampleObject



52
53
54
# File 'lib/hdo/storting_importer/has_json_schema.rb', line 52

def json_example
  @json_example ||= Util.json_pretty example
end

#kindObject



56
57
58
# File 'lib/hdo/storting_importer/has_json_schema.rb', line 56

def kind
  @kind ||= schema['properties']['kind']['default']
end

#propertiesObject Also known as: fields



64
65
66
67
68
69
70
# File 'lib/hdo/storting_importer/has_json_schema.rb', line 64

def properties
  @properties ||= (
    schema['properties'].map do |name, data|
      Field.new(name, !!data['required'], data['type'], data['description'] || 'unknown')
    end
  )
end

#schema_path(path) ⇒ Object



43
44
45
46
# File 'lib/hdo/storting_importer/has_json_schema.rb', line 43

def schema_path(path)
  @schema = MultiJson.decode(open(path).read)
  HasJsonSchema.schemas << @schema
end

#valid?(e) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/hdo/storting_importer/has_json_schema.rb', line 88

def valid?(e)
  Jschematic.validate(e, schema, :context => HasJsonSchema.schemas, :debug => true)
end

#validate!(e) ⇒ Object



92
93
94
95
96
97
# File 'lib/hdo/storting_importer/has_json_schema.rb', line 92

def validate!(e)
  Jschematic.validate!(e, schema, :context => HasJsonSchema.schemas, :debug => true)
  e
rescue Jschematic::ValidationError => ex
  raise ValidationError, "#{ex.message}: #{e.inspect}"
end