Skip to content

Commit

Permalink
Allow case-insensitive license matching
Browse files Browse the repository at this point in the history
  • Loading branch information
fbacall committed Mar 4, 2024
1 parent be703ea commit c38c963
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
9 changes: 6 additions & 3 deletions app/dictionaries/licence_dictionary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ def licence_abbreviations
@abbrvs ||= @dictionary.keys
end

def licence_names(licence_dictionary=@dictionary)
def licence_names(licence_dictionary = @dictionary)
@licence_names ||= licence_dictionary.map { |_, value| value['title'] }
end

def lookup_uri(uri)
@uri_mapping[uri]
# Translate a URI or ID in the wrong case to a valid ID, if one exists.
def normalize_id(id)
@uri_mapping[id] || @downcase_mapping[id.downcase]
end

def grouped_options_for_select(existing = nil)
Expand Down Expand Up @@ -52,13 +53,15 @@ def dictionary_filepath
def load_dictionary
d = super
@uri_mapping = {}
@downcase_mapping = {}
d.each do |id, data|
uris = data['see_also'] || []
uris << data['reference']
uris << data['details_url']
uris.reject(&:blank?).each do |uri|
@uri_mapping[uri] = id
end
@downcase_mapping[id.downcase] = id
end
d
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/concerns/has_licence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module HasLicence
# Allows setting of the license either by using the key (CC-BY-4.0) #
# or license URL (https://creativecommons.org/licenses/by/4.0/)
def licence=(key_or_uri)
id = LicenceDictionary.instance.lookup_uri(key_or_uri)
id = LicenceDictionary.instance.normalize_id(key_or_uri)

super(id || key_or_uri)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/ingestors/zenodo_ingestor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def process_material(input)
material.title = metadata['title'] unless metadata['title'].nil?
material.description = process_description metadata['description']
material.keywords = metadata['keywords'] unless metadata['keywords'].nil?
material.licence = metadata['license']['id'].upcase unless metadata.dig('license', 'id').nil?
material.licence = metadata['license']['id'] unless metadata.dig('license', 'id').nil?
unless metadata['creators'].nil?
metadata['creators'].each do |c|
entry = c['orcid'].nil? ? c['name'] : "#{c['name']} (orcid: #{c['orcid']})"
Expand Down
14 changes: 13 additions & 1 deletion test/models/material_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ class MaterialTest < ActiveSupport::TestCase
assert_includes m.node_names, nodes(:westeros).name
end

test 'can set licence either using key, URL or SPDX URL' do
test 'can set licence either using key, URL or SPDX URL in any case' do
m = materials(:good_material)

m.licence = 'CC-BY-4.0'
Expand All @@ -243,6 +243,18 @@ class MaterialTest < ActiveSupport::TestCase
m.licence = 'https://not.a.real.licence.golf'
refute m.valid?
assert_equal 'https://not.a.real.licence.golf', m.licence, "should preserve URL user input if it didn't match any licenses in the dictionary"

m.licence = 'cc-by-4.0'
assert m.valid?
assert_equal 'CC-BY-4.0', m.licence

m.licence = 'MPL-2.0-NO-COPYLEFT-EXCEPTION'
assert m.valid?
assert_equal 'MPL-2.0-no-copyleft-exception', m.licence

m.licence = 'abcXYZ123'
refute m.valid?
assert_equal 'abcXYZ123', m.licence, "should preserve user input if it didn't match any licenses in the dictionary"
end

test 'can check if matearial is stale (has not been scraped recently)' do
Expand Down

0 comments on commit c38c963

Please sign in to comment.