Module: DotenvValidator
- Defined in:
- lib/dotenv_validator.rb,
lib/dotenv_validator/errors.rb,
lib/dotenv_validator/version.rb
Overview
:nodoc: all
Defined Under Namespace
Classes: SampleFileNotFoundError
Constant Summary collapse
- VERSION =
"1.2.0"
Class Method Summary collapse
-
.analyze_variables ⇒ [String]
It analyzes the current environment and it compares it to the documentation present in .env.sample.
-
.boolean?(string) ⇒ Boolean
It checks the value to check if it is a boolean or not.
-
.check ⇒ Boolean
It checks the current environment and it returns a boolean value.
-
.check! ⇒ Object
It checks the current environment and it raises a runtime exception.
-
.email?(string) ⇒ Boolean
It checks the value to check if it is an email or not.
-
.float?(string) ⇒ Boolean
It checks the value to check if it is a float or not.
-
.integer?(string) ⇒ Boolean
It checks the value to check if it is an integer or not.
- .open_sample_file ⇒ Object
-
.root ⇒ Object
Internal:
Rails.rootis nil in Rails 4.1 before the application is initialized, so this falls back to theRAILS_ROOTenvironment variable, or the current working directory. - .sample_file ⇒ Object
-
.url?(string) ⇒ Boolean
It checks the value to check if it is a URL or not.
-
.uuid?(string) ⇒ Boolean
It checks the value to check if it is a uuid or not.
Class Method Details
.analyze_variables ⇒ [String]
It analyzes the current environment and it compares it to the documentation present in .env.sample.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/dotenv_validator.rb', line 14 def self.analyze_variables return [], [] unless File.exist?(sample_file) missing_variables = [] invalid_format = [] open_sample_file.each do |line| variable, config = line.split(" #") variable_name, _sample = variable.split("=") value = ENV[variable_name] if value.nil? || value.blank? missing_variables << variable_name if config.to_s.match?(/required/) next end next unless config =~ /format=(.*)/ valid = case Regexp.last_match(1) when "int", "integer" then integer?(value) when "float" then float?(value) when "str", "string" then true when "email" then email?(value) when "url" then url?(value) when "bool", "boolean" then boolean?(value) when "uuid" then uuid?(value) else value.match?(Regexp.new(Regexp.last_match(1))) end invalid_format << variable_name unless valid end [missing_variables, invalid_format] end |
.boolean?(string) ⇒ Boolean
It checks the value to check if it is a boolean or not.
122 123 124 |
# File 'lib/dotenv_validator.rb', line 122 def self.boolean?(string) string.match?(/(true|false)/) end |
.check ⇒ Boolean
It checks the current environment and it returns a boolean value.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/dotenv_validator.rb', line 54 def self.check result = true missing_variables, invalid_format = analyze_variables if missing_variables.any? puts("WARNING - Missing environment variables: #{missing_variables.join(", ")}") result = false end if invalid_format.any? puts("WARNING - Environment variables with invalid format: #{invalid_format.join(", ")}") result = false end result end |
.check! ⇒ Object
It checks the current environment and it raises a runtime exception.
74 75 76 77 78 79 80 |
# File 'lib/dotenv_validator.rb', line 74 def self.check! missing_variables, invalid_format = analyze_variables raise("Missing environment variables: #{missing_variables.join(", ")}") if missing_variables.any? raise("Environment variables with invalid format: #{invalid_format.join(", ")}") if invalid_format.any? end |
.email?(string) ⇒ Boolean
It checks the value to check if it is an email or not.
106 107 108 |
# File 'lib/dotenv_validator.rb', line 106 def self.email?(string) string.match?(URI::MailTo::EMAIL_REGEXP) end |
.float?(string) ⇒ Boolean
It checks the value to check if it is a float or not.
86 87 88 89 90 |
# File 'lib/dotenv_validator.rb', line 86 def self.float?(string) true if Float(string) rescue false end |
.integer?(string) ⇒ Boolean
It checks the value to check if it is an integer or not.
96 97 98 99 100 |
# File 'lib/dotenv_validator.rb', line 96 def self.integer?(string) true if Integer(string) rescue false end |
.open_sample_file ⇒ Object
135 136 137 138 139 |
# File 'lib/dotenv_validator.rb', line 135 def self.open_sample_file File.open(sample_file) rescue Errno::ENOENT raise DotenvValidator::SampleFileNotFoundError, "#{sample_file} was not found!" end |
.root ⇒ Object
Internal: Rails.root is nil in Rails 4.1 before the application is initialized, so this falls back to the RAILS_ROOT environment variable, or the current working directory.
Taken from Dotenv source code.
150 151 152 153 154 155 156 157 158 |
# File 'lib/dotenv_validator.rb', line 150 def self.root root_or_pwd = Pathname.new(ENV["RAILS_ROOT"] || Dir.pwd) if defined?(Rails) Rails.root || root_or_pwd else root_or_pwd end end |
.sample_file ⇒ Object
141 142 143 |
# File 'lib/dotenv_validator.rb', line 141 def self.sample_file File.join(root, ".env.sample") end |
.url?(string) ⇒ Boolean
It checks the value to check if it is a URL or not.
114 115 116 |
# File 'lib/dotenv_validator.rb', line 114 def self.url?(string) string.match?(/\A#{URI::DEFAULT_PARSER.make_regexp(%w[http https])}\z/) end |
.uuid?(string) ⇒ Boolean
It checks the value to check if it is a uuid or not.
130 131 132 133 |
# File 'lib/dotenv_validator.rb', line 130 def self.uuid?(string) string.match?(/\A[\da-f]{32}\z/i) || string.match?(/\A[\da-f]{8}-([\da-f]{4}-){3}[\da-f]{12}\z/i) end |