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 +4 -4
- data/app/models/europeana/blacklight/document.rb +4 -2
- data/app/models/europeana/blacklight/document/lang_maps.rb +53 -42
- data/app/models/europeana/blacklight/document/relations.rb +2 -2
- data/app/presenters/europeana/blacklight/document_presenter.rb +1 -1
- data/lib/europeana/blacklight/version.rb +1 -1
- data/spec/models/europeana/blacklight/document_spec.rb +4 -7
- metadata +2 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d3b33734136e3321b7c3bad989be93690a479f98
|
4
|
+
data.tar.gz: 9a074bd2838c7eebfb1e2a6aca9f9191fc98d9c0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
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
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
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
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
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
|
-
|
65
|
+
protected
|
52
66
|
|
53
|
-
|
54
|
-
|
55
|
-
|
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
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
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
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
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.'
|
@@ -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 '
|
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(
|
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(
|
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(
|
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.
|
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-
|
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:
|