Class: Kward::OpenRouterAPIKey

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_path: OpenAIOAuth.default_config_path) ⇒ OpenRouterAPIKey

Returns a new instance of OpenRouterAPIKey.



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

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.



6
7
8
# File 'lib/kward/auth/openrouter_api_key.rb', line 6

def config_path
  @config_path
end

Instance Method Details

#api_keyObject



12
13
14
# File 'lib/kward/auth/openrouter_api_key.rb', line 12

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)


16
17
18
# File 'lib/kward/auth/openrouter_api_key.rb', line 16

def configured?
  !api_key.to_s.empty?
end

#login(prompt:) ⇒ Object



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

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



28
29
30
# File 'lib/kward/auth/openrouter_api_key.rb', line 28

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