From c9964146fffb24f64737e32d41a8005bae12bd7f Mon Sep 17 00:00:00 2001 From: Kirk Wang Date: Fri, 3 May 2024 11:07:06 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=81=20URIs=20to=20strings,=20now=20wit?= =?UTF-8?q?h=20local=20authorities?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit will add a check for local authorities as well as the external check. This will include authorities like rights statements and licenses. --- .../allinson_flex/dynamic_indexer_behavior.rb | 7 +------ app/indexers/uri_to_string_behavior.rb | 17 ++++++++++++++++- spec/indexers/uri_to_string_behavior_spec.rb | 6 ++++++ 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/app/indexers/allinson_flex/dynamic_indexer_behavior.rb b/app/indexers/allinson_flex/dynamic_indexer_behavior.rb index 105c7a14..8abc2f1b 100644 --- a/app/indexers/allinson_flex/dynamic_indexer_behavior.rb +++ b/app/indexers/allinson_flex/dynamic_indexer_behavior.rb @@ -43,12 +43,7 @@ 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) + props_hash.keys.select { |k, _v| props_hash[k][:range] == RANGE } - ['rdf_type'] end end end diff --git a/app/indexers/uri_to_string_behavior.rb b/app/indexers/uri_to_string_behavior.rb index 4de5a32b..62ba14f0 100644 --- a/app/indexers/uri_to_string_behavior.rb +++ b/app/indexers/uri_to_string_behavior.rb @@ -6,7 +6,7 @@ module UriToStringBehavior # 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. + # Retrieves a value for a given URI, can search id.loc.gov or local authorities # # @param value [String] the value to retrieve. If this value starts with 'http', it is treated as a URI. # @return [String] @@ -15,6 +15,8 @@ module UriToStringBehavior # 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" + # uri_to_value_for('http://creativecommons.org/licenses/by-sa/4.0/') #=> "Creative Commons BY-SA Attribution-ShareAlike 4.0 International" + # uri_to_value_for('http://rightsstatements.org/vocab/InC/1.0/') #=> "In Copyright" def uri_to_value_for(value) return value.map { |v| uri_to_value_for(v) } if value.is_a?(Enumerable) return if value.blank? @@ -23,6 +25,9 @@ def uri_to_value_for(value) uri = value + string = search_local_authority(uri) + return string if string.present? + begin graph = RDF::Graph.load(uri) rescue StandardError => e @@ -38,4 +43,14 @@ def uri_to_value_for(value) object.to_s end + + private + + def search_local_authority(uri) + Qa::Authorities::Local.names.detect do |authority| + term = Qa::Authorities::Local.subauthority_for(authority).find(uri).fetch('term', nil) + return term if term + end + nil + end end diff --git a/spec/indexers/uri_to_string_behavior_spec.rb b/spec/indexers/uri_to_string_behavior_spec.rb index e7841cf9..551c6bdb 100644 --- a/spec/indexers/uri_to_string_behavior_spec.rb +++ b/spec/indexers/uri_to_string_behavior_spec.rb @@ -120,5 +120,11 @@ expect(subject.uri_to_value_for('http://example.com')).to eq 'http://example.com (No label found)' end end + + context 'when the URI is a local authority' do + it 'returns the value' do + expect(subject.uri_to_value_for('http://rightsstatements.org/vocab/InC/1.0/')).to eq 'In Copyright' + end + end end end