Class: Gem::Guardian::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/gem/guardian/configuration.rb

Overview

Project-level configuration loaded from .gem-guardian.yml.

The configuration file is intentionally small and policy-oriented. Its first supported use case is declaring publisher checksum providers for private gem registries that do not expose RubyGems.org checksum metadata.

Examples:

Publisher checksum provider

checksum_providers:
  - name: contribsys-sidekiq
    source: https://gems.contribsys.com/
    template: https://gems.contribsys.com/checksums/{filename}.sha256

Constant Summary collapse

DEFAULT_PATH =
".gem-guardian.yml"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path: nil, checksum_providers: []) ⇒ Configuration

Returns a new instance of Configuration.

Parameters:

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

    source path of the loaded configuration

  • checksum_providers (Array<#checksum_for>) (defaults to: [])

    configured checksum providers



46
47
48
49
# File 'lib/gem/guardian/configuration.rb', line 46

def initialize(path: nil, checksum_providers: [])
  @path = path
  @checksum_providers = checksum_providers
end

Instance Attribute Details

#checksum_providersObject (readonly)

Returns the value of attribute checksum_providers.



23
24
25
# File 'lib/gem/guardian/configuration.rb', line 23

def checksum_providers
  @checksum_providers
end

#pathObject (readonly)

Returns the value of attribute path.



23
24
25
# File 'lib/gem/guardian/configuration.rb', line 23

def path
  @path
end

Class Method Details

.load(path: ENV.fetch("GEM_GUARDIAN_CONFIG", DEFAULT_PATH), cwd: Dir.pwd) ⇒ Configuration

Loads configuration from .gem-guardian.yml in the current directory, or from GEM_GUARDIAN_CONFIG when that environment variable is set.

Parameters:

  • path (String, nil) (defaults to: ENV.fetch("GEM_GUARDIAN_CONFIG", DEFAULT_PATH))

    explicit configuration path

  • cwd (String) (defaults to: Dir.pwd)

    working directory used for relative paths

Returns:

  • (Configuration)

    parsed configuration. Missing files produce an empty configuration.

Raises:

  • (Error)

    when the YAML is invalid or has an unsupported shape



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/gem/guardian/configuration.rb', line 32

def self.load(path: ENV.fetch("GEM_GUARDIAN_CONFIG", DEFAULT_PATH), cwd: Dir.pwd)
  full_path = absolute_path(path, cwd)
  return new(path: full_path) unless File.file?(full_path)

  data = YAML.safe_load_file(full_path, permitted_classes: [], aliases: false) || {}
  raise Error, "#{full_path} must contain a YAML mapping" unless data.is_a?(Hash)

  new(path: full_path, checksum_providers: build_checksum_providers(data.fetch("checksum_providers", [])))
rescue Psych::Exception => e
  raise Error, "Invalid gem-guardian config #{full_path}: #{e.message}"
end

Instance Method Details

#checksum_providers?Boolean

Returns whether the config declares any checksum providers.

Returns:

  • (Boolean)

    whether the config declares any checksum providers



52
53
54
# File 'lib/gem/guardian/configuration.rb', line 52

def checksum_providers?
  !checksum_providers.empty?
end