Module: Inferno::Utils::IgDownloader

Included in:
CLI::New, Repositories::IGs
Defined in:
lib/inferno/utils/ig_downloader.rb

Constant Summary collapse

FHIR_PACKAGE_NAME_REG_EX =
/^[a-z][a-zA-Z0-9-]*\.([a-z][a-zA-Z0-9-]*\.?)*/
HTTP_URI_REG_EX =
%r{^https?://[^/?#]+[^?#]*}
FILE_URI_REG_EX =
%r{^file://(.+)}
HTTP_URI_END_REG_EX =
%r{[^/]*\.x?html?$}

Instance Method Summary collapse

Instance Method Details

#download_file(uri, destination) ⇒ Object



38
39
40
41
42
43
# File 'lib/inferno/utils/ig_downloader.rb', line 38

def download_file(uri, destination)
  # Inspired by Thor `get`
  # https://github.com/rails/thor/blob/3178667e1727504bf4fb693bf4ac74a5ca6c691e/lib/thor/actions/file_manipulation.rb#L81
  download = URI.send(:open, uri)
  IO.copy_stream(download, destination)
end

#ig_file(suffix = nil) ⇒ Object



15
16
17
# File 'lib/inferno/utils/ig_downloader.rb', line 15

def ig_file(suffix = nil)
  File.join(ig_path, suffix ? "package_#{suffix}.tgz" : 'package.tgz')
end

#ig_http_url(ig_page_url) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/inferno/utils/ig_downloader.rb', line 59

def ig_http_url(ig_page_url)
  return ig_page_url if ig_page_url.end_with? 'package.tgz'

  return "#{ig_page_url}package.tgz" if ig_page_url.end_with? '/'

  ig_page_url.gsub(HTTP_URI_END_REG_EX, 'package.tgz')
end

#ig_pathObject



11
12
13
# File 'lib/inferno/utils/ig_downloader.rb', line 11

def ig_path
  File.join('lib', library_name, 'igs')
end

#ig_registry_url(ig_npm_style) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/inferno/utils/ig_downloader.rb', line 45

def ig_registry_url(ig_npm_style)
  if ig_npm_style.include?('@')
    package_name, version = ig_npm_style.split('@')
  elsif ig_npm_style.include?('#')
    package_name, version = ig_npm_style.split('#')
  else
    raise StandardError, <<~NO_VERSION
      No IG version specified for #{ig_npm_style}; you must specify one with '@' or '#'. I.e: hl7.fhir.us.core@6.1.0
    NO_VERSION
  end

  "https://packages.fhir.org/#{package_name}/-/#{package_name}-#{version}.tgz"
end

#load_ig(ig_input, idx = nil, output_path = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/inferno/utils/ig_downloader.rb', line 19

def load_ig(ig_input, idx = nil, output_path = nil)
  case ig_input
  when FHIR_PACKAGE_NAME_REG_EX
    uri = ig_registry_url(ig_input)
  when HTTP_URI_REG_EX
    uri = ig_http_url(ig_input)
  when FILE_URI_REG_EX
    uri = ig_input[7..]
  else
    raise StandardError, <<~FAILED_TO_LOAD
      Could not find implementation guide: #{ig_input}
    FAILED_TO_LOAD
  end

  destination = output_path || ig_file(idx)
  download_file(uri, destination)
  uri
end