-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #379 from samvera/jeremyf---extracting-logic-for-d…
…etermining-qa-authority Extracting `Qa.authority_for`
- Loading branch information
Showing
10 changed files
with
218 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
module Qa | ||
# @note THIS IS NOT TESTED NOR EXERCISED CODE IT IS PROVIDED AS CONJECTURE. FUTURE CHANGES MIGHT | ||
# BUILD AND REFACTOR UPON THIS. | ||
# | ||
# @api private | ||
# @abstract | ||
# | ||
# This class is responsible for exposing methods that are required by both linked data and | ||
# non-linked data authorities. As of v5.10.0, those three methods are: params, search_header, | ||
# fetch_header. Those are the methods that are used in {Qa::LinkedData::RequestHeaderService} and | ||
# in {Qa::Authorities::Discogs::GenericAuthority}. | ||
# | ||
# The intention is to provide a class that can behave like a controller object without being that | ||
# entire controller object. | ||
# | ||
# @see Qa::LinkedData::RequestHeaderService | ||
# @see Qa::Authorities::Discogs::GenericAuthority | ||
class AuthorityRequestContext | ||
def self.fallback | ||
new | ||
end | ||
|
||
def initialize(params: {}, headers: {}, **kwargs) | ||
@params = params | ||
@headers = headers | ||
(SEARCH_HEADER_KEYS + FETCH_HEADER_KEYS).uniq.each do |key| | ||
send("#{key}=", kwargs[key]) if kwargs.key?(key) | ||
end | ||
end | ||
|
||
SEARCH_HEADER_KEYS = %i[request request_id subauthority user_language performance_data context response_header replacements].freeze | ||
FETCH_HEADER_KEYS = %i[request request_id subauthority user_language performance_data format response_header replacements].freeze | ||
|
||
attr_accessor :params, :headers | ||
attr_accessor(*(SEARCH_HEADER_KEYS + FETCH_HEADER_KEYS).uniq) | ||
|
||
def search_header | ||
SEARCH_HEADER_KEYS.each_with_object(headers.deep_dup) do |key, header| | ||
header[key] = send(key) if send(key).present? | ||
end.compact | ||
end | ||
|
||
def fetch_header | ||
FETCH_HEADER_KEYS.each_with_object(headers.deep_dup) do |key, header| | ||
header[key] = send(key) if send(key).present? | ||
end.compact | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
module Qa | ||
# @api public | ||
# @since v5.11.0 | ||
# | ||
# The intention of this wrapper is to provide a common interface that both linked and non-linked | ||
# data can use. There are implementation differences between the two, but with this wrapper, the | ||
# goal is to draw attention to those differences and insulate the end user from those issues. | ||
# | ||
# One benefit in introducing this class is that when interacting with a questioning authority | ||
# implementation you don't need to consider "Hey when I instantiate an authority, is this linked | ||
# data or not?" And what specifically are the parameter differences. You will need to perhaps | ||
# include some additional values in the context if you don't call this from a controller. | ||
class AuthorityWrapper | ||
require 'qa/authority_request_context.rb' | ||
# @param authority [#find, #search] | ||
# @param subauthority [#to_s] | ||
# @param context [#params, #search_header, #fetch_header] | ||
def initialize(authority:, subauthority:, context:) | ||
@authority = authority | ||
@subauthority = subauthority | ||
@context = context | ||
configure! | ||
end | ||
attr_reader :authority, :context, :subauthority | ||
|
||
def search(value) | ||
if linked_data? | ||
# should respond to search_header | ||
authority.search(value, request_header: context.search_header) | ||
elsif authority.method(:search).arity == 2 | ||
# This context should respond to params; see lib/qa/authorities/discogs/generic_authority.rb | ||
authority.search(value, context) | ||
else | ||
authority.search(value) | ||
end | ||
end | ||
|
||
# context has params | ||
def find(value) | ||
if linked_data? | ||
# should respond to fetch_header | ||
authority.find(value, request_header: context.fetch_header) | ||
elsif authority.method(:find).arity == 2 | ||
authority.find(value, context) | ||
else | ||
authority.find(value) | ||
end | ||
end | ||
alias fetch find | ||
|
||
def method_missing(method_name, *arguments, &block) | ||
authority.send(method_name, *arguments, &block) | ||
end | ||
|
||
def respond_to_missing?(method_name, include_private = false) | ||
authority.respond_to?(method_name, include_private) | ||
end | ||
|
||
def configure! | ||
@context.subauthority = @subauthority if @context.respond_to?(:subauthority) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters