Class: FhirPackagesManager::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/fhir_packages_manager/registry.rb

Overview

Client for a single FHIR package registry (e.g. https://packages.fhir.org or https://packages.simplifier.net). Both implement the same npm-style registry API: GET /<package> returns metadata with a "versions" map and "dist-tags", and GET /<package>/<version> streams the .tgz tarball.

Constant Summary collapse

MAX_REDIRECTS =

Returns maximum HTTP redirects followed before raising HttpError.

Returns:

  • (Integer)

    maximum HTTP redirects followed before raising HttpError

5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url) ⇒ Registry

Returns a new instance of Registry.

Parameters:



21
22
23
24
# File 'lib/fhir_packages_manager/registry.rb', line 21

def initialize(base_url)
  @base_url = base_url.to_s.chomp('/')
  @metadata_cache = {} # : Hash[String, Hash[untyped, untyped]]
end

Instance Attribute Details

#base_urlString (readonly)

Returns the registry's base URL, with any trailing slash stripped.

Returns:

  • (String)

    the registry's base URL, with any trailing slash stripped



18
19
20
# File 'lib/fhir_packages_manager/registry.rb', line 18

def base_url
  @base_url
end

Instance Method Details

#download(name, version, destination_path) ⇒ String

Downloads the package tarball to destination_path, creating parent directories as needed.

Parameters:

  • name (String)

    the package name

  • version (String)

    an exact version, as returned by #version?

  • destination_path (String)

    where to write the downloaded .tgz file

Returns:

  • (String)

    destination_path

Raises:



80
81
82
83
84
# File 'lib/fhir_packages_manager/registry.rb', line 80

def download(name, version, destination_path)
  FileUtils.mkdir_p(File.dirname(destination_path))
  download_file(tarball_url(name, version), destination_path)
  destination_path
end

#metadata(name) ⇒ Hash

Fetches (and caches) the full registry metadata document for a package name.

Parameters:

  • name (String)

    the package name

Returns:

  • (Hash)

    the parsed npm-style registry metadata ("dist-tags", "versions", etc.)

Raises:



32
33
34
35
36
37
38
# File 'lib/fhir_packages_manager/registry.rb', line 32

def (name)
  @metadata_cache[name] ||= JSON.parse(get("#{base_url}/#{name}"))
rescue HttpError => e
  raise PackageNotFoundError, "#{name} not found on #{base_url}" if e.status == 404

  raise
end

#tarball_url(name, version) ⇒ String

Returns the tarball's download URL.

Parameters:

  • name (String)

    the package name

  • version (String)

    an exact version, as returned by #version?

Returns:

  • (String)

    the tarball's download URL

Raises:



64
65
66
67
68
69
70
# File 'lib/fhir_packages_manager/registry.rb', line 64

def tarball_url(name, version)
  meta = (name)
  entry = meta.dig('versions', version)
  raise PackageNotFoundError, "#{name}@#{version} not found on #{base_url}" unless entry

  entry.dig('dist', 'tarball') || "#{base_url}/#{name}/#{version}"
end

#version?(name, version = nil) ⇒ String?

Returns the resolved version string if it exists on this registry, or nil if the package or version doesn't exist (never raises).

Parameters:

  • name (String)

    the package name

  • version (String, nil) (defaults to: nil)

    a specific version, or nil/"latest" for the newest

Returns:

  • (String, nil)

    the resolved version string if it exists on this registry, or nil if the package or version doesn't exist (never raises)



44
45
46
47
48
49
50
# File 'lib/fhir_packages_manager/registry.rb', line 44

def version?(name, version = nil)
  meta = (name)
  resolved = resolve_version(meta, version)
  resolved && meta['versions']&.key?(resolved) ? resolved : nil
rescue PackageNotFoundError
  nil
end

#versions(name) ⇒ Array<String>

Returns every version published for this package on this registry.

Parameters:

  • name (String)

    the package name

Returns:

  • (Array<String>)

    every version published for this package on this registry

Raises:



56
57
58
# File 'lib/fhir_packages_manager/registry.rb', line 56

def versions(name)
  (name)['versions']&.keys || []
end