Skip to content

Commit

Permalink
Adding a Qa::Authorities::Loc.type_for
Browse files Browse the repository at this point in the history
Prior to this commit, we always assumed that each "find" would request
from `https://id.loc.gov/authorities`.

With this commit, we use the "subauthority" to inform what the base URL
is for the `https://id.loc.gov/` host.  In some cases this is
`authorities` and in others it's as the code indicates.

Hopefully works to close the following:

- notch8/utk-hyku#267
  • Loading branch information
jeremyf committed Dec 9, 2022
1 parent d3c51a5 commit 79a13f9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/qa/authorities/loc/generic_authority.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ def find(id)
end

def find_url(id)
"https://id.loc.gov/authorities/#{@subauthority}/#{id}.json"
root_fetch_slug = Loc.root_fetch_slug_for(@subauthority)
File.join("https://id.loc.gov/", root_fetch_slug, "/#{@subauthority}/#{id}.json")
end

private
Expand Down
18 changes: 18 additions & 0 deletions lib/qa/authorities/loc_subauthority.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -7,6 +8,23 @@ def get_url_for_authority(authority)
end
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.
#
# @param authority [String] the LOC authority that matches one of the types
# @return [String]
#
# @note there is a relationship between the returned value and the encoded URLs returned by
# {#get_url_for_authority}.
def root_fetch_slug_for(authority)
validate_subauthority!(authority)
return "authorities" if authorities.include?(authority)
return "vocabulary" if vocabularies.include?(authority)
return "datatype" if datatypes.include?(authority)
return "vocabulary/preservation" if preservation.include?(authority)
end

def authorities
[
"subjects",
Expand Down
12 changes: 12 additions & 0 deletions spec/lib/authorities/loc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@
end
end

describe ".root_fetch_slug_for" do
it "raises an error for an invalid subauthority" do
expect do
described_class.root_fetch_slug_for("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.root_fetch_slug_for("graphicMaterials")).to eq("vocabulary")
end
end

describe "#response" do
subject { authority.response(url) }
let :authority do
Expand Down

0 comments on commit 79a13f9

Please sign in to comment.