-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsource.rb
More file actions
96 lines (75 loc) · 2.83 KB
/
source.rb
File metadata and controls
96 lines (75 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
require 'rest_client'
require 'typesafe_enum'
require 'berkeley_library/av/config'
require 'berkeley_library/av/constants'
require 'berkeley_library/av/record_not_found'
require 'berkeley_library/av/marc'
require 'berkeley_library/av/util'
require 'berkeley_library/av/metadata/readers'
module BerkeleyLibrary
module AV
class Metadata
class Source < TypesafeEnum::Base
include AV::Constants
new(:ALMA) { singleton_class.include(Readers::Alma) }
new(:TIND) { singleton_class.include(Readers::TIND) }
LINK_TEXT_ALMA = 'View library catalog record.'.freeze # TODO: is this right?
LINK_TEXT_TIND = 'View record in Digital Collections.'.freeze
class << self
def for_record_id(record_id)
id_type = RecordId.ensure_record_id(record_id).type
return ALMA if [RecordId::Type::ALMA, RecordId::Type::MILLENNIUM].include?(id_type)
TIND
end
end
def name
key.to_s
end
def catalog_link_text
return LINK_TEXT_ALMA if self == ALMA
LINK_TEXT_TIND if self == TIND
end
def display_uri_for(metadata)
record_id = canonical_record_id_for(metadata)
raise ArgumentError, "#{self}: unable to determine record ID from metadata #{metadata.inspect}" unless record_id
_display_uri_for(record_id)
end
def find_bib_number(metadata)
return alma_bib_number(metadata.marc_record) if self == Source::ALMA
tind_bib_number(metadata.marc_record) if self == Source::TIND
end
private
def canonical_record_id_for(metadata)
accessor = canonical_record_id_accessor
metadata.send(accessor) if metadata.respond_to?(accessor)
end
def canonical_record_id_accessor
return :alma_id if self == ALMA
:tind_id if self == TIND
end
def tind_bib_number(marc_record)
tag = TAG_TIND_CATALOG_ID
code = SUBFIELD_CODE_TIND_BIB_NUMBER
bib_from_marc(marc_record, tag, code)
end
def alma_bib_number(marc_record)
tag = TAG_ALMA_MIGRATION_INFO
code = SUBFIELD_CODE_ALMA_BIB_NUMBER
bib_from_marc(marc_record, tag, code)
end
def bib_from_marc(marc_record, tag, code)
return unless (bib_number = find_subfield_value(marc_record, tag, code))
return unless RecordId::Type.for_id(bib_number) == RecordId::Type::MILLENNIUM
RecordId.strip_check_digit(bib_number)
end
# TODO: Use marc/spec
def find_subfield_value(marc_record, tag, code)
marc_record.fields(tag).filter_map do |df|
subfield = df.find { |sf| sf.code == code }
subfield.value if subfield
end.first
end
end
end
end
end