Class: Iglu::SelfDescribingJson
- Inherits:
-
Object
- Object
- Iglu::SelfDescribingJson
- Defined in:
- lib/iglu-client/self_describing_json.rb
Overview
Class holding SchemaVer data
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
-
#schema ⇒ Object
Returns the value of attribute schema.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(schema, data) ⇒ SelfDescribingJson
constructor
Constructor.
- #to_json ⇒ Object
- #valid?(resolver) ⇒ Boolean
-
#validate(resolver) ⇒ Object
Check if JSON is valid (throw exception otherwise).
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
#data ⇒ Object
Returns the value of attribute data.
18 19 20 |
# File 'lib/iglu-client/self_describing_json.rb', line 18 def data @data end |
#schema ⇒ Object
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_json ⇒ Object
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
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 |