Module: Europeana::Blacklight::Document::LangMaps
- Included in:
- Europeana::Blacklight::Document
- Defined in:
- app/models/europeana/blacklight/document/lang_maps.rb
Overview
Methods for working with “LangMap” data types in API JSON responses
Constant Summary collapse
- DEPRECATED_ISO_LANG_CODES =
%w(in iw jaw ji jw mo mol scc scr sh).freeze
- NON_ISO_LANG_CODES =
TODO:
Empty key acceptance is a workaround for malformed API data output; remove when fixed at source
Special keys API may return in a LangMap, not ISO codes
['def', ''].freeze
Instance Method Summary collapse
- #dereferenced_lang_map_value(value) ⇒ Object
- #known_lang_map_key?(key) ⇒ Boolean
- #lang_map?(obj) ⇒ Boolean
- #lang_map_value(lang_map, locale) ⇒ Object
- #localize_lang_map(lang_map) ⇒ Object
Instance Method Details
#dereferenced_lang_map_value(value) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'app/models/europeana/blacklight/document/lang_maps.rb', line 49 def dereferenced_lang_map_value(value) return nil if value.nil? if value.is_a?(Array) return value.map { |v| dereferenced_lang_map_value(v) } end return value unless value.is_a?(String) concept = root.fetch('concepts', []).detect { |c| c[:about] == value } if concept.present? && concept.key?(:prefLabel) localize_lang_map(concept[:prefLabel]) else return value end end |
#known_lang_map_key?(key) ⇒ Boolean
24 25 26 27 28 29 |
# File 'app/models/europeana/blacklight/document/lang_maps.rb', line 24 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 |
#lang_map?(obj) ⇒ Boolean
TODO:
Are three-letter language codes valid in EDM?
19 20 21 22 |
# File 'app/models/europeana/blacklight/document/lang_maps.rb', line 19 def lang_map?(obj) return false unless obj.is_a?(Hash) obj.keys.map(&:to_s).all? { |key| known_lang_map_key?(key) } end |
#lang_map_value(lang_map, locale) ⇒ Object
43 44 45 46 47 |
# File 'app/models/europeana/blacklight/document/lang_maps.rb', line 43 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 |
#localize_lang_map(lang_map) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 |
# File 'app/models/europeana/blacklight/document/lang_maps.rb', line 31 def localize_lang_map(lang_map) if lang_map.is_a?(Array) return lang_map.map { |l| localize_lang_map(l) } 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.values end |