Module: GeekDict::Config

Defined in:
lib/geekdict/config.rb

Constant Summary collapse

CONFIG_PATH =
File.expand_path("~/.geekdict.config")
DEFAULT_PROVIDER =
"openrouter"
DEFAULT_MODEL =
"google/gemini-2.5-flash-lite"
ALLOWED_PROVIDERS =
['openai', 'openrouter', 'youdao'].freeze

Class Method Summary collapse

Class Method Details

.create_default_configObject

Method to create default configuration file



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

def create_default_config
  default_config = {
    'provider' => DEFAULT_PROVIDER,
    'model' => DEFAULT_MODEL
  }

  begin
    # Ensure the directory exists
    config_dir = File.dirname(CONFIG_PATH)
    FileUtils.mkdir_p(config_dir) unless Dir.exist?(config_dir)

    # Write the default config
    File.open(CONFIG_PATH, 'w') do |file|
      file.write(YAML.dump(default_config))
    end

    puts "Created default config file at #{CONFIG_PATH}"
  rescue => e
    warn "Warning: Could not create config file #{CONFIG_PATH}: #{e.message}"
  end
end

.load_configObject

Method to load configuration from ~/.geekdict.config



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/geekdict/config.rb', line 14

def load_config
  config = {}

  # If config file doesn't exist, create it with default values
  unless File.exist?(CONFIG_PATH)
    create_default_config
  end

  if File.exist?(CONFIG_PATH)
    begin
      loaded_config = YAML.load_file(CONFIG_PATH)
      config = loaded_config if loaded_config.is_a?(Hash)
    rescue Psych::SyntaxError => e
      warn "Warning: Error parsing config file #{CONFIG_PATH}: #{e.message}. Using defaults."
    rescue => e
      warn "Warning: Could not load config file #{CONFIG_PATH}: #{e.message}. Using defaults."
    end
  end
  # Ensure keys are symbols for easier access, handle potential nil values
  config = config.transform_keys { |k| k.to_s.downcase.to_sym rescue k } # Make keys symbols & lowercase
  {
    provider: config[:provider],
    model: config[:model]
  }
end