|
| 1 | +# Transforms a TIMDEX result into a normalized record. |
| 2 | +class NormalizeTimdexRecord |
| 3 | + def initialize(record, query) |
| 4 | + @record = record |
| 5 | + @query = query |
| 6 | + end |
| 7 | + |
| 8 | + def normalize |
| 9 | + { |
| 10 | + # Core fields |
| 11 | + 'title' => title, |
| 12 | + 'creators' => creators, |
| 13 | + 'source' => source, |
| 14 | + 'year' => year, |
| 15 | + 'format' => format, |
| 16 | + 'links' => links, |
| 17 | + 'citation' => citation, |
| 18 | + 'identifier' => identifier, |
| 19 | + 'summary' => summary, |
| 20 | + 'publisher' => publisher, |
| 21 | + 'location' => location, |
| 22 | + 'subjects' => subjects, |
| 23 | + # TIMDEX-specific fields |
| 24 | + 'content_type' => content_type, |
| 25 | + 'dates' => dates, |
| 26 | + 'contributors' => contributors, |
| 27 | + 'highlight' => highlight, |
| 28 | + 'source_link' => source_link |
| 29 | + } |
| 30 | + end |
| 31 | + |
| 32 | + private |
| 33 | + |
| 34 | + def title |
| 35 | + @record['title'] || 'Unknown title' |
| 36 | + end |
| 37 | + |
| 38 | + def creators |
| 39 | + return [] unless @record['contributors'] |
| 40 | + |
| 41 | + # Convert TIMDEX contributors to Primo-style creators format |
| 42 | + @record['contributors'] |
| 43 | + .select { |c| %w[Creator Author].include?(c['kind']) } |
| 44 | + .map { |creator| { 'value' => creator['value'], 'link' => nil } } |
| 45 | + end |
| 46 | + |
| 47 | + def source |
| 48 | + return 'Unknown source' unless @record['source'] |
| 49 | + |
| 50 | + @record['source'] |
| 51 | + end |
| 52 | + |
| 53 | + def year |
| 54 | + # Extract year from dates |
| 55 | + return nil unless @record['dates'] |
| 56 | + |
| 57 | + pub_date = @record['dates'].find { |date| date['kind'] == 'Publication date' } |
| 58 | + return pub_date['value']&.match(/\d{4}/)&.to_s if pub_date |
| 59 | + |
| 60 | + # Fallback to any date with a year |
| 61 | + @record['dates'].each do |date| |
| 62 | + year_match = date['value']&.match(/\d{4}/) |
| 63 | + return year_match.to_s if year_match |
| 64 | + end |
| 65 | + end |
| 66 | + |
| 67 | + def format |
| 68 | + return '' unless @record['contentType'] |
| 69 | + |
| 70 | + @record['contentType'].map { |type| type['value'] }.join(' ; ') |
| 71 | + end |
| 72 | + |
| 73 | + def links |
| 74 | + links = [] |
| 75 | + |
| 76 | + # Add source link if available |
| 77 | + if @record['sourceLink'] |
| 78 | + links << { |
| 79 | + 'kind' => 'full record', |
| 80 | + 'url' => @record['sourceLink'], |
| 81 | + 'text' => 'View full record' |
| 82 | + } |
| 83 | + end |
| 84 | + |
| 85 | + links |
| 86 | + end |
| 87 | + |
| 88 | + def citation |
| 89 | + @record['citation'] || nil |
| 90 | + end |
| 91 | + |
| 92 | + def summary |
| 93 | + return nil unless @record['summary'] |
| 94 | + |
| 95 | + @record['summary'].is_a?(Array) ? @record['summary'].join(' ') : @record['summary'] |
| 96 | + end |
| 97 | + |
| 98 | + def publisher |
| 99 | + # Extract from contributors or other fields |
| 100 | + return nil unless @record['contributors'] |
| 101 | + |
| 102 | + publisher = @record['contributors'].find { |c| c['kind'] == 'Publisher' } |
| 103 | + publisher&.dig('value') |
| 104 | + end |
| 105 | + |
| 106 | + def location |
| 107 | + return nil unless @record['locations'] |
| 108 | + |
| 109 | + @record['locations'].map { |loc| loc['value'] }.compact.join('; ') |
| 110 | + end |
| 111 | + |
| 112 | + def subjects |
| 113 | + return [] unless @record['subjects'] |
| 114 | + |
| 115 | + @record['subjects'].map { |subject| subject['value'] } |
| 116 | + end |
| 117 | + |
| 118 | + def identifier |
| 119 | + @record['timdexRecordId'] |
| 120 | + end |
| 121 | + |
| 122 | + # TIMDEX-specific methods |
| 123 | + def content_type |
| 124 | + @record['contentType'] |
| 125 | + end |
| 126 | + |
| 127 | + def dates |
| 128 | + @record['dates'] |
| 129 | + end |
| 130 | + |
| 131 | + def contributors |
| 132 | + @record['contributors'] |
| 133 | + end |
| 134 | + |
| 135 | + def highlight |
| 136 | + @record['highlight'] |
| 137 | + end |
| 138 | + |
| 139 | + def source_link |
| 140 | + @record['sourceLink'] |
| 141 | + end |
| 142 | +end |
0 commit comments