class DnsMadeEasy::Credentials::YamlFile

Attributes

account[RW]
filename[RW]
mash[RW]

Public Class Methods

new(file: default_credentials_path) click to toggle source
# File lib/dnsmadeeasy/credentials/yaml_file.rb, line 14
def initialize(file: default_credentials_path)
  self.filename = file
  parse! if exist?
end

Public Instance Methods

keys(account: nil, encryption_key: nil) click to toggle source
# File lib/dnsmadeeasy/credentials/yaml_file.rb, line 19
def keys(account: nil, encryption_key: nil)
  return nil unless exist?
  return nil if mash.nil?

  creds = if mash.accounts.is_a?(Array)
            credentials_from_array(mash.accounts, account&.to_s)

          elsif mash.credentials
            mash.credentials

          else
            raise DnsMadeEasy::InvalidCredentialsFormatError,
                  'expected either "accounts" or "credentials" as the top-level key'
          end

  return nil unless creds

  ApiKeys.new creds.api_key,
              creds.api_secret,
              encryption_key || creds.encryption_key
end
to_s() click to toggle source
# File lib/dnsmadeeasy/credentials/yaml_file.rb, line 41
def to_s
  "file #{filename}"
end

Private Instance Methods

contents() click to toggle source
# File lib/dnsmadeeasy/credentials/yaml_file.rb, line 77
def contents
  ::File.read(filename)
end
credentials_from_array(accounts, account_name = nil) click to toggle source
# File lib/dnsmadeeasy/credentials/yaml_file.rb, line 47
def credentials_from_array(accounts, account_name = nil)
  account = if account_name
              accounts.find { |a| a.name == account_name }
            elsif accounts.size == 1
              accounts.first
            else
              accounts.find(&:default_account)
            end

  unless account
    raise DnsMadeEasy::APIKeyAndSecretMissingError,
          (account ? "account #{account} was not found" : 'Default account does not exist')
  end

  unless account.credentials
    raise DnsMadeEasy::InvalidCredentialsFormatError,
          'Expected an account entry to have the "credentials" key'
  end

  account.credentials
end
exist?() click to toggle source
# File lib/dnsmadeeasy/credentials/yaml_file.rb, line 73
def exist?
  ::File.exist?(filename)
end
load_hash() click to toggle source
# File lib/dnsmadeeasy/credentials/yaml_file.rb, line 81
def load_hash
  Hashie::Mash.new(YAML.safe_load(contents))
end
parse!() click to toggle source
# File lib/dnsmadeeasy/credentials/yaml_file.rb, line 69
def parse!
  self.mash = Hashie::Extensions::SymbolizeKeys.symbolize_keys(load_hash)
end