class

Poncho::Parser

Inherits Reference < Object

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 becomes DB_NAME, DB_NAME becomes DB_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"

Constructors

new(raw : String | IO)
Source

Class methods

from_file(file : String, overwrite = false)
Source

Instance methods

parse(overwrite = false)

Parse environment variables

Source
parse!

Parse environment variables and overwrite existing ones.

Same as #parse(overwrite: true)

Source
to_h

Returns this collection as a plain Hash.

Source

Macros

method_missing(call)
Source