-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #602 from scientist-softserv/uri-to-human-readable…
…-string 🎁 Convert URI to human readable strings
- Loading branch information
Showing
13 changed files
with
288 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# frozen_string_literal: true | ||
|
||
# OVERRIDE AllinsonFlex v0.1.0 to turn URI's into human readable strings | ||
# | ||
# For whatever reason our decorator pattern was not overriding #generate_solr_document | ||
# If you check AllinsonFlex::DynamicIndexerBehavior.ancestors, you get what you expect | ||
# with a decorator. If you checked the #source_location of :generate_solr_document | ||
# you also get what you expect. However, when running a reindex, I was not going | ||
# through the decorator. I decided since this is only one method, overriding the entire | ||
# mixin was acceptable for now. | ||
|
||
module AllinsonFlex | ||
module DynamicIndexerBehavior | ||
include UriToStringBehavior | ||
extend ActiveSupport::Concern | ||
|
||
RANGE = "http://www.w3.org/2001/XMLSchema#anyURI" | ||
|
||
included do | ||
class_attribute :model_class | ||
end | ||
|
||
def generate_solr_document | ||
dynamic_schema_service = object.dynamic_schema_service | ||
uri_properties = uri_properties_from(dynamic_schema_service) | ||
|
||
super.tap do |solr_doc| | ||
dynamic_schema_service.indexing_properties.each_pair do |prop_name, index_field_names| | ||
value = if uri_properties.include?(prop_name.to_s) | ||
uri_to_value_for(object.send(prop_name)) | ||
else | ||
object.send(prop_name) | ||
end | ||
|
||
index_field_names.each { |index_field_name| solr_doc[index_field_name] = value } | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def uri_properties_from(dynamic_schema_service) | ||
schema = HashWithIndifferentAccess.new(dynamic_schema_service.dynamic_schema.schema) | ||
props_hash = schema[:properties] | ||
# remove rdf_type since they are also URIs but not for our purposes | ||
props_hash.keys.select { |k, _v| props_hash[k][:range] == RANGE } - local_authorities - ['rdf_type'] | ||
end | ||
|
||
def local_authorities | ||
# Hyku has these pluralized while m3 has these singularized | ||
Qa::Authorities::Local.names.map(&:singularize) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# frozen_string_literal: true | ||
|
||
module UriToStringBehavior | ||
extend ActiveSupport::Concern | ||
|
||
# UTK uses this label to house the value that needs to be rendered | ||
LABEL = "http://www.w3.org/2004/02/skos/core#prefLabel" | ||
|
||
# Retrieves a value for a given URI. | ||
# | ||
# @param value [String] the value to retrieve. If this value starts with 'http', it is treated as a URI. | ||
# @return [String] | ||
# | ||
# @example | ||
# uri_to_value_for('http://example.com') #=> "Failed to load RDF data: ..." | ||
# uri_to_value_for('http://id.loc.gov/authorities/names/n2017180154') #=> "University of Tennessee" | ||
# uri_to_value_for('Doe, John') #=> "Doe, John" | ||
def uri_to_value_for(value) | ||
return value.map { |v| uri_to_value_for(v) } if value.is_a?(Enumerable) | ||
return if value.blank? | ||
return value unless value.is_a?(String) | ||
return value unless value.start_with?('http') | ||
|
||
uri = value | ||
|
||
begin | ||
graph = RDF::Graph.load(uri) | ||
rescue StandardError => e | ||
Rails.logger.error("Failed to load RDF data: #{e.message}") | ||
return "#{uri} (Failed to load URI)" | ||
end | ||
|
||
subject = RDF::URI.new(uri) | ||
predicate = RDF::URI.new(LABEL) | ||
object = graph.query([subject, predicate, nil]).objects.first | ||
return "#{uri} (No label found)" if object.blank? | ||
|
||
object.to_s | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.