Class: Credentials

Inherits:
Object
  • Object
show all
Defined in:
lib/credentials.rb

Overview

Credentials class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCredentials

Returns a new instance of Credentials.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/credentials.rb', line 10

def initialize
  @credentials_file = File.expand_path('~/.gchatsh')
  unless File.exist?(credentials_file)
    File.write(credentials_file, initial_file_format)
    error_message = <<~ERROR
      You need to add your API key to the credentials file at #{credentials_file}
    ERROR
    raise "\n#{error_message}"
  end

  Dotenv.load(credentials_file)

  if ENV['KEY'].empty?
    raise missing_config_message('API key not found')
  elsif ENV['URL'].empty?
    raise missing_config_message('API URL not found')
  elsif ENV['MODEL'].empty?
    raise missing_config_message('Model not found')
  end

  @url = ENV['URL']
  @key = ENV['KEY']
  @model = ENV['MODEL']
end

Instance Attribute Details

#credentials_fileObject

Returns the value of attribute credentials_file.



8
9
10
# File 'lib/credentials.rb', line 8

def credentials_file
  @credentials_file
end

#keyObject

Returns the value of attribute key.



8
9
10
# File 'lib/credentials.rb', line 8

def key
  @key
end

#modelObject

Returns the value of attribute model.



8
9
10
# File 'lib/credentials.rb', line 8

def model
  @model
end

#urlObject

Returns the value of attribute url.



8
9
10
# File 'lib/credentials.rb', line 8

def url
  @url
end

Instance Method Details

#initial_file_formatObject



39
40
41
42
43
44
45
# File 'lib/credentials.rb', line 39

def initial_file_format
  <<~CREDENTIALS
    URL=https://api.groq.com/openai/v1/chat/completions
    KEY=
    MODEL=llama3-8b-8192
  CREDENTIALS
end

#missing_config_message(message) ⇒ Object



35
36
37
# File 'lib/credentials.rb', line 35

def missing_config_message(message)
  "#{message} in the credentials file at #{credentials_file}"
end