-
Notifications
You must be signed in to change notification settings - Fork 30
/
authority_url_service.rb
60 lines (54 loc) · 3.1 KB
/
authority_url_service.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# Provide service for constructing the external access URL for an authority.
module Qa
module LinkedData
class AuthorityUrlService
# Build a url for an authority/subauthority for the specified action.
# @param authority [Symbol] name of a registered authority
# @param subauthority [String] name of a subauthority
# @param action [Symbol] action with valid values :search or :term
# @param action_request [String] the request the user is making of the authority (e.g. query text or term id/uri)
# @param substitutions [Hash] variable-value pairs to substitute into the URL template
# @return a valid URL the submits the action request to the external authority
def self.build_url(action_config:, action:, action_request:, substitutions: {}, subauthority: nil)
action_validation(action)
url_config = Qa::IriTemplate::UrlConfig.new(action_url(action_config, action))
selected_substitutions = url_config.extract_substitutions(substitutions)
selected_substitutions[action_request_variable(action_config, action)] = action_request
selected_substitutions[action_subauth_variable(action_config, action)] = action_subauth_variable_value(action_config, subauthority, action) if subauthority.present?
Qa::IriTemplateService.build_url(url_config: url_config, substitutions: selected_substitutions)
end
def self.action_validation(action)
return if [:search, :term].include? action
raise Qa::UnsupportedAction, "#{action} Not Supported - Action must be one of the supported actions (e.g. :term, :search)"
end
private_class_method :action_validation
# TODO: elr - rename search and term config methods to be the same to avoid all the ternary checks
def self.action_url(auth_config, action)
action == :search ? auth_config.url : auth_config.term_url
end
private_class_method :action_url
def self.action_request_variable(action_config, action)
key = action == :search ? :query : :term_id
action == :search ? action_config.qa_replacement_patterns[key] : action_config.term_qa_replacement_patterns[key]
end
private_class_method :action_request_variable
def self.action_subauth_variable(action_config, action)
action == :search ? action_config.qa_replacement_patterns[:subauth] : action_config.term_qa_replacement_patterns[:subauth]
end
private_class_method :action_subauth_variable
def self.action_subauth_variable_value(action_config, subauthority, action)
case action
when :search
pattern = action_subauth_variable(action_config, action)
default = action_config.url_mappings[pattern.to_sym][:default]
action_config.subauthorities[subauthority.to_sym] || default
when :term
pattern = action_subauth_variable(action_config, action)
default = action_config.term_url_mappings[pattern.to_sym][:default]
action_config.term_subauthorities[subauthority.to_sym] || default
end
end
private_class_method :action_subauth_variable_value
end
end
end