europeana-blacklight 0.4.2 → 0.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 332413f00fbe66dc175eb213d174b4e3aa7f67b0
4
- data.tar.gz: c5a1b0b0ae51b4ee350289659cb8c6978c455984
3
+ metadata.gz: d3b33734136e3321b7c3bad989be93690a479f98
4
+ data.tar.gz: 9a074bd2838c7eebfb1e2a6aca9f9191fc98d9c0
5
5
  SHA512:
6
- metadata.gz: ba0200e294ce97524fc25d645bfc88fe96c1d4b6c01ffcbafed8d3758d7bf1d02cfdd6eb1fcc672505750e90bb8f051400b2237e65435b7b7655c3a2a3f1df1f
7
- data.tar.gz: dc63f5f6b57840fa6609c96c86aa2d1721907ed9b7a681b440c7a227a3d823ffb2746b246276c379545f8257a9c7cb630b8d7ab0d555f8c15b2b3a62af2c10be
6
+ metadata.gz: bb07e72f2e73a96b03efa331cdd5262a3074eae8afa9d6ff83ba1d3238e28e40fd959565e6d555317d2892c1873bdcf86fced824d3c72e0300b9e9f9a8eb6621
7
+ data.tar.gz: 0d5fc80c5518a447e195b19f01fee849d7bb6faac3df87956e1c8feaad5654e8f17e52f81edcde9576f88e398838a202049b14622452d7e4065df3bad226a55b
@@ -13,6 +13,7 @@ module Europeana
13
13
  include MoreLikeThis
14
14
  include Relations
15
15
 
16
+ attr_reader :root
16
17
  attr_writer :provider_id, :record_id
17
18
 
18
19
  class << self
@@ -23,8 +24,9 @@ module Europeana
23
24
  end
24
25
  end
25
26
 
26
- def initialize(source_doc = {}, response = nil)
27
+ def initialize(source_doc = {}, response = nil, root = self)
27
28
  fields, @relations = extract_relations(source_doc)
29
+ @root = root
28
30
  super(fields, response)
29
31
  end
30
32
 
@@ -41,7 +43,7 @@ module Europeana
41
43
  else
42
44
  super
43
45
  end
44
- Document.localize_lang_map(value)
46
+ localize_lang_map(value)
45
47
  end
46
48
 
47
49
  def respond_to?(*args)
@@ -5,8 +5,6 @@ module Europeana
5
5
  # Methods for working with "LangMap" data types in API JSON responses
6
6
  # @see http://labs.europeana.eu/api/getting-started#datatypes
7
7
  module LangMaps
8
- extend ActiveSupport::Concern
9
-
10
8
  # @see https://www.loc.gov/standards/iso639-2/php/code_changes.php
11
9
  DEPRECATED_ISO_LANG_CODES = %w(in iw jaw ji jw mo mol scc scr sh)
12
10
 
@@ -15,59 +13,72 @@ module Europeana
15
13
  # output; remove when fixed at source
16
14
  NON_ISO_LANG_CODES = ['def', '']
17
15
 
18
- class_methods do
19
- # @todo Are three-letter language codes valid in EDM?
20
- def lang_map?(obj)
21
- return false unless obj.is_a?(Hash)
22
- obj.keys.map(&:to_s).all? { |key| known_lang_map_key?(key) }
23
- end
16
+ # @todo Are three-letter language codes valid in EDM?
17
+ def lang_map?(obj)
18
+ return false unless obj.is_a?(Hash)
19
+ obj.keys.map(&:to_s).all? { |key| known_lang_map_key?(key) }
20
+ end
24
21
 
25
- def known_lang_map_key?(key)
26
- key = key.dup.downcase
27
- DEPRECATED_ISO_LANG_CODES.include?(key) ||
28
- NON_ISO_LANG_CODES.include?(key) ||
29
- (!ISO_639.find(key.split('-').first).nil?)
22
+ def known_lang_map_key?(key)
23
+ key = key.dup.downcase
24
+ DEPRECATED_ISO_LANG_CODES.include?(key) ||
25
+ NON_ISO_LANG_CODES.include?(key) ||
26
+ !ISO_639.find(key.split('-').first).nil?
27
+ end
28
+
29
+ def localize_lang_map(lang_map)
30
+ if lang_map.is_a?(Array)
31
+ return lang_map.map { |l| localize_lang_map(l) }
30
32
  end
31
33
 
32
- def localize_lang_map(lang_map)
33
- if lang_map.is_a?(Array)
34
- return lang_map.map { |l| localize_lang_map(l) }
35
- end
34
+ return lang_map unless lang_map?(lang_map)
36
35
 
37
- return lang_map unless lang_map?(lang_map)
36
+ lang_map_value(lang_map, I18n.locale.to_s) ||
37
+ lang_map_value(lang_map, I18n.default_locale.to_s) ||
38
+ dereferenced_lang_map_value(lang_map[:def]) ||
39
+ lang_map.values
40
+ end
38
41
 
39
- lang_map_value(lang_map, I18n.locale.to_s) ||
40
- lang_map_value(lang_map, I18n.default_locale.to_s) ||
41
- lang_map[:def] ||
42
- lang_map.values
42
+ def lang_map_value(lang_map, locale)
43
+ keys = salient_lang_map_keys(lang_map, locale)
44
+ return nil unless keys.present?
45
+ keys.map { |k| lang_map[k] }.flatten.uniq
46
+ end
47
+
48
+ def dereferenced_lang_map_value(value)
49
+ return nil if value.nil?
50
+
51
+ if value.is_a?(Array)
52
+ return value.map { |v| dereferenced_lang_map_value(v) }
43
53
  end
44
54
 
45
- def lang_map_value(lang_map, locale)
46
- keys = salient_lang_map_keys(lang_map, locale)
47
- return nil unless keys.present?
48
- keys.map { |k| lang_map[k] }.flatten.uniq
55
+ return value unless value.is_a?(String)
56
+
57
+ concept = root.fetch('concepts', []).detect { |c| c[:about] == value }
58
+ if concept.present? && concept.key?(:prefLabel)
59
+ localize_lang_map(concept[:prefLabel])
60
+ else
61
+ return value
49
62
  end
63
+ end
50
64
 
51
- protected
65
+ protected
52
66
 
53
- def salient_lang_map_keys(lang_map, locale)
54
- iso_code = locale.split('-').first
55
- iso_locale = ISO_639.find(iso_code)
67
+ def salient_lang_map_keys(lang_map, locale)
68
+ iso_code = locale.split('-').first
69
+ iso_locale = ISO_639.find(iso_code)
56
70
 
57
- # Favour exact matches
58
- keys = lang_map.keys.select do |k|
59
- [locale, iso_locale.alpha2, iso_locale.alpha3].include?(k)
60
- end.flatten.compact
61
- return keys unless keys.blank?
71
+ # Favour exact matches
72
+ keys = lang_map.keys.select do |k|
73
+ [locale, iso_locale.alpha2, iso_locale.alpha3].include?(k)
74
+ end.flatten.compact
75
+ return keys unless keys.blank?
62
76
 
63
- # Any sub-code will do
64
- lang_map.keys.select do |k|
65
- k =~ %r{\A#{iso_code}-}
66
- end.flatten.compact
67
- end
77
+ # Any sub-code will do
78
+ lang_map.keys.select do |k|
79
+ k =~ %r{\A#{iso_code}-}
80
+ end.flatten.compact
68
81
  end
69
-
70
- delegate :lang_map?, :localize_lang_map, to: :class
71
82
  end
72
83
  end
73
84
  end
@@ -24,9 +24,9 @@ module Europeana
24
24
  relation_keys.each do |k|
25
25
  if source_doc.key?(k)
26
26
  if source_doc[k].is_a?(Hash)
27
- relations[k] = self.class.new(source_doc[k], nil)
27
+ relations[k] = self.class.new(source_doc[k], nil, self)
28
28
  elsif source_doc[k].is_a?(Array)
29
- relations[k] = source_doc[k].map { |v| self.class.new(v, nil) }
29
+ relations[k] = source_doc[k].map { |v| self.class.new(v, nil, self) }
30
30
  else
31
31
  fail StandardError,
32
32
  'Relations should be a collection of objects.'
@@ -29,7 +29,7 @@ module Europeana
29
29
  end
30
30
 
31
31
  def get_field_values(field, field_config, options = {})
32
- Document.localize_lang_map(super)
32
+ @document.localize_lang_map(super)
33
33
  end
34
34
  end
35
35
  end
@@ -1,5 +1,5 @@
1
1
  module Europeana
2
2
  module Blacklight
3
- VERSION = '0.4.2'.freeze
3
+ VERSION = '0.4.3'.freeze
4
4
  end
5
5
  end
@@ -51,11 +51,11 @@ RSpec.describe Europeana::Blacklight::Document do
51
51
  it { is_expected.to eq('Document') }
52
52
  end
53
53
 
54
- describe '.lang_map?' do
54
+ describe '#lang_map?' do
55
55
  context 'when not a Hash' do
56
56
  it 'returns false' do
57
57
  [nil, 0, 1, 'xyz', Array].each do |arg|
58
- expect(described_class.lang_map?(arg)).to eq(false)
58
+ expect(subject.lang_map?(arg)).to eq(false)
59
59
  end
60
60
  end
61
61
  end
@@ -63,20 +63,17 @@ RSpec.describe Europeana::Blacklight::Document do
63
63
  context 'when a Hash' do
64
64
  context 'when full EDM' do
65
65
  it 'returns false' do
66
- expect(described_class.lang_map?(edm)).to eq(false)
66
+ expect(subject.lang_map?(edm)).to eq(false)
67
67
  end
68
68
  end
69
69
  context 'when a record relation' do
70
70
  it 'returns false' do
71
- expect(described_class.lang_map?(edm[:proxies].first)).to eq(false)
71
+ expect(subject.lang_map?(edm[:proxies].first)).to eq(false)
72
72
  end
73
73
  end
74
74
  end
75
75
  end
76
76
 
77
- it { is_expected.to delegate_method(:lang_map?).to(:class) }
78
- it { is_expected.to delegate_method(:localize_lang_map).to(:class) }
79
-
80
77
  describe '#provider_id' do
81
78
  it 'returns first part of ID' do
82
79
  expect(subject.provider_id).to eq('abc')
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: europeana-blacklight
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Doe
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-30 00:00:00.000000000 Z
11
+ date: 2016-05-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: blacklight
@@ -312,4 +312,3 @@ test_files:
312
312
  - spec/models/europeana/blacklight/document_spec.rb
313
313
  - spec/presenters/europeana/blacklight/document_presenter_spec.rb
314
314
  - spec/spec_helper.rb
315
- has_rdoc: