Class: JsonToEnv

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

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.load(json) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/json_to_env.rb', line 4

def self.load(json)
  hash = JSON.parse json
  paths = hash.keys.map { |key| [key] }
  envs = {}

  while path = paths.shift
    value = hash
    path.each { |step| value = value[step] }

    if value.respond_to?(:keys)
      value.keys.each { |key| paths << path + [key] }
    else
      envs[path.join("_").upcase] = value
    end
  end

  envs.each { |k,v| ENV[k] = v unless ENV.has_key? k }
end

.load_file(file_path) ⇒ Object



23
24
25
26
27
# File 'lib/json_to_env.rb', line 23

def self.load_file(file_path)
  if File.exist? file_path
    self.load File.read(file_path)
  end
end