Class: Fnsapi::TmpStorage

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

Instance Method Summary collapse

Constructor Details

#initializeTmpStorage

Returns a new instance of TmpStorage.



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

def initialize
  @file = File.open(file_path, 'a+')
end

Instance Method Details

#tokenObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/fnsapi/tmp_storage.rb', line 15

def token
  data = JSON.parse(@file.read)
  expired_at = Time.parse(data['expire_at'])

  if expired_at < Time.now
    @file.truncate(0)
    return
  end

  data['token']
rescue JSON::ParserError
  @file.truncate(0)
  nil
end

#write_token(token, expire_at) ⇒ Object



9
10
11
12
13
# File 'lib/fnsapi/tmp_storage.rb', line 9

def write_token(token, expire_at)
  @file.truncate(0)
  @file.write({ token: token, expire_at: expire_at }.to_json)
  @file.rewind
end