Class: FhirPackagesManager::Manager

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

Overview

Orchestrates checking availability across a set of registries, honoring an ignore list, and downloading tarballs into a destination folder.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(registries:, destination:, ignore_list: nil) ⇒ Manager

Returns a new instance of Manager.

Parameters:

  • registries (Array<String, Registry>)

    one or more registry base URLs and/or Registry instances, checked in the order given

  • destination (String)

    folder downloaded tarballs are written into

  • ignore_list (IgnoreList, nil) (defaults to: nil)

    packages/versions to skip in #fetch

Raises:

  • (ArgumentError)

    if registries is nil or empty



21
22
23
24
25
26
27
# File 'lib/fhir_packages_manager/manager.rb', line 21

def initialize(registries:, destination:, ignore_list: nil)
  raise ArgumentError, 'at least one registry is required' if registries.nil? || registries.empty?

  @registries = registries.map { |entry| entry.is_a?(Registry) ? entry : Registry.new(entry) }
  @destination = destination.to_s
  @ignore_list = ignore_list
end

Instance Attribute Details

#destinationString (readonly)

Returns the folder packages are downloaded into.

Returns:

  • (String)

    the folder packages are downloaded into



12
13
14
# File 'lib/fhir_packages_manager/manager.rb', line 12

def destination
  @destination
end

#ignore_listIgnoreList? (readonly)

Returns packages/versions skipped by #fetch.

Returns:



14
15
16
# File 'lib/fhir_packages_manager/manager.rb', line 14

def ignore_list
  @ignore_list
end

#registriesArray<Registry> (readonly)

Returns registries checked, in the order given to #initialize.

Returns:



10
11
12
# File 'lib/fhir_packages_manager/manager.rb', line 10

def registries
  @registries
end

Instance Method Details

#available?(name, version = nil) ⇒ Boolean

Returns true if any configured registry has this package/version.

Parameters:

  • name (String)

    the package name

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

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

Returns:

  • (Boolean)

    true if any configured registry has this package/version



32
33
34
# File 'lib/fhir_packages_manager/manager.rb', line 32

def available?(name, version = nil)
  !find_registry(name, version).nil?
end

#fetch(package) ⇒ FetchResult

Fetches a single package. Skips it if it's on the ignore list, otherwise downloads its tarball into #destination.

Parameters:

  • package (String, Package)

    a "name@version" string, a bare name (latest), or a Package

Returns:



53
54
55
# File 'lib/fhir_packages_manager/manager.rb', line 53

def fetch(package)
  fetch_package(Package.parse(package))
end

#fetch_all(packages) ⇒ Array<FetchResult>

Returns one result per package, in the same order.

Parameters:

Returns:

  • (Array<FetchResult>)

    one result per package, in the same order



59
60
61
# File 'lib/fhir_packages_manager/manager.rb', line 59

def fetch_all(packages)
  packages.map { |package| fetch(package) }
end

#find_registry(name, version = nil) ⇒ Array(Registry, String)?

Returns the first registry that has this package/version, paired with the resolved version string; nil if none of them do.

Parameters:

  • name (String)

    the package name

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

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

Returns:

  • (Array(Registry, String), nil)

    the first registry that has this package/version, paired with the resolved version string; nil if none of them do



40
41
42
43
44
45
46
# File 'lib/fhir_packages_manager/manager.rb', line 40

def find_registry(name, version = nil)
  registries.each do |registry|
    resolved = registry.version?(name, version)
    return [registry, resolved] if resolved
  end
  nil
end

#list_versions(name) ⇒ Hash{String => Array<String>}

Lists every version of a package published across all configured registries, skipping registries that don't have it at all and filtering out any ignored versions.

Parameters:

  • name (String)

    the package name

Returns:

  • (Hash{String => Array<String>})

    versions, keyed by registry base_url, for registries that have at least one non-ignored version; empty if the whole package is ignored or no registry has it

Raises:

  • (HttpError)

    if a registry that does have the package fails for another reason (mirrors #find_registry/Registry#version?, which likewise only treat "not found" as an expected, non-raising outcome)



73
74
75
76
77
78
79
80
81
# File 'lib/fhir_packages_manager/manager.rb', line 73

def list_versions(name)
  result = {} # : Hash[String, Array[String]]
  return result if ignored?(Package.new(name, nil))

  registries.each_with_object(result) do |registry, found|
    versions = available_versions(registry, name)
    found[registry.base_url] = versions unless versions.empty?
  end
end

#sync(name) ⇒ Array<FetchResult>

Downloads every non-ignored version of a package that isn't already present in #destination (as name-version.tgz, the same convention #fetch downloads use). A whole-package ignore skips it entirely, without querying any registry; an individual ignored version instead surfaces as a normal :ignored FetchResult, same as #fetch.

Parameters:

  • name (String)

    the package name

Returns:

  • (Array<FetchResult>)

    one result per version considered, across all registries



90
91
92
93
94
95
# File 'lib/fhir_packages_manager/manager.rb', line 90

def sync(name)
  whole_package = Package.new(name, nil)
  return [FetchResult.new(package: whole_package, status: :ignored)] if ignored?(whole_package)

  candidate_versions(name).map { |version| sync_version(Package.new(name, version)) }
end