class GeoCombine::Geoblacklight
Constants
- DEPRECATED_KEYS_V1
- GEOBLACKLIGHT_VERSION
- SCHEMA_JSON_URL
Attributes
Public Class Methods
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
# File lib/geo_combine/geoblacklight.rb, line 30 def initialize(metadata, fields = {}) @metadata = JSON.parse(metadata).merge(fields) @schema = nil end
Public Instance Methods
Validate dct_references_s @return [Boolean]
# File lib/geo_combine/geoblacklight.rb, line 70 def dct_references_validate! return true unless metadata.key?('dct_references_s') # TODO: shouldn't we require this field? 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
Calls metadata enhancement methods for each key, value pair in the metadata hash
# File lib/geo_combine/geoblacklight.rb, line 38 def enhance_metadata upgrade_to_v1 if metadata['geoblacklight_version'].blank? metadata.each do |key, value| translate_formats(key, value) enhance_subjects(key, value) format_proper_date(key, value) fields_should_be_array(key, value) translate_geometry_type(key, value) end end
# File lib/geo_combine/geoblacklight.rb, line 81 def spatial_validate! GeoCombine::BoundingBox.from_envelope(metadata['solr_geom']).valid? end
Returns a string of JSON from a GeoBlacklight hash @return (String)
# File lib/geo_combine/geoblacklight.rb, line 53 def to_json(options = {}) metadata.to_json(options) end
Validates a GeoBlacklight-Schema json document @return [Boolean]
# File lib/geo_combine/geoblacklight.rb, line 60 def valid? @schema ||= JSON.parse(open(SCHEMA_JSON_URL).read) JSON::Validator.validate!(@schema, to_json, fragment: '#/properties/layer') && dct_references_validate! && spatial_validate! end
Private Instance Methods
Enhances the 'dc_subject_sm' field by translating subjects to ISO topic categories
# File lib/geo_combine/geoblacklight.rb, line 105 def enhance_subjects(key, value) return unless key == 'dc_subject_sm' metadata[key] = value.map do |val| if subjects.include?(val) subjects[val] else val end end end
# File lib/geo_combine/geoblacklight.rb, line 124 def fields_should_be_array(key, value) return unless should_be_array.include?(key) && !value.is_a?(Array) metadata[key] = [value] end
Formats
the 'layer_modified_dt' to a valid valid RFC3339 date/time string and ISO8601 (for indexing into Solr)
# File lib/geo_combine/geoblacklight.rb, line 119 def format_proper_date(key, value) return unless key == 'layer_modified_dt' metadata[key] = Time.parse(value).utc.iso8601 end
GeoBlacklight-Schema fields that should be type Array
# File lib/geo_combine/geoblacklight.rb, line 131 def should_be_array %w[ dc_creator_sm dc_subject_sm dct_spatial_sm dct_temporal_sm dct_isPartOf_sm ].freeze end
Enhances the 'dc_format_s' field by translating a format type to a valid GeoBlacklight-Schema format
# File lib/geo_combine/geoblacklight.rb, line 90 def translate_formats(key, value) return unless key == 'dc_format_s' && formats.include?(value) metadata[key] = formats[value] end
Enhances the 'layer_geom_type_s' field by translating from known types
# File lib/geo_combine/geoblacklight.rb, line 97 def translate_geometry_type(key, value) return unless key == 'layer_geom_type_s' && geometry_types.include?(value) metadata[key] = geometry_types[value] end
Converts a pre-v1.0 schema into a compliant v1.0 schema
# File lib/geo_combine/geoblacklight.rb, line 143 def upgrade_to_v1 metadata['geoblacklight_version'] = '1.0' # ensure required fields metadata['dc_identifier_s'] = metadata['uuid'] if metadata['dc_identifier_s'].blank? # normalize to alphanum and - only metadata['layer_slug_s'].gsub!(/[^[[:alnum:]]]+/, '-') if metadata['layer_slug_s'].present? # remove deprecated fields metadata.except!(*DEPRECATED_KEYS_V1) # ensure we have a proper v1 record valid? end