Class: StreetCred::CredFile

Inherits:
Object
  • Object
show all
Defined in:
lib/streetcreds/cred_file.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filepath:, hash: nil, convert_on_valid: false) ⇒ CredFile

Returns a new instance of CredFile.



15
16
17
18
# File 'lib/streetcreds/cred_file.rb', line 15

def initialize(filepath:, hash: nil, convert_on_valid: false)
  @filepath = self.class.full_path(filepath)
  load_file(hash: hash, convert_on_valid: convert_on_valid)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object

Act like a hash



69
70
71
# File 'lib/streetcreds/cred_file.rb', line 69

def method_missing(meth, *args)
  @hash.send(meth, *args)
end

Class Method Details

.decrypt_existing_file(filepath:) ⇒ Object



9
10
11
12
13
# 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:) ⇒ Object



3
4
5
6
7
# 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) ⇒ Object



73
74
75
# File 'lib/streetcreds/cred_file.rb', line 73

def self.full_path(filepath)
  Pathname.new(filepath).expand_path.to_s
end

.symbolize_keys(hash) ⇒ Object



78
79
80
81
82
# File 'lib/streetcreds/cred_file.rb', line 78

def self.symbolize_keys(hash)
  Hash[hash.map do |k, v|
    [k.to_sym, (v.is_a?(Hash) ? symbolize_keys(v) : v)]
  end]
end

Instance Method Details

#ask_passwordObject



56
57
58
59
60
61
62
# File 'lib/streetcreds/cred_file.rb', line 56

def ask_password
  return @password if @password
  print "Password?\n> "
  @password = $stdin.noecho(&:gets).chomp
  puts ''
  @password
end

#decrypt_file(contents: nil, password: nil) ⇒ Object



34
35
36
37
38
# File 'lib/streetcreds/cred_file.rb', line 34

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) ⇒ Object



51
52
53
54
# File 'lib/streetcreds/cred_file.rb', line 51

def encrypt_file(contents:, password: nil)
  password = ask_password if password.nil?
  contents.encrypt(:symmetric, :password => password)
end

#inspectObject



64
65
66
# File 'lib/streetcreds/cred_file.rb', line 64

def inspect
  @hash.inspect
end

#load_file(convert_on_valid:, hash: nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/streetcreds/cred_file.rb', line 20

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

#saveObject



40
41
42
43
44
45
46
47
48
49
# File 'lib/streetcreds/cred_file.rb', line 40

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