Class: FHIR::Boot::Preprocess

Inherits:
Object
  • Object
show all
Defined in:
lib/fhir_models/bootstrap/preprocess.rb

Class Method Summary collapse

Class Method Details

.pre_process_bundle(filename) ⇒ Object



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
# File 'lib/fhir_models/bootstrap/preprocess.rb', line 5

def self.pre_process_bundle(filename)
  # Read the file
  puts "Processing #{File.basename(filename)}..."
  start = File.size(filename)
  json = File.open(filename, 'r:UTF-8', &:read)
  hash = JSON.parse(json)

  # Remove entries that do not interest us: CompartmentDefinitions, OperationDefinitions, Conformance statements
  hash['entry'].select! do |entry|
    ['StructureDefinition', 'ValueSet', 'CodeSystem', 'SearchParameter'].include? entry['resource']['resourceType']
  end

  # Remove unnecessary elements from the hash
  hash['entry'].each do |entry|
    next unless entry['resource']

    pre_process_structuredefinition(entry['resource']) if entry['resource']['resourceType'] == 'StructureDefinition'
    pre_process_valueset(entry['resource']) if entry['resource']['resourceType'] == 'ValueSet'
    pre_process_codesystem(entry['resource']) if entry['resource']['resourceType'] == 'CodeSystem'
    pre_process_searchparam(entry['resource']) if entry['resource']['resourceType'] == 'SearchParameter'
    remove_fhir_comments(entry['resource'])
  end

  # Output the post processed file
  f = File.open(filename, 'w:UTF-8')
  f.write(JSON.pretty_unparse(hash))
  f.close
  finish = File.size(filename)
  puts "  Removed #{(start - finish) / 1024} KB" if start != finish
end

.pre_process_codesystem(hash) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/fhir_models/bootstrap/preprocess.rb', line 69

def self.pre_process_codesystem(hash)
  # Remove large HTML narratives and unused content
  ['meta', 'text', 'publisher', 'contact', 'description', 'requirements'].each { |key| hash.delete(key) }
  return unless hash['concept']

  hash['concept'].each do |concept|
    pre_process_codesystem_concept(concept)
  end
end

.pre_process_codesystem_concept(hash) ⇒ Object



79
80
81
82
83
84
85
86
# File 'lib/fhir_models/bootstrap/preprocess.rb', line 79

def self.pre_process_codesystem_concept(hash)
  ['extension', 'definition', 'designation'].each { |key| hash.delete(key) }
  return unless hash['concept']

  hash['concept'].each do |concept|
    pre_process_codesystem_concept(concept)
  end
end

.pre_process_json_example(filename) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/fhir_models/bootstrap/preprocess.rb', line 129

def self.pre_process_json_example(filename)
  # Read the file
  puts "Processing #{File.basename(filename)}..."
  start = File.size(filename)
  json = File.open(filename, 'r:UTF-8', &:read)
  hash = JSON.parse(json)

  # Remove narratives
  ['text'].each { |key| hash.delete(key) }

  # Output the post processed file
  f = File.open(filename, 'w:UTF-8')
  f.write(JSON.pretty_unparse(hash))
  f.close
  finish = File.size(filename)
  puts "  Removed #{(start - finish) / 1024} KB" if start != finish
end

.pre_process_schema(filename) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/fhir_models/bootstrap/preprocess.rb', line 110

def self.pre_process_schema(filename)
  # Read the file
  puts "Processing #{File.basename(filename)}..."
  start = File.size(filename)
  raw = File.open(filename, 'r:UTF-8', &:read)

  # Remove annotations
  doc = Nokogiri::XML(raw)
  doc.root.add_namespace_definition('xs', 'http://www.w3.org/2001/XMLSchema')
  doc.search('//xs:annotation').each(&:remove)

  # Output the post processed file
  f = File.open(filename, 'w:UTF-8')
  f.write(doc.to_xml)
  f.close
  finish = File.size(filename)
  puts "  Removed #{(start - finish) / 1024} KB" if start != finish
end

.pre_process_searchparam(hash) ⇒ Object



88
89
90
91
# File 'lib/fhir_models/bootstrap/preprocess.rb', line 88

def self.pre_process_searchparam(hash)
  # Remove large HTML narratives and unused content
  ['id', 'url', 'name', 'date', 'publisher', 'contact', 'description', 'xpathUsage'].each { |key| hash.delete(key) }
end

.pre_process_structuredefinition(hash) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/fhir_models/bootstrap/preprocess.rb', line 36

def self.pre_process_structuredefinition(hash)
  # Remove large HTML narratives and unused content
  ['text', 'publisher', 'contact', 'description', 'requirements', 'mapping'].each { |key| hash.delete(key) }

  # Remove unused descriptions within the snapshot and differential elements
  ['snapshot', 'differential'].each do |key|
    next unless hash[key]

    hash[key]['element'].each do |element|
      ['short', 'definition', 'comments', 'requirements', 'alias', 'mapping'].each { |subkey| element.delete(subkey) }
    end
  end
end

.pre_process_valueset(hash) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/fhir_models/bootstrap/preprocess.rb', line 50

def self.pre_process_valueset(hash)
  # Remove large HTML narratives and unused content
  ['meta', 'text', 'publisher', 'contact', 'description', 'requirements'].each { |key| hash.delete(key) }

  return unless hash['compose']

  ['include', 'exclude'].each do |key|
    next unless hash['compose'][key]

    hash['compose'][key].each do |element|
      next unless element['concept']

      element['concept'].each do |concept|
        concept.delete('designation')
      end
    end
  end
end

.pre_process_xml_example(filename) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/fhir_models/bootstrap/preprocess.rb', line 147

def self.pre_process_xml_example(filename)
  # Read the file
  puts "Processing #{File.basename(filename)}..."
  start = File.size(filename)
  raw = File.open(filename, 'r:UTF-8', &:read)

  # Remove annotations
  doc = Nokogiri::XML(raw)
  doc.search('text').each do |node|
    node.remove if node.parent == doc.root
  end

  # Output the post processed file
  f = File.open(filename, 'w:UTF-8')
  f.write(doc.to_xml)
  f.close

  # Remove the weird parantheses on xml example filenames
  # we do this so they match the names of the json examples
  if filename.include?('(') && filename.include?(')')
    rename = filename.gsub(/\([A-Za-z0-9\-.]*\)/, '')
    File.rename(filename, rename)
    filename = rename
  end

  finish = File.size(filename)
  puts "  Removed #{(start - finish) / 1024} KB" if start != finish
end

.remove_fhir_comments(hash) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/fhir_models/bootstrap/preprocess.rb', line 93

def self.remove_fhir_comments(hash)
  hash.delete('fhir_comments')
  hash.each do |_key, value|
    case value
    when Hash
      remove_fhir_comments(value)
    when Array
      value.each do |v|
        remove_fhir_comments(v) if v.is_a?(Hash)
      end
    end
  end
  hash.keep_if do |_key, value|
    !value.is_a?(Hash) || !value.empty?
  end
end