module Dotenv

Extended Modules

Defined in:

dotenv.cr
parser.cr

Instance Method Summary

Instance Method Detail

def build(env_vars : Hash(String, String), value_quotes : Quotes = Quotes::None) : String #

Builds a .env formatted string, and escape special characters in values.

Only variable key characters are validated.

require "dotenv"

Dotenv.build({"VAR" => "Hello"}) => "VAR=Hello"

[View source]
def build(io, env_vars : Hash(String, String), value_quotes : Quotes = Quotes::None) : Nil #

Builds a .env formatted data to the IO, and quotes to put around the value.

Only variable key characters are validated.

require "dotenv"

File.open "w", ".env" do |io|
  Dotenv.build(io, {"VAR" => "Hello"})
end

[View source]
def load(filename : Path | String = ".env", override_keys : Bool = false) : Hash(String, String) #

Loads environment variables from a file into the ENV constant.

require "dotenv"

File.write ".env-file", "VAR=Hello"
Dotenv.load ".env-file"    # => {"VAR" => "Hello"}
Dotenv.load ".absent-file" # => No such file or directory (File::NotFoundError)

[View source]
def load(io : IO, override_keys : Bool = false) : Hash(String, String) #

Loads environment variables from an IO into the ENV constant.

require "dotenv"

hash = Dotenv.load IO::Memory.new("VAR=Hello")
hash # => {"VAR" => "Hello"}

[View source]
def load(hash : Hash(String, String), override_keys : Bool = false) : Hash(String, String) #

Loads a Hash of environment variables into the ENV constant.

require "dotenv"

Dotenv.load({"VAR" => "test"})

[View source]
def load?(filename : Path | String = ".env", override_keys : Bool = false) : Hash(String, String) | Nil #

Loads environment variables from a file into the ENV constant if the file is present, else returns nil.

require "dotenv"

File.write ".env-file", "VAR=Hello"
Dotenv.load? ".env-file"    # => {"VAR" => "Hello"}
Dotenv.load? ".not-present" # => nil

[View source]
def load_string(env_vars : String, override_keys : Bool = false) : Hash(String, String) #

Loads environment variables from a String into the ENV constant.

require "dotenv"

hash = Dotenv.load_string "VAR=Hello"
hash # => {"VAR" => "Hello"}

[View source]
def parse(env_vars : String | IO) : Hash(String, String) #

Parses a .env formatted String/IO data, and returns a hash (without loading it to ENV).

require "dotenv"

hash = Dotenv.parse "VAR=Hello"
hash # => {"VAR" => "Hello"}

[View source]