Class: Aws::ASMR::Cache

Inherits:
Struct
  • Object
show all
Defined in:
lib/aws/asmr/cache.rb

Constant Summary collapse

PATH =
"#{Aws::ASMR::ROOT}/cache"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#access_key_idObject

Returns the value of attribute access_key_id

Returns:

  • (Object)

    the current value of access_key_id



6
7
8
# File 'lib/aws/asmr/cache.rb', line 6

def access_key_id
  @access_key_id
end

#expirationObject

Returns the value of attribute expiration

Returns:

  • (Object)

    the current value of expiration



6
7
8
# File 'lib/aws/asmr/cache.rb', line 6

def expiration
  @expiration
end

#secret_access_keyObject

Returns the value of attribute secret_access_key

Returns:

  • (Object)

    the current value of secret_access_key



6
7
8
# File 'lib/aws/asmr/cache.rb', line 6

def secret_access_key
  @secret_access_key
end

#session_tokenObject

Returns the value of attribute session_token

Returns:

  • (Object)

    the current value of session_token



6
7
8
# File 'lib/aws/asmr/cache.rb', line 6

def session_token
  @session_token
end

Class Method Details

.baseObject



11
12
13
14
15
16
17
18
19
# File 'lib/aws/asmr/cache.rb', line 11

def base
  @base ||= begin
    if File.exist?(PATH)
      JSON.parse(File.read(PATH))
    else
      {}
    end
  end
end

.destroy!Object



32
33
34
# File 'lib/aws/asmr/cache.rb', line 32

def destroy!
  File.exist?(PATH) && File.delete(PATH)
end

.firstObject



21
22
23
24
# File 'lib/aws/asmr/cache.rb', line 21

def first
  k,v = base.first
  get(k)
end

.get(assume_role_arn) ⇒ Object



26
27
28
29
30
# File 'lib/aws/asmr/cache.rb', line 26

def get(assume_role_arn)
  cache = base[assume_role_arn] && new(base[assume_role_arn])
  return nil unless cache
  cache.expired? ? nil : cache
end

Instance Method Details

#expiration_timeObject



37
38
39
40
# File 'lib/aws/asmr/cache.rb', line 37

def expiration_time
  return nil unless expiration
  expiration.is_a?(Time) ? expiration : Time.parse(expiration)
end

#expired?Boolean

Returns:

  • (Boolean)


42
43
44
45
# File 'lib/aws/asmr/cache.rb', line 42

def expired?
  return true unless expiration_time
  expiration_time < (Time.now+60*5)
end

#save!(assume_role_arn) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/aws/asmr/cache.rb', line 47

def save!(assume_role_arn)
  FileUtils.mkdir_p(Pathname.new(PATH).dirname)
  self.class.base[assume_role_arn] = to_h
  File.open(PATH, 'w') do |f|
    f.puts(JSON.pretty_generate(self.class.base))
  end
end

#shell_variablesObject



55
56
57
58
59
60
61
# File 'lib/aws/asmr/cache.rb', line 55

def shell_variables
  {
    AWS_ACCESS_KEY_ID: access_key_id,
    AWS_SECRET_ACCESS_KEY: secret_access_key,
    AWS_SESSION_TOKEN: session_token,
  }.map{|k,v| "#{k}=#{v}"}
end