Module: Restless::Settings

Defined in:
lib/restless/settings.rb

Overview

CONTRACT.md section 12. .restless/settings.json is created and owned by the api CLI (npx api setup); the SDK consumes exactly two fields of it at runtime (CONFIG-015).

Defined Under Namespace

Classes: ConfigError

Class Method Summary collapse

Class Method Details

.load(start_dir = Dir.pwd) ⇒ Object

CONFIG-010, CONFIG-011. Walk up from the working directory to the filesystem root, take the first hit, and read at most once per process -- including the negative result.



20
21
22
23
24
25
26
27
# File 'lib/restless/settings.rb', line 20

def load(start_dir = Dir.pwd)
  @mutex.synchronize do
    return @cache if @loaded

    @loaded = true
    @cache = read_settings(start_dir)
  end
end

.reset_cache!Object

Test-only. Do not call from production code.



30
31
32
33
34
35
# File 'lib/restless/settings.rb', line 30

def reset_cache!
  @mutex.synchronize do
    @loaded = false
    @cache = nil
  end
end

.resolve_api(settings, name = nil) ⇒ Object

CONFIG-013, CONFIG-014. Returns name:, request_id_prefix:, redact: or nil.

Raises:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/restless/settings.rb', line 39

def resolve_api(settings, name = nil)
  return nil if settings.nil?

  apis = settings["apis"]
  return nil unless apis.is_a?(Array) && !apis.empty?

  if name && !name.empty?
    match = apis.find { |a| a["name"] == name } || apis.find { |a| a["id"] == name }
    unless match
      raise ConfigError,
            "restless-sdk: no API named #{name.inspect} in .restless/settings.json " \
            "(found: #{apis.map { |a| a['name'] }.join(', ')})"
    end
    return entry(match)
  end

  return entry(apis[0]) if apis.length == 1

  # Guessing would silently apply the wrong redaction list.
  raise ConfigError,
        "restless-sdk: .restless/settings.json has multiple APIs " \
        "(#{apis.map { |a| a['name'] }.join(', ')}) -- pass api: \"<name>\" to " \
        "Restless::Client.new to pick one."
end