Class: Iglu::SelfDescribingJson

Inherits:
Object
  • Object
show all
Defined in:
lib/iglu-client/self_describing_json.rb

Overview

Class holding SchemaVer data

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema, data) ⇒ SelfDescribingJson

Constructor. To initalize from string - use static parse_schemaver



21
22
23
24
25
# File 'lib/iglu-client/self_describing_json.rb', line 21

def initialize(schema, data)
  @schema = schema
  @data = data
  @valid = false
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



18
19
20
# File 'lib/iglu-client/self_describing_json.rb', line 18

def data
  @data
end

#schemaObject

Returns the value of attribute schema.



18
19
20
# File 'lib/iglu-client/self_describing_json.rb', line 18

def schema
  @schema
end

Class Method Details

.parse_json(json) ⇒ Object



48
49
50
51
52
53
54
55
56
# 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

Instance Method Details

#to_jsonObject



27
28
29
30
31
32
# File 'lib/iglu-client/self_describing_json.rb', line 27

def to_json
  {
    :schema => @schema.as_uri,
    :data => @data
  }
end

#valid?(resolver) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
46
# 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) ⇒ Object

Check if JSON is valid (throw exception otherwise)



35
36
37
38
# File 'lib/iglu-client/self_describing_json.rb', line 35

def validate(resolver)
  @valid = resolver.validate(self.to_json)
  @valid
end