Class: DomoscioAdmin::AuthorizationToken::FileStorage

Inherits:
Object
  • Object
show all
Defined in:
lib/domoscio_admin/authorization_token.rb

Overview

Manages tokens with local File

Instance Method Summary collapse

Constructor Details

#initializeFileStorage

Returns a new instance of FileStorage.



35
36
37
# File 'lib/domoscio_admin/authorization_token.rb', line 35

def initialize
  raise 'Path to temporary folder is not defined' unless DomoscioAdmin.configuration.temp_dir
end

Instance Method Details

#file_pathObject



58
59
60
# File 'lib/domoscio_admin/authorization_token.rb', line 58

def file_path
  File.join(DomoscioAdmin.configuration.temp_dir, 'DomoscioAdmin.AuthorizationToken.FileStore.tmp')
end

#getObject



39
40
41
42
43
44
45
46
47
# File 'lib/domoscio_admin/authorization_token.rb', line 39

def get
  f = File.open(file_path, File::RDONLY)
  f.flock(File::LOCK_SH)
  txt = f.read
  f.close
  YAML.load(txt) || nil
rescue Errno::ENOENT
  nil
end

#store(token) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/domoscio_admin/authorization_token.rb', line 49

def store(token)
  File.open(file_path, File::RDWR|File::CREAT, 0o644) do |f|
    f.flock(File::LOCK_EX)
    f.truncate(0)
    f.rewind
    f.puts(YAML.dump(token))
  end
end