Module: Environment

Defined in:
lib/environment.rb

Overview

Environment

Environment variables in are not available to scripts running as crons and/or services. This gem allows ruby scripts running as a cron and/or as a service full access to the system environment variables.

Installation

# gem install environment

Usage

The global constant ENV is left untouched by default. To read an environment variable, use Environment.get or Environment[].

Example usage:

require "rubygems"
require "environment"

# the following two are the same
path = Environment['PATH']
home = Environment.get('HOME')

If you want to override the default ENV constant, use the override! method

Example usage:

require "rubygems"
require "environment"

host = ENV['HOSTNAME'] #nil
Environment.override!
host = ENV['HOSTNAME'] #yourdomain.com

Constant Summary collapse

CURRENT_DIR =
File.dirname(__FILE__)
SHELL_SCRIPT =
"#{CURRENT_DIR}/save_environment_variables.sh"
VARIABLE_DUMP =
"/tmp/.environment_variables"

Class Method Summary collapse

Class Method Details

.[](key) ⇒ Object



55
56
57
# File 'lib/environment.rb', line 55

def self.[](key)
  return @env[key]
end

.get(key) ⇒ Object



51
52
53
# File 'lib/environment.rb', line 51

def self.get(key)
  return @env[key]
end

.loadObject



40
41
42
43
44
45
46
47
48
49
# File 'lib/environment.rb', line 40

def self.load
  success = system("bash -l #{SHELL_SCRIPT}")
  if success && File.exists?(VARIABLE_DUMP)
    File.read(VARIABLE_DUMP).each_line do |line|
      key,value = line.chomp.split("=")
      @env[key] = value
    end
    File.delete(VARIABLE_DUMP)
  end
end

.override!Object



59
60
61
62
63
# File 'lib/environment.rb', line 59

def self.override!
  @env.each do |key,value|
    ENV[key] = value
  end
end