Class: Wiq::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/wiq/config.rb

Overview

Resolves host + alias + token from the documented precedence chain.

Host:

--host > WIQ_HOST > .wiq/config.json:host > sole stored host
      > PRODUCTION_HOST

Alias (only meaningful once host is known):

--as > WIQ_ALIAS > .wiq/config.json:alias > sole alias for host > "default"

Token:

WIQ_TOKEN (direct override) > credentials store (host, alias)

Constant Summary collapse

PRODUCTION_HOST =
"https://www.wrestlingiq.com"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Config

Returns a new instance of Config.



26
27
28
29
# File 'lib/wiq/config.rb', line 26

def initialize(options = {})
  @options = options || {}
  @sources = {}
end

Instance Attribute Details

#alias_nameObject (readonly)

Returns the value of attribute alias_name.



20
21
22
# File 'lib/wiq/config.rb', line 20

def alias_name
  @alias_name
end

#hostObject (readonly)

Returns the value of attribute host.



20
21
22
# File 'lib/wiq/config.rb', line 20

def host
  @host
end

#sourcesObject (readonly)

Returns the value of attribute sources.



20
21
22
# File 'lib/wiq/config.rb', line 20

def sources
  @sources
end

#tokenObject (readonly)

Returns the value of attribute token.



20
21
22
# File 'lib/wiq/config.rb', line 20

def token
  @token
end

Class Method Details

.load(options = {}) ⇒ Object



22
23
24
# File 'lib/wiq/config.rb', line 22

def self.load(options = {})
  new(options).tap(&:resolve!)
end

Instance Method Details

#require_host!Object

Raises:



38
39
40
# File 'lib/wiq/config.rb', line 38

def require_host!
  raise HostUnsetError unless @host
end

#require_token!Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/wiq/config.rb', line 42

def require_token!
  require_host!
  return if @token

  # No env override, no usable alias-keyed entry — decide which error to raise.
  aliases = Credentials.aliases_for(@host)
  if aliases.empty?
    raise NotAuthenticatedError
  elsif @alias_name && !Credentials.for_host(@host, @alias_name)
    raise AliasNotFoundError.new(@host, @alias_name, aliases)
  elsif @alias_name.nil?
    raise AmbiguousAliasError.new(@host, aliases)
  else
    raise NotAuthenticatedError
  end
end

#resolve!Object



31
32
33
34
35
36
# File 'lib/wiq/config.rb', line 31

def resolve!
  @host = resolve_host
  @alias_name = resolve_alias if @host
  @token = resolve_token
  self
end

#traceObject



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/wiq/config.rb', line 59

def trace
  {
    host: @host,
    host_source: @sources[:host],
    alias: @alias_name,
    alias_source: @sources[:alias],
    token_present: !@token.nil?,
    token_source: @sources[:token],
    credentials_path: Credentials.path,
    repo_config_path: repo_config_path
  }
end