Module: Whoosh::EnvLoader

Defined in:
lib/whoosh/env_loader.rb

Class Method Summary collapse

Class Method Details

.dotenv_available?Boolean

Returns:

  • (Boolean)


39
40
41
42
43
44
# File 'lib/whoosh/env_loader.rb', line 39

def self.dotenv_available?
  require "dotenv"
  true
rescue LoadError
  false
end

.load(root) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/whoosh/env_loader.rb', line 6

def self.load(root)
  path = File.join(root, ".env")
  return unless File.exist?(path)

  if dotenv_available?
    require "dotenv"
    Dotenv.load(path)
    return
  end

  parse(File.read(path)).each do |key, value|
    ENV[key] ||= value
  end
end

.parse(content) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/whoosh/env_loader.rb', line 21

def self.parse(content)
  pairs = {}
  content.each_line do |line|
    line = line.strip
    next if line.empty? || line.start_with?("#")
    key, value = line.split("=", 2)
    next unless key && value
    key = key.strip
    value = value.strip
    if (value.start_with?('"') && value.end_with?('"')) ||
       (value.start_with?("'") && value.end_with?("'"))
      value = value[1..-2]
    end
    pairs[key] = value
  end
  pairs
end