Class: Cryptograpi::ConfigCredentials

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

Overview

Loads and reads a credential file or uses default info

Instance Method Summary collapse

Methods inherited from Info

#set_attrs

Constructor Details

#initialize(configuration_file, profile) ⇒ ConfigCredentials

Returns a new instance of ConfigCredentials.



28
29
30
31
32
33
34
35
# File 'lib/cryptograpi_ruby/credentials.rb', line 28

def initialize(configuration_file, profile)
  # Check if the file exists
  raise "There is an error finding or reading the #{configuration_file} file" if !configuration_file.nil? && !File.exist?(configuration_file)

  configuration_file = '~/.cryptograpi/credentials' if configuration_file.nil?

  @credentials = load_configuration_file(configuration_file, profile) if File.exist?(File.expand_path(configuration_file))
end

Instance Method Details

#attrsObject



37
38
39
# File 'lib/cryptograpi_ruby/credentials.rb', line 37

def attrs
  @credentials
end

#load_configuration_file(file, profile) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/cryptograpi_ruby/credentials.rb', line 41

def load_configuration_file(file, profile)
  config = ConfigParser.new(File.expand_path(file))

  # Dict for profiles
  p  = {}
  d  = {}

  # If there is a default profile, get it
  d = config['default'] if config['default'].present?

  d['SERVER'] = Cryptograpi::CRYPTOGRAPI_HOST unless d.key?('SERVER')

  # If there is a supplied profile, get it
  p = config[profile] if config[profile].present?

  # Use the supplied profile. Otherwise use default
  access_key_id = p.key?('ACCESS_KEY_ID') ? p['ACCESS_KEY_ID'] : d['ACCESS_KEY_ID']
  secret_access_key = p.key?('SECRET_ACCESS_KEY') ? p['SECRET_ACCESS_KEY'] : d['SECRET_ACCESS_KEY']
  signing_key = p.key?('SIGNING_KEY') ? p['SIGNING_KEY'] : d['SIGNING_KEY']
  host = p.key?('SERVER') ? p['SERVER'] : d['SERVER']

  # Sanitizing the host variable to always include https 
  host = "https://#{host}" if !host.include?('http://') && !host.include?('https://')

  Info.new(access_key_id, secret_access_key, signing_key, host).set_attrs
end