Class: Inferno::Entities::IG

Inherits:
Entity
  • Object
show all
Defined in:
lib/inferno/entities/ig.rb

Overview

IG is a wrapper class around the relevant concepts inside an IG. Not everything within an IG is currently used by Inferno.

Constant Summary collapse

ATTRIBUTES =
[
  :id,
  :profiles,
  :extensions,
  :value_sets,
  :search_params,
  :examples
].freeze
FILES_TO_SKIP =

These files aren’t FHIR resources

['package.json', 'validation-summary.json'].freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Entity

#to_hash

Constructor Details

#initialize(**params) ⇒ IG

Returns a new instance of IG.



26
27
28
29
30
31
32
33
34
# File 'lib/inferno/entities/ig.rb', line 26

def initialize(**params)
  super(params, ATTRIBUTES)

  @profiles = []
  @extensions = []
  @value_sets = []
  @examples = []
  @search_params = []
end

Class Method Details

.from_directory(ig_directory) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/inferno/entities/ig.rb', line 77

def self.from_directory(ig_directory)
  ig = IG.new

  ig_path = Pathname.new(ig_directory)
  Dir.glob("#{ig_path}/**/*") do |f|
    relative_path = Pathname.new(f).relative_path_from(ig_path).to_s
    next if skip_item?(relative_path, File.directory?(f))

    begin
      resource = FHIR::Json.from_json(File.read(f))
      next if resource.nil?

      ig.handle_resource(resource, relative_path)
    rescue StandardError
      next
    end
  end
  ig
end

.from_file(ig_path) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/inferno/entities/ig.rb', line 36

def self.from_file(ig_path)
  raise "#{ig_path} does not exist" unless File.exist?(ig_path)

  # fhir_models by default logs the entire content of non-FHIR files
  # which could be things like a package.json
  original_logger = FHIR.logger
  FHIR.logger = Logger.new('/dev/null')

  if File.directory?(ig_path)
    from_directory(ig_path)
  elsif ig_path.end_with? '.tgz'
    from_tgz(ig_path)
  else
    raise "Unable to load #{ig_path} as it does not appear to be a directory or a .tgz file"
  end
ensure
  FHIR.logger = original_logger if defined? original_logger
end

.from_tgz(ig_path) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/inferno/entities/ig.rb', line 55

def self.from_tgz(ig_path)
  tar = Gem::Package::TarReader.new(
    Zlib::GzipReader.open(ig_path)
  )

  ig = IG.new

  tar.each do |entry|
    next if skip_item?(entry.full_name, entry.directory?)

    begin
      resource = FHIR::Json.from_json(entry.read)
      next if resource.nil?

      ig.handle_resource(resource, entry.full_name)
    rescue StandardError
      next
    end
  end
  ig
end

.skip_item?(relative_path, is_directory) ⇒ Boolean

Returns:

  • (Boolean)


100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/inferno/entities/ig.rb', line 100

def self.skip_item?(relative_path, is_directory)
  return true if is_directory

  file_name = relative_path.split('/').last

  return true unless file_name.end_with? '.json'
  return true unless relative_path.start_with? 'package/'

  return true if file_name.start_with? '.' # ignore hidden files
  return true if file_name.end_with? '.openapi.json'
  return true if FILES_TO_SKIP.include? file_name

  false
end

Instance Method Details

#extract_package_id(ig_resource) ⇒ Object



134
135
136
# File 'lib/inferno/entities/ig.rb', line 134

def extract_package_id(ig_resource)
  "#{ig_resource.id}##{ig_resource.version || 'current'}"
end

#handle_resource(resource, relative_path) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/inferno/entities/ig.rb', line 115

def handle_resource(resource, relative_path)
  case resource.resourceType
  when 'StructureDefinition'
    if resource.type == 'Extension'
      extensions.push resource
    else
      profiles.push resource
    end
  when 'ValueSet'
    value_sets.push resource
  when 'SearchParameter'
    search_params.push resource
  when 'ImplementationGuide'
    @id = extract_package_id(resource)
  else
    examples.push(resource) if relative_path.start_with? 'package/example'
  end
end