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



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

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

#from_json(str) ⇒ Object



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

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



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

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

#kindObject



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

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

#propertiesObject Also known as: fields



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

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
# File 'lib/hdo/storting_importer/has_json_schema.rb', line 43

def schema_path(path)
  @schema_path = path
end

#valid?(e) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#validate!(e) ⇒ Object



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

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