class RCreds::Fetcher

Attributes

default[R]
keys[R]

Public Class Methods

new(keys, default, environment) click to toggle source
# File lib/r_creds/fetcher.rb, line 5
def initialize(keys, default, environment)
  @keys = keys.map!(&:to_sym)
  @default = default
  @environment = environment
end

Public Instance Methods

call() click to toggle source
# File lib/r_creds/fetcher.rb, line 13
def call
  check_rails
  fetch
end

Private Instance Methods

blank?(cred) click to toggle source
# File lib/r_creds/fetcher.rb, line 40
def blank?(cred)
  cred.respond_to?(:empty?) ? !!cred.empty? : !cred
end
check_rails() click to toggle source
# File lib/r_creds/fetcher.rb, line 61
def check_rails
  return define_rails_strategy && true if defined?(::Rails)

  raise NoRailsError, 'RCreds works with 5.2 and above'
end
define_rails_strategy() click to toggle source
# File lib/r_creds/fetcher.rb, line 67
def define_rails_strategy
  @rails_version = Rails.version.to_i
end
environment() click to toggle source
# File lib/r_creds/fetcher.rb, line 20
def environment
  (presence?(@environment) || Rails.env).to_sym
end
fetch() click to toggle source
# File lib/r_creds/fetcher.rb, line 24
def fetch
  cred = send("fetch_rails_#{@rails_version}")

  presence?(cred) || presence?(ENV[keys.join('_').upcase]) || default
end
fetch_rails_5() click to toggle source
# File lib/r_creds/fetcher.rb, line 44
def fetch_rails_5
  Rails.application.credentials.dig(environment, *keys)
end
fetch_rails_6() click to toggle source
# File lib/r_creds/fetcher.rb, line 48
def fetch_rails_6
  if rails6_multi_env?
    puts "WARNING! Environment choice does not work in Rails 6. Fetching credentials for '#{Rails.env}'" if presence?(@environment)
    Rails.application.credentials.dig(*keys)
  else
    Rails.application.credentials.dig(environment, *keys)
  end
end
presence?(cred) click to toggle source
# File lib/r_creds/fetcher.rb, line 30
def presence?(cred)
  return nil if cred.nil?

  present?(cred) ? cred : false
end
present?(cred) click to toggle source
# File lib/r_creds/fetcher.rb, line 36
def present?(cred)
  !blank?(cred)
end
rails6_multi_env?() click to toggle source
# File lib/r_creds/fetcher.rb, line 57
def rails6_multi_env?
  Rails.application.credentials.content_path.basename.to_s != 'credentials.yml.enc'
end