Skip to content

Commit

Permalink
Extracting Qa.authority_for
Browse files Browse the repository at this point in the history
Prior to this commit, we used different initialization logic for linked
data authorities and non-linked data.  Further, the initialization logic
was locked away inside of a controller.

With this commit, we expose a method that provides consistent
initialization logic; albeit with different returned classes that have
slightly different implementation nuances.  The application can then
rely on that and we can share initialization logic across controllers
and also expose a method that allows non-controller initialization to
begin to follow the same logic pathway.

Where are the tests?  The initializations are covered by controller
tests, so I have chosen not to write new ones.

There is further work to do because LinkedData and non-LinkedData there
are different signatures for `find` and `search`.  Ideally, we would
normalize the public facing implementation logic.  However, this commit
preserves backwards compatibility, simply introducing a new feature.
  • Loading branch information
jeremyf committed Dec 7, 2022
1 parent 14d233a commit 24776bc
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 22 deletions.
4 changes: 2 additions & 2 deletions app/controllers/qa/linked_data_terms_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ def create_request_header_service
end

def init_authority
@authority = Qa::Authorities::LinkedData::GenericAuthority.new(vocab_param)
rescue Qa::InvalidLinkedDataAuthority => e
@authority = Qa.authority_for(vocab: params[:vocab], subauthority: params[:subauthority])
rescue Qa::InvalidAuthorityError, Qa::InvalidLinkedDataAuthority => e
msg = e.message
logger.warn msg
render json: { errors: msg }, status: :bad_request
Expand Down
28 changes: 8 additions & 20 deletions app/controllers/qa/terms_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,26 +69,14 @@ def check_vocab_param
end

def init_authority # rubocop:disable Metrics/MethodLength
begin
mod = authority_class.camelize.constantize
rescue NameError
msg = "Unable to initialize authority #{authority_class}"
logger.warn msg
render json: { errors: msg }, status: :bad_request
return
end
begin
@authority = if mod.is_a? Class
mod.new
else
raise Qa::MissingSubAuthority, "No sub-authority provided" if params[:subauthority].blank?
mod.subauthority_for(params[:subauthority])
end
rescue Qa::InvalidSubAuthority, Qa::MissingSubAuthority => e
msg = e.message
logger.warn msg
render json: { errors: msg }, status: :bad_request
end
@authority = Qa.authority_for(vocab: params[:vocab],
subauthority: params[:subauthority],
# Included to preserve error message text
try_linked_data_config: false)
rescue Qa::InvalidAuthorityError, Qa::InvalidSubAuthority, Qa::MissingSubAuthority => e
msg = e.message
logger.warn msg
render json: { errors: msg }, status: :bad_request
end

def check_query_param
Expand Down
42 changes: 42 additions & 0 deletions lib/qa.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ def self.deprecation_warning(in_msg: nil, msg:)
warn "[DEPRECATED] #{in_msg}#{msg} It will be removed in the next major release."
end

# Raised when the authority is not valid
class InvalidAuthorityError < RuntimeError
def initialize(authority_class)
super "Unable to initialize authority #{authority_class}"
end
end

# Raised when the configuration directory for local authorities doesn't exist
class ConfigDirectoryNotFound < StandardError; end

Expand Down Expand Up @@ -67,4 +74,39 @@ class MissingParameter < StandardError; end

# Raised when data is returned but cannot be normalized
class DataNormalizationError < StandardError; end

# @api public
# @since 5.11.0
#
# @param vocab [String]
# @param subauthority [String]
#
# @param try_linked_data_config [Boolean] when true attempt to check for a linked data authority;
# this is included as an option to help preserve error messaging from the 5.10.0 branch.
# Unless you want to mirror the error messages of `Qa::TermsController#init_authority` then
# use the default value.
#
# @note :try_linked_data_config is included to preserve error message text; something which is
# extensively tested in this gem.
#
# @return [#search, #find] an authority that will respond to #search and #find; and in some cases
# #fetch. This is provided as a means of normalizing how we initialize an authority.
# And to provide a means to request an authority both within a controller request cycle as
# well as outside of that cycle.
def self.authority_for(vocab:, subauthority: nil, try_linked_data_config: true)
authority_constant_name = "Qa::Authorities::#{vocab.to_s.camelcase}"
authority_constant = authority_constant_name.safe_constantize
if authority_constant.nil?
if try_linked_data_config
return Qa::Authorities::LinkedData::GenericAuthority.new(vocab.upcase.to_sym)
else
raise InvalidAuthorityError, authority_constant_name
end
end

return authority_constant.new if authority_constant.is_a?(Class)
return authority_constant.subauthority_for(subauthority) if subauthority.present?

raise Qa::MissingSubAuthority, "No sub-authority provided"
end
end

0 comments on commit 24776bc

Please sign in to comment.