class Iglu::SelfDescribingJson

Class holding SchemaVer data

Attributes

data[RW]
schema[RW]

Public Class Methods

new(schema, data) click to toggle source

Constructor. To initalize from string - use static parse_schemaver

# File lib/iglu-client/self_describing_json.rb, line 21
def initialize(schema, data)
  @schema = schema
  @data = data
  @valid = false
end
parse_json(json) click to toggle source
# File lib/iglu-client/self_describing_json.rb, line 48
def self.parse_json(json)
  schema = json[:schema] || json['schema']
  data = json[:data] || json['data']
  if schema.nil? or data.nil?
    raise IgluError.new "Not a self-describing JSON"
  end
  schema_key = SchemaKey.parse_key(schema)
  SelfDescribingJson.new(schema_key, data)
end

Public Instance Methods

to_json() click to toggle source
# File lib/iglu-client/self_describing_json.rb, line 27
def to_json
  {
    :schema => @schema.as_uri,
    :data => @data
  }
end
valid?(resolver) click to toggle source
# File lib/iglu-client/self_describing_json.rb, line 40
def valid?(resolver)
  begin
    @valid or validate(resolver)
  rescue JSON::Schema::ValidationError => _
    false
  end
end
validate(resolver) click to toggle source

Check if JSON is valid (throw exception otherwise)

# File lib/iglu-client/self_describing_json.rb, line 35
def validate(resolver)
  @valid = resolver.validate(self.to_json)
  @valid
end