Class: DataPackage::Profile
- Inherits:
-
Hash
- Object
- Hash
- DataPackage::Profile
- Includes:
- Helpers
- Defined in:
- lib/datapackage/profile.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Public.
Instance Method Summary collapse
-
#initialize(descriptor) ⇒ Profile
constructor
A new instance of Profile.
-
#iter_errors(data) ⇒ Object
Lazily yields each ValidationError raised for data.
- #jsonschema ⇒ Object
-
#valid?(data) ⇒ Boolean
(also: #valid)
Returns true if there are no errors in data, false if there are.
-
#validate(data) ⇒ Object
Validate data against this profile.
Methods included from Helpers
#base_path, #dereference_descriptor, #is_fully_qualified_url?, #is_safe_path?, #join_paths, #load_json, #resolve_json_reference
Constructor Details
#initialize(descriptor) ⇒ Profile
Returns a new instance of Profile.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/datapackage/profile.rb', line 9 def initialize(descriptor) unless descriptor.is_a?(String) raise ProfileException.new 'Profile must be a URL or registry identifier' end @name = descriptor if is_fully_qualified_url?(descriptor) self.merge!(load_json(descriptor)) else self.merge!(get_profile_from_registry(descriptor)) end rescue OpenURI::HTTPError, SocketError => e raise ProfileException.new "Profile URL returned #{e.}" rescue JSON::ParserError raise ProfileException.new 'Profile is not valid JSON' end |
Instance Attribute Details
#name ⇒ Object (readonly)
Public
7 8 9 |
# File 'lib/datapackage/profile.rb', line 7 def name @name end |
Instance Method Details
#iter_errors(data) ⇒ Object
Lazily yields each ValidationError raised for data
37 38 39 |
# File 'lib/datapackage/profile.rb', line 37 def iter_errors(data) JSON::Validator.fully_validate(self, data).each{ |error| yield error } end |
#jsonschema ⇒ Object
25 26 27 |
# File 'lib/datapackage/profile.rb', line 25 def jsonschema self.to_h end |
#valid?(data) ⇒ Boolean Also known as: valid
Returns true if there are no errors in data, false if there are
42 43 44 |
# File 'lib/datapackage/profile.rb', line 42 def valid?(data) JSON::Validator.validate(self, data) end |
#validate(data) ⇒ Object
Validate data against this profile. Returns true or raises DataPackage::ValidationError
30 31 32 33 34 |
# File 'lib/datapackage/profile.rb', line 30 def validate(data) JSON::Validator.validate!(self, data) rescue JSON::Schema::ValidationError => e raise DataPackage::ValidationError.new(e.) end |