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.
  • Loading branch information
jeremyf committed Dec 9, 2022
1 parent d3c51a5 commit e962cf8
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
12 changes: 12 additions & 0 deletions lib/qa/authorities/loc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 @@ -4,6 +4,7 @@ class Loc::GenericAuthority < Base
def initialize(subauthority)
super()
@subauthority = subauthority
@subauthority_type = Loc.type_for(subauthority: subauthority)
end

include WebServiceBase
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 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 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 ".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
Expand Down

0 comments on commit e962cf8

Please sign in to comment.