Class: OAIUtils

Inherits:
Object
  • Object
show all
Defined in:
backend/app/lib/oai/mappers/oai_utils.rb

Class Method Summary collapse

Class Method Details

.display_string(json) ⇒ Object



67
68
69
# File 'backend/app/lib/oai/mappers/oai_utils.rb', line 67

def self.display_string(json)
  self.strip_mixed_content(json['display_string'] || json['title'])
end

.extract_published_note_content(note, toplevel = true) ⇒ Object



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
# File 'backend/app/lib/oai/mappers/oai_utils.rb', line 4

def self.extract_published_note_content(note, toplevel = true)
  if toplevel && !note['publish']
    return []
  end

  if note.is_a?(Hash)
    if note['publish']
      if note['jsonmodel_type'] == 'note_chronology'
        [
          strip_mixed_content(note['title']),
          ASUtils.wrap(note['items']).map {|item|
            [
              item['event_date'],
              ASUtils.wrap(item['events']).map {|e| strip_mixed_content(e)}.join(', ')
            ].compact.join(', ')
          }.join('; ')
        ].compact.join('. ')
      elsif note['jsonmodel_type'] == 'note_definedlist'
        [
          note['title'],
          ASUtils.wrap(note['items']).map {|item|
            [
              strip_mixed_content(item['label']),
              strip_mixed_content(item['value'])
            ].compact.join(': ')
          }.join('; ')
        ].compact.join('. ')
      elsif note['jsonmodel_type'] == 'note_orderedlist'
        [
          strip_mixed_content(note['title']),
          ASUtils.wrap(note['items']).map {|i| strip_mixed_content(i)}.join('; ')
        ].compact.join('. ')
      elsif note.has_key?('content')
        Array(note['content']).map {|content|
          strip_mixed_content(content)
        }
      else
        note.values.map {|value| extract_published_note_content(value, false)}.flatten
      end
    else
      []
    end
  elsif note.is_a?(Array)
    note.map {|value| extract_published_note_content(value, false)}.flatten
  else
    []
  end
end

.strip_mixed_content(s) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'backend/app/lib/oai/mappers/oai_utils.rb', line 54

def self.strip_mixed_content(s)
  return s if s.nil?

  # ANW-1082: For some reason, Nokogiri is grabbing stuff in <title> tags -- like <title>Groundhog Day</title> and moving it to the front of the string.
  # To fix this, manually strip out that tag and then carry on.

  s.gsub!(/<title.*?>/, "")
  s.gsub!('</title>', "")

  Nokogiri::HTML(s).text
end