diff --git a/lib/qa/authorities/loc.rb b/lib/qa/authorities/loc.rb index 3af05c6e..cc54bbc4 100644 --- a/lib/qa/authorities/loc.rb +++ b/lib/qa/authorities/loc.rb @@ -14,5 +14,17 @@ def self.subauthority_for(subauthority) def self.subauthorities authorities + vocabularies + datatypes + preservation end + + # @note The returned value is the root directory of the URL. The graphicMaterials sub-authority + # has a "type" of vocabulary. https://id.loc.gov/vocabulary/graphicMaterials/tgm008083.html + # In some cases, this is plural and in others this is singular. + # @return [String] + def self.type_for(subauthority:) + validate_subauthority!(subauthority) + return "authorities" if authorities.include?(subauthority) + return "vocabulary" if vocabularies.include?(subauthority) + return "datatype" if datatypes.include?(subauthority) + return "preservation" if preservation.include?(subauthority) + end end end diff --git a/lib/qa/authorities/loc/generic_authority.rb b/lib/qa/authorities/loc/generic_authority.rb index 21b3b3b5..2f746b1c 100644 --- a/lib/qa/authorities/loc/generic_authority.rb +++ b/lib/qa/authorities/loc/generic_authority.rb @@ -4,6 +4,7 @@ class Loc::GenericAuthority < Base def initialize(subauthority) super() @subauthority = subauthority + @subauthority_type = Loc.type_for(subauthority: subauthority) end include WebServiceBase @@ -35,7 +36,7 @@ def find(id) end def find_url(id) - "https://id.loc.gov/authorities/#{@subauthority}/#{id}.json" + "https://id.loc.gov/#{@subauthority_type}/#{@subauthority}/#{id}.json" end private diff --git a/lib/qa/authorities/loc_subauthority.rb b/lib/qa/authorities/loc_subauthority.rb index 97e0c408..9a873fb8 100644 --- a/lib/qa/authorities/loc_subauthority.rb +++ b/lib/qa/authorities/loc_subauthority.rb @@ -1,4 +1,5 @@ module Qa::Authorities::LocSubauthority + # @todo Rename to reflect that this is a URI encoded url fragement used only for searching. def get_url_for_authority(authority) if authorities.include?(authority) then authority_base_url elsif vocabularies.include?(authority) then vocab_base_url diff --git a/spec/lib/authorities/loc_spec.rb b/spec/lib/authorities/loc_spec.rb index 147241ef..627de922 100644 --- a/spec/lib/authorities/loc_spec.rb +++ b/spec/lib/authorities/loc_spec.rb @@ -42,6 +42,18 @@ end end + describe ".type_for" do + it "raises an error for an invalid subauthority" do + expect do + described_class.type_for(subauthority: "no-one-would-ever-have-this-one") + end.to raise_error Qa::InvalidSubAuthority + end + + it "returns the corresponding type for the given subauthority" do + expect(described_class.type_for(subauthority: "graphicMaterials")).to eq("vocabulary") + end + end + describe "#response" do subject { authority.response(url) } let :authority do