class Cloudcost::ApiToken

Attributes

profile[RW]
token[RW]

Public Class Methods

new(options = {}) click to toggle source
# File lib/cloudcost/api_token.rb, line 12
def initialize(options = {})
  @profile = options[:profile]
  @token = load
end

Public Instance Methods

get_from_profile() click to toggle source
# File lib/cloudcost/api_token.rb, line 29
def get_from_profile
  [
    "#{ENV["XDG_CONFIG_HOME"] || "#{ENV["HOME"]}/.config"}/cloudscale/cloudscale.ini",
    "#{ENV["HOME"]}/.cloudscale.ini",
    "#{ENV["PWD"]}/cloudscale.ini"
  ].each do |path|
    if File.exist? path
      config = ParseConfig.new(path)
      return config[@profile]["api_token"] if config.groups.include? @profile
    end
  end
  nil
end
load() click to toggle source
# File lib/cloudcost/api_token.rb, line 17
def load
  api_token = nil
  if @profile
    api_token = get_from_profile
    raise ProfileError, "profile \"#{@profile}\" not found" unless api_token
  else
    api_token = ENV["CLOUDSCALE_API_TOKEN"]
    raise TokenError, "no CLOUDSCALE_API_TOKEN found in environment" unless api_token
  end
  api_token
end