Credentials or env

Curated ago by @pedroaugustorduarte

Description

A simple helper that selects some configuration from credentials or environment variable

1
2
3
4
5
6
7
8
9
10
11
# frozen_string_literal: true

module Credentials
  # Returns credentials or env
  # OBS: Does not support nested credentials
  # @example Credentials::credentials_or_env('LIBRARY_SERVICE_URL', default: 'http://localhost:30000')
  def self.credentials_or_env(string_key, default: nil)
    credentials_key = string_key.downcase.to_sym
    Rails.application.credentials.dig(credentials_key) || ENV.fetch(string_key, default)
  end
end