class SC2Cli::Shared::Cache

Attributes

base[R]
expires[R]
region[R]
token[R]

Public Class Methods

new(configuration:, region: nil) click to toggle source
# File lib/sc2cli/shared/cache.rb, line 39
def initialize(configuration:, region: nil)
  @region = region || configuration.region
  @base   = configuration.base

  @path = File.join(@base, "#{@@prefix}#{region.name}#{@@suffix}")

  load
end

Public Instance Methods

update(token:, expires:) click to toggle source
# File lib/sc2cli/shared/cache.rb, line 50
def update(token:, expires:)
  @token   = token
  @expires = expires

  save
end

Private Instance Methods

load() click to toggle source
# File lib/sc2cli/shared/cache.rb, line 63
def load
  token   = nil
  expires = nil

  if File.file?(@path)
    @@console.info("Reading cache: #{@path}")
    yaml = YAML.load(File.read(@path))

    if yaml.key?("token") then
      token = yaml["token"]

      @@console.fatal("Error in cache! 'token' must be a string!") unless token.kind_of?(String)
      @@console.fatal("Error in cache! 'token' must not be blank!") if token.empty?

      @@console.fatal("Error in cache! 'token' set but 'expires' missing!") unless yaml.key?("expires")

      expires = yaml["expires"]

      @@console.fatal("Error in cache! 'expires' must be a integer!") unless expires.kind_of?(Integer)
    end
  end

  @token   = token
  @expires = expires.kind_of?(Integer) ? Time.at(expires) : nil
end
save() click to toggle source
# File lib/sc2cli/shared/cache.rb, line 91
def save
  yaml = Hash.new

  yaml["token"]   = @token
  yaml["expires"] = @expires.to_i

  File.write(@path, yaml.to_yaml)
end