Module: DotenvValidator
- Defined in:
- lib/dotenv_validator.rb,
lib/dotenv_validator/version.rb
Overview
Knows how to check the environment variables and compares it to .env.sample and the comments in every line of your .env.sample file.
Constant Summary collapse
- VERSION =
'1.1.0'
Class Method Summary collapse
-
.analyze_variables ⇒ [String]
It analyzes the current environment and it compares it to the documentation present in .env.sample.
-
.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.
Class Method Details
.analyze_variables ⇒ [String]
It analyzes the current environment and it compares it to the documentation present in .env.sample.
11 12 13 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 |
# File 'lib/dotenv_validator.rb', line 11 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) else value.match?(Regexp.new(Regexp.last_match(1))) end invalid_format << variable_name unless valid end [missing_variables, invalid_format] end |
.check ⇒ Boolean
It checks the current environment and it returns a boolean value.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/dotenv_validator.rb', line 49 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.
69 70 71 72 73 74 75 |
# File 'lib/dotenv_validator.rb', line 69 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.
101 102 103 |
# File 'lib/dotenv_validator.rb', line 101 def self.email?(string) string.match?(/[\w@]+@[\w@]+\.[\w@]+/) end |
.float?(string) ⇒ Boolean
It checks the value to check if it is a float or not.
81 82 83 84 85 |
# File 'lib/dotenv_validator.rb', line 81 def self.float?(string) true if Float(string) rescue StandardError false end |
.integer?(string) ⇒ Boolean
It checks the value to check if it is an integer or not.
91 92 93 94 95 |
# File 'lib/dotenv_validator.rb', line 91 def self.integer?(string) true if Integer(string) rescue StandardError false end |
.open_sample_file ⇒ Object
113 114 115 |
# File 'lib/dotenv_validator.rb', line 113 def self.open_sample_file File.open(sample_file) 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.
126 127 128 129 130 131 132 133 134 |
# File 'lib/dotenv_validator.rb', line 126 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
117 118 119 |
# File 'lib/dotenv_validator.rb', line 117 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.
109 110 111 |
# File 'lib/dotenv_validator.rb', line 109 def self.url?(string) string.match?(%r{https?://.+}) end |