class Poncho::Parser
- Poncho::Parser
- Reference
- Object
Overview
Poncho::Parser
is a .env file parser
Poncho parses the contents of your file containing environment variables is available to use.
It accepts a String or IO and will return an Hash
with the parsed keys and values.
Rules
Poncho parser currently supports the following rules:
- Skipped the empty line and comment(
#
). - Ignore the comment which after (
#
). ENV=development
becomes{"ENV" => "development"}
.- Converts camelcase boundaries to underscores and upcase the key:
dbName
becomesDB_NAME
,DB_NAME
becomesDB_NAME
- Support variables in value.
$NAME
or${NAME}
. - Whitespace is removed from both ends of the value.
NAME = foo
becomes`{"NAME" => "foo"} - New lines are expanded if in double quotes.
MULTILINE="new\nline"
becomes `{"MULTILINE" => "new\nline"} - Inner quotes are maintained (such like
Hash
/JSON
).JSON={"foo":"bar"}
becomes `{"JSON" => "{"foo":"bar"}"} - Empty values become empty strings.
- Single and double quoted values are escaped.
- Overwrite optional (default is non-overwrite).
- Support variables in value.
WELCOME="hello $NAME"
becomes{"WELCOME" => "hello foo"}
Overrides
By default, Poncho won't overwrite existing environment variables as dotenv assumes the deployment environment
has more knowledge about configuration than the application does.
To overwrite existing environment variables you can use Poncho.parse!(string_or_io)
/
Poncho.from_file(file, overwrite: true)
and Poncho.parse(string_or_io, overwrite: true)
.
Parse file
parser = Poncho::Parser.from_file(".env")
parser["ENV"] # => "development"
Parse raw string
parser = Poncho::Parser.new("ENV=development\nDB_NAME=poncho\nENV=production")
parser.parse
parser["ENV"] # => "development"
# Overwrite the key
parser.parse!
parser["ENV"] # => "production"
Defined in:
poncho/parser.crConstructors
Class Method Summary
Macro Summary
Instance Method Summary
-
#parse(overwrite = false)
Parse environment variables
-
#parse!
Parse environment variables and overwrite existing ones.
-
#to_h : Hash(String, String)
Returns this collection as a plain Hash.