Class: SecretKey

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_dir) ⇒ SecretKey

Returns a new instance of SecretKey.



7
8
9
10
11
12
# File 'lib/secret_key.rb', line 7

def initialize(base_dir)
  tmp = "#{base_dir}/tmp"
  tmp = File.expand_path(tmp)
  FileUtils.mkdir_p(tmp)
  @path = "#{tmp}/secret_key"
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



5
6
7
# File 'lib/secret_key.rb', line 5

def key
  @key
end

Instance Method Details

#readObject



14
15
16
17
18
19
20
21
22
# File 'lib/secret_key.rb', line 14

def read
  if @key
    @key
  elsif exists?
    @key = File.read(@path).strip
  else
    write
  end
end

#writeObject



24
25
26
27
28
29
30
31
# File 'lib/secret_key.rb', line 24

def write
  File.open(@path, 'w') do |f|
    @key = RUBY_PLATFORM + rand(10000000000000).to_s
    @key = Digest::SHA1.hexdigest(@key)
    f.write(@key)
  end
  @key
end