Class: Samus::Credentials

Inherits:
Object
  • Object
show all
Defined in:
lib/samus/credentials.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Credentials

Returns a new instance of Credentials.



11
12
13
14
# File 'lib/samus/credentials.rb', line 11

def initialize(name)
  @name = name
  load_credential_file
end

Class Attribute Details

.credentialsObject

Returns the value of attribute credentials.



6
7
8
# File 'lib/samus/credentials.rb', line 6

def credentials
  @credentials
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/samus/credentials.rb', line 3

def name
  @name
end

Instance Method Details

#loadObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/samus/credentials.rb', line 16

def load
  return self.class.credentials[name] if self.class.credentials[name]

  hsh = {}
  data = nil
  if File.executable?(@file)
    data = `#{@file}`
    if $?.to_i != 0
      Samus.error "Loading credential #{name} failed with #{$?}"
    end
  else
    data = File.read(@file)
  end

  data.split(/\r?\n/).each do |line|
    name, value = *line.strip.split(':')
    if value.nil?
      Samus.error "Failed to parse credential from #{@file} (exec bit: #{File.executable?(@file)})"
    end

    hsh["_creds_#{name.strip.downcase}"] = value.strip
  end

  self.class.credentials[name] = hsh
end