class StreetCreds::CredFile

Public Class Methods

decrypt_existing_file(filepath:) click to toggle source
# File lib/streetcreds/cred_file.rb, line 9
def self.decrypt_existing_file(filepath:)
  filepath           = full_path(filepath)
  decrypted_contents = new(filepath: filepath).decrypt_file
  File.open(filepath, 'w+') { |f| f.write(decrypted_contents) }
end
encrypt_existing_file(filepath:) click to toggle source
# File lib/streetcreds/cred_file.rb, line 3
def self.encrypt_existing_file(filepath:)
  filepath = full_path(filepath)
  hash     = YAML.load(File.open(filepath))
  new(filepath: filepath, hash: hash)
end
full_path(filepath) click to toggle source
# File lib/streetcreds/cred_file.rb, line 74
def self.full_path(filepath)
  Pathname.new(filepath).expand_path.to_s
end
new(filepath:, hash: nil, convert_on_valid: false, password: nil) click to toggle source
# File lib/streetcreds/cred_file.rb, line 15
def initialize(filepath:, hash: nil, convert_on_valid: false, password: nil)
  @filepath = self.class.full_path(filepath)
  @password = password
  load_file(hash: hash, convert_on_valid: convert_on_valid)
end
symbolize_keys(hash) click to toggle source

Adapted from stackoverflow.com/a/8379653/5637619

# File lib/streetcreds/cred_file.rb, line 79
def self.symbolize_keys(hash)
  Hash[hash.map do |k, v|
    [k.to_sym, (v.is_a?(Hash) ? symbolize_keys(v) : v)]
  end]
end

Public Instance Methods

ask_password() click to toggle source
# File lib/streetcreds/cred_file.rb, line 57
def ask_password
  return @password if @password
  print "Password?\n> "
  @password = $stdin.noecho(&:gets).chomp
  puts ''
  @password
end
decrypt_file(contents: nil, password: nil) click to toggle source
# File lib/streetcreds/cred_file.rb, line 35
def decrypt_file(contents: nil, password: nil)
  contents = File.read(@filepath) unless contents
  password = ask_password if password.nil?
  contents.decrypt(:symmetric, :password => password)
end
encrypt_file(contents:, password: nil) click to toggle source
# File lib/streetcreds/cred_file.rb, line 52
def encrypt_file(contents:, password: nil)
  password = ask_password if password.nil?
  contents.encrypt(:symmetric, :password => password)
end
inspect() click to toggle source
# File lib/streetcreds/cred_file.rb, line 65
def inspect
  @hash.inspect
end
load_file(convert_on_valid:, hash: nil) click to toggle source
# File lib/streetcreds/cred_file.rb, line 21
def load_file(convert_on_valid:, hash: nil)
  if hash.nil?
    begin
      file_contents = File.read(@filepath)
      return @hash = {} if file_contents.empty?
      @hash = YAMLHelper.load_if_valid(file_contents) if convert_on_valid
      @hash = YAMLHelper.load_if_valid(decrypt_file) unless @hash
    rescue Errno::ENOENT
      @hash = {}
    end
  end
  @hash = self.class.symbolize_keys(@hash)
end
method_missing(meth, *args) click to toggle source

Act like a hash

# File lib/streetcreds/cred_file.rb, line 70
def method_missing(meth, *args)
  @hash.send(meth, *args)
end
save() click to toggle source
# File lib/streetcreds/cred_file.rb, line 41
def save
  FileUtils.mkdir_p(File.dirname(@filepath))
  yaml = self.class.symbolize_keys(@hash).to_yaml
  if yaml.empty?
    File.delete(@filepath)
  else
    encrypted_yaml = encrypt_file(contents: yaml)
    File.open(@filepath, 'w+') { |f| f.write(encrypted_yaml) }
  end
end