Class: DomoscioRails::AuthorizationToken::FileStorage

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

Instance Method Summary collapse

Constructor Details

#initialize(temp_dir = nil) ⇒ FileStorage



46
47
48
49
50
51
# File 'lib/domoscio_rails/authorization_token.rb', line 46

def initialize(temp_dir = nil)
  @temp_dir = temp_dir || DomoscioRails.configuration.temp_dir
  if !@temp_dir
    raise "Path to temporary folder is not defined"
  end
end

Instance Method Details

#file_pathObject



74
75
76
# File 'lib/domoscio_rails/authorization_token.rb', line 74

def file_path
  File.join(@temp_dir, "DomoscioRails.AuthorizationToken.FileStore.tmp")
end

#getObject



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/domoscio_rails/authorization_token.rb', line 53

def get
  begin
    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
end

#store(token) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/domoscio_rails/authorization_token.rb', line 65

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