Class: AUCoreTestKit::Generator::IGLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/au_core_test_kit/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.



16
17
18
# File 'lib/au_core_test_kit/generator/ig_loader.rb', line 16

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.



14
15
16
# File 'lib/au_core_test_kit/generator/ig_loader.rb', line 14

def ig_file_name
  @ig_file_name
end

Instance Method Details

#ig_resourcesObject



20
21
22
# File 'lib/au_core_test_kit/generator/ig_loader.rb', line 20

def ig_resources
  @ig_resources ||= IGResources.new
end

#loadObject



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

def load
  load_ig
  load_standalone_resources
end

#load_igObject



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

def load_ig
  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 if file_name.end_with? 'openapi.json'

    next unless file_name.end_with? '.json'

    next unless entry.full_name.start_with? 'package/'

    begin
      resource = FHIR.from_contents(entry.read)
      next if resource.nil?
    rescue StandardError
      puts "#{file_name} does not appear to be a FHIR resource."
      next
    end

    ig_resources.add(resource)
  end
  json_files = Dir.glob(File.join(Dir.pwd, 'lib', 'au_core_test_kit', 'igs', '*.json'))
  json_files.each do |file_path|
    file_content = File.read(file_path)
    bundle = FHIR.from_contents(file_content)
    bundle.entry.each do |entry|
      ig_resources.add(entry.resource)
    end
  end
  ig_resources
end

#load_standalone_resourcesObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/au_core_test_kit/generator/ig_loader.rb', line 66

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

  return ig_resources unless File.exist? ig_directory

  Dir.glob(File.join(ig_directory, '*.json')).each do |file_path|
    begin
      resource = FHIR.from_contents(File.read(file_path))
      next if resource.nil?
    rescue StandardError
      file_name = file_path.split('/').last
      puts "#{file_name} does not appear to be a FHIR resource."
      next
    end

    ig_resources.add(resource)
  end

  ig_resources
end