Class: FHIR::Generator::IGLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/fhir_models/generator/ig_loader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ig_file_name) ⇒ IGLoader

Returns a new instance of IGLoader.



11
12
13
# File 'lib/fhir_models/generator/ig_loader.rb', line 11

def initialize(ig_file_name)
  self.ig_file_name = ig_file_name
end

Instance Attribute Details

#ig_file_nameObject

Returns the value of attribute ig_file_name.



9
10
11
# File 'lib/fhir_models/generator/ig_loader.rb', line 9

def ig_file_name
  @ig_file_name
end

Instance Method Details

#excluded_filesObject



19
20
21
# File 'lib/fhir_models/generator/ig_loader.rb', line 19

def excluded_files
  @excluded_files ||= []
end

#ig_resourcesObject



15
16
17
# File 'lib/fhir_models/generator/ig_loader.rb', line 15

def ig_resources
  @ig_resources ||= IGResources.new
end

#load(show_summary: false) ⇒ Object



23
24
25
26
27
# File 'lib/fhir_models/generator/ig_loader.rb', line 23

def load(show_summary: false)
  load_excluded_files
  load_ig(show_summary)
  load_supplement_resources
end

#load_excluded_filesObject



29
30
31
32
33
34
35
36
37
# File 'lib/fhir_models/generator/ig_loader.rb', line 29

def load_excluded_files
  ig_directory = ig_file_name.chomp('.tgz')

  return ig_resources unless File.exist? ig_directory

  file_name = File.join(ig_directory, 'package_exclude', 'excluded_files.json')

  @excluded_files = JSON.parse(File.read(file_name))
end

#load_ig(show_summary) ⇒ Object



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
# File 'lib/fhir_models/generator/ig_loader.rb', line 39

def load_ig(show_summary)
  tar = Gem::Package::TarReader.new(
    Zlib::GzipReader.open(ig_file_name)
  )

  tar.each do |entry|
    next if entry.directory?

    file_name = entry.full_name.split('/').last
    next unless file_name.end_with? '.json'
    next if excluded_files.include?(file_name)

    begin
      if file_name == 'package.json'
        resource = JSON.parse(entry.read)
        ig_resources. = .new(resource)
        puts "Extract FHIR Package version #{ig_resources.ig_metadata.version}" if show_summary
        next
      end

      next unless file_name.start_with?('CodeSystem', 'SearchParameter', 'StructureDefinition', 'ValueSet')

      resource = JSON.parse(entry.read)
      next if resource.empty?
    rescue StandardError
      puts "Cannot read and parse JSON file #{file_name}."
      next
    end

    ig_resources.add(resource)
  end

  if show_summary
    puts "Extracted Primitve Types: #{ig_resources.primitive_types&.count}"
    puts "Extracted Complex Types: #{ig_resources.complex_types&.count}"
    puts "Extracted Resource Definitions: #{ig_resources.resource_definitions&.count}"
    puts "Extracted Extension Definitions: #{ig_resources.extension_definitions&.count}"
    puts "Extracted Profiles: #{ig_resources.profiles&.count}"
    puts "Extracted Value Sets: #{ig_resources.get_value_sets&.count}"
    puts "Extracted Code Systems: #{ig_resources.get_code_systems&.count}"
    puts "Extracted Search Parameters: #{ig_resources.get_search_parameters&.count}"
  end

  ig_resources
end

#load_supplement_resourcesObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/fhir_models/generator/ig_loader.rb', line 85

def load_supplement_resources
  ig_directory = ig_file_name.chomp('.tgz')

  return ig_resources unless File.exist? ig_directory

  Dir.glob(File.join(ig_directory, 'package_supplement', '*.json')).each do |file_path|
    begin
      resource = JSON.parse(File.read(file_path))
      next if resource.empty?
    rescue StandardError
      file_name = file_path.split('/').last
      puts "Cannot read and parse JSON file #{file_name}."
      next
    end

    ig_resources.add(resource, break_bundle: true)
  end

  ig_resources
end