Class: Kward::OpenRouterAPIKey

Inherits:
Object
  • Object
show all
Defined in:
lib/kward/auth/openrouter_api_key.rb

Overview

Config helper for storing and removing OpenRouter API keys.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_path: OpenAIOAuth.default_config_path) ⇒ OpenRouterAPIKey

Returns a new instance of OpenRouterAPIKey.



10
11
12
# File 'lib/kward/auth/openrouter_api_key.rb', line 10

def initialize(config_path: OpenAIOAuth.default_config_path)
  @config_path = File.expand_path(config_path)
end

Instance Attribute Details

#config_pathObject (readonly)

Returns the value of attribute config_path.



8
9
10
# File 'lib/kward/auth/openrouter_api_key.rb', line 8

def config_path
  @config_path
end

Instance Method Details

#api_keyObject



14
15
16
# File 'lib/kward/auth/openrouter_api_key.rb', line 14

def api_key
  ENV["OPENROUTER_API_KEY"].to_s.empty? ? ConfigFiles.config_value(config, "openrouter_api_key") : ENV["OPENROUTER_API_KEY"]
end

#configured?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/kward/auth/openrouter_api_key.rb', line 18

def configured?
  !api_key.to_s.empty?
end

#login(prompt:) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/kward/auth/openrouter_api_key.rb', line 22

def (prompt:)
  api_key = prompt.ask("OpenRouter API key:").to_s.strip
  raise "OpenRouter API key must be a non-empty string" if api_key.empty?

  ConfigFiles.update_config({ "openrouter_api_key" => api_key }, @config_path)
  @config_path
end

#logoutObject



30
31
32
# File 'lib/kward/auth/openrouter_api_key.rb', line 30

def logout
  ConfigFiles.delete_config_key("openrouter_api_key", @config_path)
end