Sha256: f4b351bc6489b1f84338169626939bd01481cb2f157a96762c4d6d963f9ec8e2

Contents?: true

Size: 1.59 KB

Versions: 5

Compression:

Stored size: 1.59 KB

Contents

require "env_json/version"
require "json"

# Module containing utilities for loading a JSON file into environments variables. see {EnvJson#load_env_from_source_with_overrides}
module EnvJson
  module_function

  # Load environments variables from a source into ENV
  #
  # @param json [#read] any source that can be read by JSON.load
  # @param environment_name [#to_str, nil] the environment name from where to take overrides
  # @return [Hash] the loaded environment variables
  def load_env_from_source_with_overrides(json, environment_name=nil)
    json = JSON.load(json)
    @env = generate_configuration(json, environment_name).tap do |configuration|
      apply_env(configuration)
    end
  end

  # return the consolidated env built by {EnvJson#load_env_from_source_with_overrides}
  # @return [Hash] the environment variables loaded from JSON
  def env
    @env or fail 'call load_env_from_source_with_overrides first'
  end

  # @!visibility private
  def generate_configuration(raw_hash, environment_name=nil, configuration={})
    raw_hash.inject(configuration) do |conf, (key,value)|
      next conf if key =~ /^_/

      if key == environment_name
        conf = generate_configuration(value, environment_name, conf)
      elsif not value.is_a? String and key =~ /^(test|development|staging|production)/
        # skip other environments keys
        next conf
      else
        conf[key] = value
      end
      conf
    end
  end

  # @!visibility private
  def apply_env(configuration)
    configuration.each do |key,value|
      ENV[key] ||= value
    end
  end
end

require 'env_json/railtie' if defined?(Rails)

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
env_json-0.3.2 lib/env_json.rb
env_json-0.3.0 lib/env_json.rb
env_json-0.2.2 lib/env_json.rb
env_json-0.2.1 lib/env_json.rb
env_json-0.2.0 lib/env_json.rb