Sha256: ce5e414e26411c631609c5da371a4aebdde1f4a4725497fe678e2de4c57faefd

Contents?: true

Size: 1.72 KB

Versions: 14

Compression:

Stored size: 1.72 KB

Contents

require 'active_support/all'
require 'fhir_models'
require 'pathname'
require 'rubygems/package'
require 'zlib'
require_relative 'ig_resources'

module USCoreTestKit
  class Generator
    class IGLoader
      attr_accessor :ig_file_name

      def initialize(ig_file_name)
        self.ig_file_name = ig_file_name
      end

      def ig_resources
        @ig_resources ||= IGResources.new
      end

      def load
        load_ig
        load_standalone_resources
      end

      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

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

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
us_core_test_kit-0.6.0 lib/us_core_test_kit/generator/ig_loader.rb
us_core_test_kit-0.5.0 lib/us_core_test_kit/generator/ig_loader.rb
us_core_test_kit-0.4.7 lib/us_core_test_kit/generator/ig_loader.rb
us_core_test_kit-0.4.6 lib/us_core_test_kit/generator/ig_loader.rb
us_core_test_kit-0.4.5 lib/us_core_test_kit/generator/ig_loader.rb
us_core_test_kit-0.4.4 lib/us_core_test_kit/generator/ig_loader.rb
us_core_test_kit-0.4.3 lib/us_core_test_kit/generator/ig_loader.rb
us_core_test_kit-0.4.2 lib/us_core_test_kit/generator/ig_loader.rb
us_core_test_kit-0.4.1 lib/us_core_test_kit/generator/ig_loader.rb
us_core_test_kit-0.4.0 lib/us_core_test_kit/generator/ig_loader.rb
us_core_test_kit-0.3.2 lib/us_core_test_kit/generator/ig_loader.rb
us_core_test_kit-0.3.1 lib/us_core_test_kit/generator/ig_loader.rb
us_core_test_kit-0.3.0 lib/us_core_test_kit/generator/ig_loader.rb
us_core_test_kit-0.2.5 lib/us_core_test_kit/generator/ig_loader.rb