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,
  :resources_by_type,
  :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.



23
24
25
26
27
# File 'lib/inferno/entities/ig.rb', line 23

def initialize(**params)
  super(params, ATTRIBUTES)
  @resources_by_type ||= Hash.new { |hash, key| hash[key] = [] }
  @examples = []
end

Class Method Details

.extract_package_id(ig_resource) ⇒ Object



122
123
124
# File 'lib/inferno/entities/ig.rb', line 122

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

.from_directory(ig_directory) ⇒ Object



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

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.id = extract_package_id(ig.ig_resource)

  ig
end

.from_file(ig_path) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/inferno/entities/ig.rb', line 29

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



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/inferno/entities/ig.rb', line 48

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.id = extract_package_id(ig.ig_resource)

  ig
end

.skip_item?(relative_path, is_directory) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#capability_statement(mode = 'server') ⇒ Object



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

def capability_statement(mode = 'server')
  resources_by_type['CapabilityStatement'].find do |capability_statement_resource|
    capability_statement_resource.rest.any? { |r| r.mode == mode }
  end
end

#code_system_by_url(url) ⇒ Object



156
157
158
# File 'lib/inferno/entities/ig.rb', line 156

def code_system_by_url(url)
  resources_by_type['CodeSystem'].find { |system| system.url == url }
end

#extensionsObject



130
131
132
# File 'lib/inferno/entities/ig.rb', line 130

def extensions
  resources_by_type['StructureDefinition'].filter { |sd| sd.type == 'Extension' }
end

#handle_resource(resource, relative_path) ⇒ Object



114
115
116
117
118
119
120
# File 'lib/inferno/entities/ig.rb', line 114

def handle_resource(resource, relative_path)
  if relative_path.start_with? 'package/example'
    examples << resource
  else
    resources_by_type[resource.resourceType] << resource
  end
end

#ig_resourceObject



140
141
142
# File 'lib/inferno/entities/ig.rb', line 140

def ig_resource
  resources_by_type['ImplementationGuide'].first
end

#profile_by_url(url) ⇒ Object



144
145
146
# File 'lib/inferno/entities/ig.rb', line 144

def profile_by_url(url)
  profiles.find { |profile| profile.url == url }
end

#profilesObject



126
127
128
# File 'lib/inferno/entities/ig.rb', line 126

def profiles
  resources_by_type['StructureDefinition'].filter { |sd| sd.type != 'Extension' }
end

#resource_for_profile(url) ⇒ Object



148
149
150
# File 'lib/inferno/entities/ig.rb', line 148

def resource_for_profile(url)
  profiles.find { |profile| profile.url == url }.type
end

#value_set_by_url(url) ⇒ Object



152
153
154
# File 'lib/inferno/entities/ig.rb', line 152

def value_set_by_url(url)
  resources_by_type['ValueSet'].find { |profile| profile.url == url }
end