Class: Clacky::ClackyCloudConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/clacky/clacky_cloud_config.rb

Overview

ClackyCloudConfig — stores the Clacky Cloud credentials used for workspace-key import (workspace_api_key + backend base_url) in a dedicated file so the user never has to re-enter them.

File location: ~/.clacky/clacky_cloud.yml File format (YAML):

workspace_key: clacky_ak_xxxx
base_url: https://api.clacky.ai
dashboard_url: https://app.clacky.ai   # optional, inferred from base_url if absent

Usage:

cfg = ClackyCloudConfig.load
cfg.workspace_key   # => "clacky_ak_xxxx" or nil
cfg.base_url        # => "https://api.clacky.ai"
cfg.dashboard_url   # => "https://app.clacky.ai"  (explicit or inferred)
cfg.configured?     # => true / false

cfg.workspace_key = "clacky_ak_newkey"
cfg.save

Constant Summary collapse

CONFIG_DIR =
File.join(Dir.home, ".clacky")
CONFIG_FILE =
File.join(CONFIG_DIR, "clacky_cloud.yml")
DEFAULT_BASE_URL =
"https://api.clacky.ai"
DEFAULT_DASHBOARD_URL =
"https://app.clacky.ai"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(workspace_key: nil, base_url: DEFAULT_BASE_URL, dashboard_url: nil) ⇒ ClackyCloudConfig

Returns a new instance of ClackyCloudConfig.



35
36
37
38
39
40
41
42
43
44
# File 'lib/clacky/clacky_cloud_config.rb', line 35

def initialize(workspace_key: nil, base_url: DEFAULT_BASE_URL, dashboard_url: nil)
  @workspace_key = workspace_key.to_s.strip
  @workspace_key = nil if @workspace_key.empty?
  @base_url      = (base_url.to_s.strip.empty? ? DEFAULT_BASE_URL : base_url.to_s.strip)
                     .sub(%r{/+$}, "")  # strip trailing slash

  # dashboard_url: use explicit value if provided, otherwise infer from base_url
  explicit = dashboard_url.to_s.strip.sub(%r{/+$}, "")
  @dashboard_url = explicit.empty? ? infer_dashboard_url(@base_url) : explicit
end

Instance Attribute Details

#base_urlObject

Returns the value of attribute base_url.



33
34
35
# File 'lib/clacky/clacky_cloud_config.rb', line 33

def base_url
  @base_url
end

#dashboard_urlObject

Returns the value of attribute dashboard_url.



33
34
35
# File 'lib/clacky/clacky_cloud_config.rb', line 33

def dashboard_url
  @dashboard_url
end

#workspace_keyObject

Returns the value of attribute workspace_key.



33
34
35
# File 'lib/clacky/clacky_cloud_config.rb', line 33

def workspace_key
  @workspace_key
end

Class Method Details

.clear!(config_file = CONFIG_FILE) ⇒ Object

Remove the saved file (used for reset / tests)



89
90
91
# File 'lib/clacky/clacky_cloud_config.rb', line 89

def self.clear!(config_file = CONFIG_FILE)
  FileUtils.rm_f(config_file)
end

.load(config_file = CONFIG_FILE) ⇒ Object

Load from ~/.clacky/clacky_cloud.yml (returns an empty config if the file is absent)



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/clacky/clacky_cloud_config.rb', line 47

def self.load(config_file = CONFIG_FILE)
  if File.exist?(config_file)
    data = YAML.safe_load(File.read(config_file)) || {}
    new(
      workspace_key: data["workspace_key"],
      base_url:      data["base_url"]      || DEFAULT_BASE_URL,
      dashboard_url: data["dashboard_url"]
    )
  else
    new
  end
rescue => e
  # Corrupt file — return empty config rather than crash
  warn "[clacky_cloud_config] Failed to load #{config_file}: #{e.message}"
  new
end

Instance Method Details

#configured?Boolean

True when a non-empty workspace_key is stored

Returns:

  • (Boolean)


84
85
86
# File 'lib/clacky/clacky_cloud_config.rb', line 84

def configured?
  !@workspace_key.nil? && !@workspace_key.empty?
end

#save(config_file = CONFIG_FILE) ⇒ Object

Persist to ~/.clacky/clacky_cloud.yml



65
66
67
68
69
70
# File 'lib/clacky/clacky_cloud_config.rb', line 65

def save(config_file = CONFIG_FILE)
  FileUtils.mkdir_p(File.dirname(config_file))
  File.write(config_file, to_yaml)
  FileUtils.chmod(0o600, config_file)
  self
end

#to_yamlObject

Serialize to YAML string



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

def to_yaml
  data = { "base_url" => @base_url }
  data["workspace_key"]  = @workspace_key  if @workspace_key
  # Only persist dashboard_url when it differs from the inferred default,
  # so the file stays minimal for users who don't need to override it.
  inferred = infer_dashboard_url(@base_url)
  data["dashboard_url"]  = @dashboard_url  if @dashboard_url != inferred
  YAML.dump(data)
end