Class: Ubiq::ConfigCredentials

Inherits:
CredentialsInfo show all
Defined in:
lib/ubiq/credentials.rb

Overview

Class to load a credentials file or the default and read the credentials from either a supplied profile or use the default

Instance Method Summary collapse

Methods inherited from CredentialsInfo

#set_attributes

Constructor Details

#initialize(config_file, profile) ⇒ ConfigCredentials

Returns a new instance of ConfigCredentials.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ubiq/credentials.rb', line 32

def initialize(config_file, profile)
  # If config file is not found
  if !config_file.nil? && !File.exist?(config_file)
    raise RuntimeError, "Unable to open config file #{config_file} or contains missing values"
  end

  if config_file.nil?
    config_file = '~/.ubiq/credentials'
  end

  # If config file is found
  if File.exist?(File.expand_path(config_file))
    @creds = load_config_file(config_file, profile)
  end
end

Instance Method Details

#get_attributesObject



48
49
50
# File 'lib/ubiq/credentials.rb', line 48

def get_attributes
  return @creds
end

#load_config_file(file, profile) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ubiq/credentials.rb', line 52

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

  # Create empty dictionaries for the default and supplied profile
  p = {}
  d = {}

  # get the default profile if there is one
  if config['default'].present?
    d = config['default']
  end

  if !d.key?('SERVER')
    d['SERVER'] = Ubiq::UBIQ_HOST
  end

  # get the supplied profile if there is one
  if config[profile].present?
    p = config[profile]
  end

  # Use given profile if it is available, otherwise use default.
  access_key_id = p.key?('ACCESS_KEY_ID') ? p['ACCESS_KEY_ID'] : d['ACCESS_KEY_ID']
  secret_signing_key = p.key?('SECRET_SIGNING_KEY') ? p['SECRET_SIGNING_KEY'] : d['SECRET_SIGNING_KEY']
  secret_crypto_access_key = p.key?('SECRET_CRYPTO_ACCESS_KEY') ? p['SECRET_CRYPTO_ACCESS_KEY'] : d['SECRET_CRYPTO_ACCESS_KEY']
  host = p.key?('SERVER') ? p['SERVER'] : d['SERVER']

  # If the provided host does not contain http protocol then add to it
  if !host.include?('http://') && !host.include?('https://')
    host = 'https://' + host
  end

  return CredentialsInfo.new(access_key_id, secret_signing_key, secret_crypto_access_key, host).set_attributes
end