Class: QingStor::SDK::Config

Inherits:
Hash
  • Object
show all
Defined in:
lib/qingstor/sdk/general/config.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(initial_config = {}) ⇒ Config

Returns a new instance of Config.



37
38
39
40
41
# File 'lib/qingstor/sdk/general/config.rb', line 37

def initialize(initial_config = {})
  self.connection = Net::HTTP::Persistent.new
  load_default_config
  update initial_config
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



27
28
29
# File 'lib/qingstor/sdk/general/config.rb', line 27

def connection
  @connection
end

Class Method Details

.init(access_key_id, secret_access_key) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/qingstor/sdk/general/config.rb', line 29

def self.init(access_key_id, secret_access_key)
  initial_config = {
    access_key_id:     access_key_id,
    secret_access_key: secret_access_key,
  }
  Config.new(initial_config)
end

Instance Method Details

#checkObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/qingstor/sdk/general/config.rb', line 49

def check
  %i(access_key_id secret_access_key host port protocol).each do |x|
    if !self[x] || self[x].to_s.empty?
      raise ConfigurationError, "#{x.to_sym} not specified"
    end
  end
  if self[:additional_user_agent] && !self[:additional_user_agent].empty?
    self[:additional_user_agent].each_byte do |x|
      # Allow space(32) to ~(126) in ASCII Table, exclude "(34).
      if x < 32 || x > 126 || x == 32 || x == 34
        raise ConfigurationError, 'additional User-Agent contains characters that not allowed'
      end
    end
  end
  self
end

#load_config_from_file(path) ⇒ Object



75
76
77
78
# File 'lib/qingstor/sdk/general/config.rb', line 75

def load_config_from_file(path)
  path = path.sub '~', Dir.home if path.start_with? '~/'
  update YAML.load_file File.absolute_path path
end

#load_default_configObject



66
67
68
# File 'lib/qingstor/sdk/general/config.rb', line 66

def load_default_config
  load_config_from_file Contract::DEFAULT_CONFIG_FILEPATH
end

#load_user_configObject



70
71
72
73
# File 'lib/qingstor/sdk/general/config.rb', line 70

def load_user_config
  install_default_user_config unless File.exist? Contract::USER_CONFIG_FILEPATH
  load_config_from_file Contract::USER_CONFIG_FILEPATH
end

#update(another_config = {}) ⇒ Object



43
44
45
46
47
# File 'lib/qingstor/sdk/general/config.rb', line 43

def update(another_config = {})
  deep_merge! another_config.deep_symbolize_keys!
  Logger.set_level self[:log_level]
  self
end