Class: Nanobot::Config::Loader
- Inherits:
-
Object
- Object
- Nanobot::Config::Loader
- 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.('~/.nanobot/config.json').freeze
Class Method Summary collapse
-
.create_default(path = nil) ⇒ Config
Create default config file with helpful placeholders.
-
.exists?(path = nil) ⇒ Boolean
Check if config exists.
-
.get_config_path(path = nil) ⇒ Pathname
Get config path.
-
.load(path = nil) ⇒ Config
Load configuration from file.
-
.save(config, path = nil) ⇒ Object
Save configuration to file.
Class Method Details
.create_default(path = nil) ⇒ Config
Create default config file with helpful placeholders
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
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
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
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
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 |