Method: IBMSmartCloud#initialize

Defined in:
lib/smartcloud.rb

#initialize(opts = {}) ⇒ IBMSmartCloud

Returns a new instance of IBMSmartCloud.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/smartcloud.rb', line 37

def initialize(opts={})
  @save_response = opts[:save_response]
  @simulated_response = opts[:simulated_response_file] && File.read(opts[:simulated_response_file])

  # For handling errors
  @retries = (opts[:retries] || 120).to_i
  @sleep_interval = (opts[:sleep_interval] || 30).to_i

  @username = opts[:username] || ENV['SMARTCLOUD_USERNAME'] || raise(RuntimeError, "Please specify username in an option or as ENV variable SMARTCLOUD_USERNAME")
  @password = opts[:password]|| ENV['SMARTCLOUD_PASSWORD'] || raise(RuntimeError, "Please specify password in an option or as ENV variable SMARTCLOUD_PASSWORD")
  @logger = opts[:logger] || SmartcloudLogger.new(STDOUT)
  @debug = opts[:debug] || false

  @config = YAML.load_file(File.join(File.dirname(__FILE__), "config/config.yml"))
  @states = @config["states"]

  @api_url = (opts[:api_url] || @config["api_url"]).to_s.dup # gotta dup it because the option string is frozen
  @api_url.gsub!("https://", "https://#{CGI::escape(@username)}:#{CGI::escape(@password)}@")

  @http_client = Kernel.const_get(@config["http_client"])
  @http_client.timeout = 120 # ibm requests can be very slow
  @http_client.log = @logger if @debug
end