lib/geo_combine/geoblacklight.rb in geo_combine-0.1.0 vs lib/geo_combine/geoblacklight.rb in geo_combine-0.2.0

- old
+ new

@@ -1,20 +1,25 @@ +require 'open-uri' + module GeoCombine class Geoblacklight include GeoCombine::Formats include GeoCombine::Subjects include GeoCombine::GeometryTypes attr_reader :metadata + GEOBLACKLIGHT_VERSION = 'v1.1.0' + ## # Initializes a GeoBlacklight object # @param [String] metadata be a valid JSON string document in # GeoBlacklight-Schema # @param [Hash] fields enhancements to metadata that are merged with @metadata def initialize(metadata, fields = {}) @metadata = JSON.parse(metadata).merge(fields) + @schema = nil end ## # Calls metadata enhancement methods for each key, value pair in the # metadata hash @@ -37,11 +42,25 @@ ## # Validates a GeoBlacklight-Schema json document # @return [Boolean] def valid? - schema = JSON.parse(File.read(File.join(File.dirname(__FILE__), '../schema/geoblacklight-schema.json'))) - JSON::Validator.validate!(schema, to_json, validate_schema: true) + @schema ||= JSON.parse(open("https://raw.githubusercontent.com/geoblacklight/geoblacklight/#{GEOBLACKLIGHT_VERSION}/schema/geoblacklight-schema.json").read) + JSON::Validator.validate!(@schema, to_json, fragment: '#/properties/layer') && dct_references_validate! + end + + ## + # Validate dct_references_s + # @return [Boolean] + def dct_references_validate! + return true unless metadata.key?('dct_references_s') + begin + ref = JSON.parse(metadata['dct_references_s']) + raise GeoCombine::Exceptions::InvalidDCTReferences, 'dct_references must be parsed to a Hash' unless ref.is_a?(Hash) + true + rescue JSON::ParserError => e + raise e, "Invalid JSON in dct_references_s: #{e.message}" + end end private ##