Class: DaVinciUSDrugFormularyTestKit::Generator::IGLoader

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



13
14
15
# File 'lib/davinci_us_drug_formulary_test_kit/generator/ig_loader.rb', line 13

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.



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

def ig_file_name
  @ig_file_name
end

Instance Method Details

#ig_resourcesObject



17
18
19
# File 'lib/davinci_us_drug_formulary_test_kit/generator/ig_loader.rb', line 17

def ig_resources
  @ig_resources ||= IGResources.new
end

#loadObject



21
22
23
24
25
26
27
28
29
30
# File 'lib/davinci_us_drug_formulary_test_kit/generator/ig_loader.rb', line 21

def load
  # `IGResources` uses the first resource it finds for a resource type.
  # To "supercede" resources, load them first. Any duplicate resources will
  # be accessed instead of the IG resources because they are "first."
  # To "supplement" resources, load them last. Any duplicate resources will
  # be ignored because the IG resources are "first."
  load_supersede_resources
  load_ig
  load_supplement_resources
end

#load_igObject



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

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 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

  ig_resources
end

#load_standalone_resources(appendage = '') ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/davinci_us_drug_formulary_test_kit/generator/ig_loader.rb', line 69

def load_standalone_resources(appendage = '')
  ig_directory = "#{ig_file_name.chomp('.tgz')}#{appendage}"

  Dir.glob(File.join(ig_directory, '*.json')).each do |file_path|
    resource = FHIR.from_contents(File.read(file_path))
    next if resource.nil?

    if resource.resourceType == 'Bundle' && !resource.entry.nil?
      resource_arr = resource.entry
      resource_arr.each do |entry|
        ig_resources.add(entry.resource)
      end
    else
      ig_resources.add(resource)
    end
  rescue StandardError
    file_name = file_path.split('/').last
    puts "#{file_name} does not appear to be a FHIR resource."
    next
  end

  ig_resources
end

#load_supersede_resourcesObject



61
62
63
# File 'lib/davinci_us_drug_formulary_test_kit/generator/ig_loader.rb', line 61

def load_supersede_resources
  load_standalone_resources('_supersede')
end

#load_supplement_resourcesObject



65
66
67
# File 'lib/davinci_us_drug_formulary_test_kit/generator/ig_loader.rb', line 65

def load_supplement_resources
  load_standalone_resources('_supplement')
end