module Europeana::API::Record::LangMap
Methods for working with “LangMap” data types in Record
API
JSON responses
Constants
Public Class Methods
Is the argument a known language map key?
@param key [String] String to check @return [Boolean]
# File lib/europeana/api/record/lang_map.rb, line 39 def known_lang_map_key?(key) key = key.dup.downcase DEPRECATED_ISO_LANG_CODES.include?(key) || NON_ISO_LANG_CODES.include?(key) || !ISO_639.find(key.split('-').first).nil? end
Is the argument a language map?
Critera:
-
Must be a
Hash
. -
Must have only keys which are known language codes
@param candidate [Object] Object to check @return [Boolean]
# File lib/europeana/api/record/lang_map.rb, line 30 def lang_map?(candidate) return false unless candidate.is_a?(Hash) candidate.keys.map(&:to_s).all? { |key| known_lang_map_key?(key) } end
Fetch the language map value for a given locale
@param lang_map [Hash] Verified language map @param locale [String] Locale to fetch the value for @return [Array<Object>,nil] Array of value(s) for the given locale,
or nil if none are present
# File lib/europeana/api/record/lang_map.rb, line 78 def lang_map_value(lang_map, locale) keys = salient_lang_map_keys(lang_map, locale) return nil unless keys.present? keys.map { |k| lang_map[k] }.flatten.uniq end
Localise a language map
-
If the argument is not a language map, return as-is
-
If the language map has a key for the current locale, return that value
-
If the language map has a key for the default locale, return that value
-
Else, return all values
Arrays will be recursed over, localising each element.
@param lang_map [Object] Object to attempt to localise @return [Object,Array<Object>] Localised value
# File lib/europeana/api/record/lang_map.rb, line 59 def localise_lang_map(lang_map) if lang_map.is_a?(Array) return lang_map.map { |val| localise_lang_map(val) } end return lang_map unless lang_map?(lang_map) lang_map_value(lang_map, ::I18n.locale.to_s) || lang_map_value(lang_map, ::I18n.default_locale.to_s) || lang_map_value(lang_map, 'def') || lang_map.values end
Protected Class Methods
@return [Array<String>] valid keys in the language map for the given locale
# File lib/europeana/api/record/lang_map.rb, line 87 def salient_lang_map_keys(lang_map, locale) iso_code = locale.split('-').first iso_locale = ISO_639.find(iso_code) # Favour exact matches keys = lang_map.keys.select do |k| [locale, iso_locale&.alpha2, iso_locale&.alpha3].include?(k) end.flatten.compact return keys unless keys.blank? # Any sub-code will do lang_map.keys.select do |k| k =~ %r{\A#{iso_code}-} end.flatten.compact end