Skip to content

Commit

Permalink
update language in request header to use preferred language
Browse files Browse the repository at this point in the history
  • Loading branch information
elrayle committed Nov 6, 2019
1 parent 102e479 commit 709be1c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 22 deletions.
4 changes: 2 additions & 2 deletions app/services/qa/linked_data/authority_url_service.rb
Expand Up @@ -10,7 +10,7 @@ class << self
# @param request_header [Hash] optional attributes that can be appended to the generated URL
# @option replacements [Hash] variable-value pairs to substitute into the URL template (optional)
# @option subauthority [String] name of a subauthority (optional)
# @option language [Array<Symbol>] languages for filtering returned literals (optional)
# @option preferred_language [Array<Symbol>] languages for filtering returned literals (optional)
# @return a valid URL that submits the action request to the external authority
# @note All parameters after request_header are deprecated and will be removed in the next major release.
def build_url(action_config:, action:, action_request:, request_header: {}, substitutions: {}, subauthority: nil, language: nil) # rubocop:disable Metrics/ParameterLists
Expand Down Expand Up @@ -58,7 +58,7 @@ def action_language_variable(action_config)

def language_value(action_config, request_header)
return nil unless action_config.supports_language_parameter?
request_header.fetch(:language, []).first
request_header.fetch(:preferred_language, []).first
end

def build_request_header(substitutions, subauthority, language) # rubocop:disable Metrics/CyclomaticComplexity
Expand Down
15 changes: 10 additions & 5 deletions app/services/qa/linked_data/request_header_service.rb
Expand Up @@ -6,12 +6,12 @@ class RequestHeaderService

# @param request [HttpRequest] request from controller
# @param params [Hash] attribute-value pairs holding the request parameters
# @option language [Symbol] language used to select literals when multi-language is supported (e.g. :en, :fr, etc.)
# @option replacements [Hash] replacement values with { pattern_name (defined in YAML config) => value }
# @option subauth [String] the subauthority to query
# @option subauthority [String] the subauthority to query
# @option lang [Symbol] language used to select literals when multi-language is supported (e.g. :en, :fr, etc.)
# @option performance_data [Boolean] true if include_performance_data should be returned with the results; otherwise, false (default: false)
# @option context [Boolean] true if context should be returned with the results; otherwise, false (default: false) (search only)
# @option format [String] return data in this format (fetch only)
# @note params may have additional attribute-value pairs that are passed through via replacements (only configured replacements are used)
def initialize(request, params)
@request = request
@params = params
Expand All @@ -24,8 +24,8 @@ def search_header
header = {}
header[:subauthority] = params.fetch(:subauthority, nil)
header[:language] = language
header[:context] = context?
header[:performance_data] = performance_data?
header[:context] = context?
header[:replacements] = replacements
header
end
Expand All @@ -37,8 +37,8 @@ def fetch_header
header = {}
header[:subauthority] = params.fetch(:subauthority, nil)
header[:language] = language
header[:format] = format
header[:performance_data] = performance_data?
header[:format] = format
header[:replacements] = replacements
header
end
Expand All @@ -59,30 +59,35 @@ def content_type_for_format

private

# filter literals in results to this language
def language
request_language = request.env['HTTP_ACCEPT_LANGUAGE']
request_language = request_language.scan(/^[a-z]{2}/).first if request_language.present?
lang = params[:lang] || request_language
lang.present? ? Array(lang) : nil
end

# include extended context in the results if true (applies to search only)
def context?
context = params.fetch(:context, 'false')
context.casecmp?('true')
end

# include performance data in the results if true
def performance_data?
performance_data = params.fetch(:performance_data, 'false')
performance_data.casecmp?('true')
end

# any params not specifically handled are passed through via replacements
def replacements
params.reject do |k, _v|
['q', 'vocab', 'controller', 'action', 'subauthority', 'lang', 'id',
'context', 'performance_data', 'response_header', 'format'].include?(k)
end
end

# results are returned in the format (applies to fetch only)
def format
f = params.fetch(:format, 'json').downcase
['jsonld', 'n3', 'ntriples'].include?(f) ? f : 'json'
Expand Down
20 changes: 13 additions & 7 deletions lib/qa/authorities/linked_data/find_term.rb
Expand Up @@ -15,8 +15,8 @@ def initialize(term_config)
@term_config = term_config
end

attr_reader :term_config, :full_graph, :filtered_graph, :language, :id, :uri, :access_time_s, :normalize_time_s, :fetched_size, :normalized_size
private :full_graph, :filtered_graph, :language, :id, :uri, :access_time_s, :normalize_time_s, :fetched_size, :normalized_size
attr_reader :term_config, :full_graph, :filtered_graph, :language, :id, :uri, :access_time_s, :normalize_time_s, :fetched_size, :normalized_size, :subauthority
private :full_graph, :filtered_graph, :language, :id, :uri, :access_time_s, :normalize_time_s, :fetched_size, :normalized_size, :subauthority

delegate :term_subauthority?, :prefixes, :authority_name, to: :term_config

Expand Down Expand Up @@ -45,12 +45,9 @@ def initialize(term_config)
# "http://schema.org/sameAs":["http://id.loc.gov/authorities/names/n79021621","https://viaf.org/viaf/126293486"] } }
def find(id, request_header: {}, language: nil, replacements: {}, subauth: nil, format: 'json', performance_data: false) # rubocop:disable Metrics/ParameterLists
request_header = build_request_header(language: language, replacements: replacements, subauth: subauth, format: format, performance_data: performance_data) if request_header.empty?
subauth = request_header.fetch(:subauthority, nil)
raise Qa::InvalidLinkedDataAuthority, "Unable to initialize linked data term sub-authority #{subauth}" unless subauth.nil? || term_subauthority?(subauth)
@language = language_service.preferred_language(user_language: request_header.fetch(:language, nil), authority_language: term_config.term_language)
unpack_request_header(request_header)
raise Qa::InvalidLinkedDataAuthority, "Unable to initialize linked data term sub-authority #{subauthority}" unless subauthority.nil? || term_subauthority?(subauthority)
@id = id
@performance_data = request_header.fetch(:performance_data, false)
@format = request_header.fetch(:format, 'json')
url = authority_service.build_url(action_config: term_config, action: :term, action_request: normalize_id, request_header: request_header)
Rails.logger.info "QA Linked Data term url: #{url}"
load_graph(url: url)
Expand Down Expand Up @@ -95,6 +92,15 @@ def perform_normalization
convert_results_to_json(results)
end

def unpack_request_header(request_header)
@subauthority = request_header.fetch(:subauthority, nil)
@format = request_header.fetch(:format, 'json')
@performance_data = request_header.fetch(:performance_data, false)
@language = language_service.preferred_language(user_language: request_header.fetch(:language, nil),
authority_language: term_config.term_language)
request_header[:preferred_language] = Array(@language)
end

def filter_graph
@filtered_graph = graph_service.deep_copy(graph: @full_graph)
@filtered_graph = graph_service.filter(graph: @filtered_graph, language: language) unless language.blank?
Expand Down
22 changes: 14 additions & 8 deletions lib/qa/authorities/linked_data/search_query.rb
Expand Up @@ -15,8 +15,8 @@ def initialize(search_config)
@search_config = search_config
end

attr_reader :search_config, :full_graph, :filtered_graph, :language, :access_time_s, :normalize_time_s, :fetched_size, :normalized_size
private :full_graph, :filtered_graph, :language, :access_time_s, :normalize_time_s, :fetched_size, :normalized_size
attr_reader :search_config, :full_graph, :filtered_graph, :language, :access_time_s, :normalize_time_s, :fetched_size, :normalized_size, :subauthority
private :full_graph, :filtered_graph, :language, :access_time_s, :normalize_time_s, :fetched_size, :normalized_size, :subauthority

delegate :subauthority?, :supports_sort?, :prefixes, :authority_name, to: :search_config

Expand All @@ -36,11 +36,8 @@ def initialize(search_config)
# {"uri":"http://id.worldcat.org/fast/409667","id":"409667","label":"Cornell, Ezra, 1807-1874"} ]
def search(query, request_header: {}, language: nil, replacements: {}, subauth: nil, context: false, performance_data: false) # rubocop:disable Metrics/ParameterLists
request_header = build_request_header(language: language, replacements: replacements, subauth: subauth, context: context, performance_data: performance_data) if request_header.empty?
subauth = request_header.fetch(:subauthority, nil)
raise Qa::InvalidLinkedDataAuthority, "Unable to initialize linked data search sub-authority #{subauth}" unless subauth.nil? || subauthority?(subauth)
@context = request_header.fetch(:context, false)
@performance_data = request_header.fetch(:performance_data, false)
@language = language_service.preferred_language(user_language: request_header.fetch(:language, nil), authority_language: search_config.language)
unpack_request_header(request_header)
raise Qa::InvalidLinkedDataAuthority, "Unable to initialize linked data search sub-authority #{subauthority}" unless subauthority.nil? || subauthority?(subauthority)
url = authority_service.build_url(action_config: search_config, action: :search, action_request: query, request_header: request_header)
Rails.logger.info "QA Linked Data search url: #{url}"
load_graph(url: url)
Expand Down Expand Up @@ -91,7 +88,16 @@ def map_results

results_mapper_service.map_values(graph: filtered_graph, prefixes: prefixes, ldpath_map: ldpath_map,
predicate_map: predicate_map, sort_key: :sort,
preferred_language: @language, context_map: context_map)
preferred_language: language, context_map: context_map)
end

def unpack_request_header(request_header)
@subauthority = request_header.fetch(:subauthority, nil)
@context = request_header.fetch(:context, false)
@performance_data = request_header.fetch(:performance_data, false)
@language = language_service.preferred_language(user_language: request_header.fetch(:language, nil),
authority_language: search_config.language)
request_header[:preferred_language] = Array(@language)
end

def context_map
Expand Down

0 comments on commit 709be1c

Please sign in to comment.