Class: Nanobot::Config::Loader

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

Overview

Loader handles loading and saving configuration from/to JSON files

Constant Summary collapse

DEFAULT_CONFIG_PATH =
File.expand_path('~/.nanobot/config.json').freeze

Class Method Summary collapse

Class Method Details

.create_default(path = nil) ⇒ Config

Create default config file with helpful placeholders

Parameters:

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

    custom path or nil for default

Returns:



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/nanobot/config/loader.rb', line 66

def create_default(path = nil)
  config = Config.new(
    providers: {
      anthropic: {
        api_key: 'sk-ant-api03-...'
      },
      openai: {
        api_key: 'sk-...'
      },
      openrouter: {
        api_key: 'sk-or-v1-...',
        api_base: 'https://openrouter.ai/api/v1'
      }
    },
    tools: {
      web: {
        search: {
          api_key: 'BSA...'
        }
      }
    }
  )
  save(config, path)
  config
end

.exists?(path = nil) ⇒ Boolean

Check if config exists

Parameters:

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

    custom path or nil for default

Returns:

  • (Boolean)


59
60
61
# File 'lib/nanobot/config/loader.rb', line 59

def exists?(path = nil)
  get_config_path(path).exist?
end

.get_config_path(path = nil) ⇒ Pathname

Get config path

Parameters:

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

    custom path or nil for default

Returns:

  • (Pathname)


52
53
54
# File 'lib/nanobot/config/loader.rb', line 52

def get_config_path(path = nil)
  Pathname.new(path || DEFAULT_CONFIG_PATH)
end

.load(path = nil) ⇒ Config

Load configuration from file

Parameters:

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

    path to config file (default: ~/.nanobot/config.json)

Returns:



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/nanobot/config/loader.rb', line 17

def load(path = nil)
  config_path = Pathname.new(path || DEFAULT_CONFIG_PATH)

  unless config_path.exist?
    # Return default config if file doesn't exist
    return Config.new
  end

  begin
    data = JSON.parse(config_path.read, symbolize_names: true)
    Config.new(**data)
  rescue StandardError => e
    raise "Error loading config from #{config_path}: #{e.message}"
  end
end

.save(config, path = nil) ⇒ Object

Save configuration to file

Parameters:

  • config (Config) —

    configuration to save

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

    path to save to (default: ~/.nanobot/config.json)



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/nanobot/config/loader.rb', line 36

def save(config, path = nil)
  config_path = Pathname.new(path || DEFAULT_CONFIG_PATH)

  # Ensure directory exists
  config_path.dirname.mkpath unless config_path.dirname.exist?

  # Convert to hash and save as JSON
  data = config_to_hash(config)

  config_path.write(JSON.pretty_generate(data))
  FileUtils.chmod(0o600, config_path)
end