Class: Wsaa::CredentialStore
- Inherits:
-
Object
- Object
- Wsaa::CredentialStore
- Defined in:
- lib/wsaa/credential_store.rb
Overview
File-based cache for WSAA credentials.
Stores credentials as YAML files with date-based filenames.
Instance Attribute Summary collapse
-
#cache_dir ⇒ String
readonly
Directory where cache files are stored.
-
#service ⇒ String
readonly
The service name used in the cache filename.
Instance Method Summary collapse
-
#cache_file_path ⇒ String
Returns the full path to the cache file.
-
#clear ⇒ Object
Deletes the cache file.
-
#initialize(cache_dir:, service:) ⇒ CredentialStore
constructor
Creates a new CredentialStore.
-
#read ⇒ Credentials?
Reads cached credentials.
-
#write(credentials) ⇒ Credentials
Writes credentials to the cache.
Constructor Details
#initialize(cache_dir:, service:) ⇒ CredentialStore
Creates a new CredentialStore.
20 21 22 23 |
# File 'lib/wsaa/credential_store.rb', line 20 def initialize(cache_dir:, service:) @cache_dir = cache_dir @service = service end |
Instance Attribute Details
#cache_dir ⇒ String (readonly)
Directory where cache files are stored.
12 13 14 |
# File 'lib/wsaa/credential_store.rb', line 12 def cache_dir @cache_dir end |
#service ⇒ String (readonly)
The service name used in the cache filename.
12 13 14 |
# File 'lib/wsaa/credential_store.rb', line 12 def service @service end |
Instance Method Details
#cache_file_path ⇒ String
Returns the full path to the cache file.
66 67 68 |
# File 'lib/wsaa/credential_store.rb', line 66 def cache_file_path File.join(cache_dir, cache_filename) end |
#clear ⇒ Object
Deletes the cache file.
58 59 60 |
# File 'lib/wsaa/credential_store.rb', line 58 def clear File.delete(cache_file_path) if File.exist?(cache_file_path) end |
#read ⇒ Credentials?
Reads cached credentials.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/wsaa/credential_store.rb', line 29 def read return nil unless File.exist?(cache_file_path) data = YAML.load_file(cache_file_path) expiration_time = parse_expiration_time(data['expiration_time']) credentials = Credentials.new( token: data['token'], sign: data['sign'], expiration_time: expiration_time ) credentials.valid? ? credentials : nil rescue StandardError nil end |
#write(credentials) ⇒ Credentials
Writes credentials to the cache.
51 52 53 54 |
# File 'lib/wsaa/credential_store.rb', line 51 def write(credentials) File.write(cache_file_path, YAML.dump(credentials.to_h.transform_keys(&:to_s))) credentials end |