Skip to content

Commit

Permalink
🎁 URIs to strings, now with local authorities
Browse files Browse the repository at this point in the history
This commit will add a check for local authorities as well as the
external check.  This will include authorities like rights statements
and licenses.
  • Loading branch information
kirkkwang committed May 3, 2024
1 parent 446d900 commit c996414
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
7 changes: 1 addition & 6 deletions app/indexers/allinson_flex/dynamic_indexer_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
17 changes: 16 additions & 1 deletion app/indexers/uri_to_string_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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?
Expand All @@ -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
Expand All @@ -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
6 changes: 6 additions & 0 deletions spec/indexers/uri_to_string_behavior_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit c996414

Please sign in to comment.