Class: Fog::Backblaze::TokenCache

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/backblaze/token_cache.rb

Direct Known Subclasses

FileTokenCache, NullTokenCache

Defined Under Namespace

Classes: FileTokenCache, NullTokenCache

Constant Summary collapse

TTLS =
{
  auth_response: 3600 * 24,
  buckets: 3600 * 24,
  upload_url: 3600 * 24
}

Instance Method Summary collapse

Constructor Details

#initializeTokenCache

Returns a new instance of TokenCache.


29
30
31
# File 'lib/fog/backblaze/token_cache.rb', line 29

def initialize
  @data = load_data || {}
end

Instance Method Details

#access_part(name) ⇒ Object


78
79
80
81
82
83
# File 'lib/fog/backblaze/token_cache.rb', line 78

def access_part(name)
  name = name.to_s
  if @data[name] && ::DateTime.parse(@data[name]['expires_at']).to_time > ::Time.now
    @data[name]['value']
  end
end

#auth_responseObject


56
57
58
# File 'lib/fog/backblaze/token_cache.rb', line 56

def auth_response
  access_part(:auth_response)
end

#auth_response=(value) ⇒ Object


60
61
62
63
# File 'lib/fog/backblaze/token_cache.rb', line 60

def auth_response=(value)
  write_part(:auth_response, value, 3600 * 24)
  save_data
end

#bucketsObject


65
66
67
# File 'lib/fog/backblaze/token_cache.rb', line 65

def buckets
  access_part(:buckets)
end

#buckets=(value) ⇒ Object


69
70
71
72
# File 'lib/fog/backblaze/token_cache.rb', line 69

def buckets=(value)
  write_part(:buckets, value, 3600 * 24)
  save_data
end

#fetch(field) ⇒ Object


45
46
47
48
49
50
51
52
53
54
# File 'lib/fog/backblaze/token_cache.rb', line 45

def fetch(field)
  if result = access_part(field)
    result
  else
    result = yield
    write_part(field, result, TTLS[field])
    save_data
    result
  end
end

#load_dataObject


33
34
# File 'lib/fog/backblaze/token_cache.rb', line 33

def load_data
end

#resetObject


98
99
100
101
# File 'lib/fog/backblaze/token_cache.rb', line 98

def reset
  @data = {}
  save_data
end

#save_dataObject


36
37
# File 'lib/fog/backblaze/token_cache.rb', line 36

def save_data
end

#upload_urlObject


74
75
76
# File 'lib/fog/backblaze/token_cache.rb', line 74

def upload_url
  
end

#write_part(name, value, ttl = 3600) ⇒ Object


85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/fog/backblaze/token_cache.rb', line 85

def write_part(name, value, ttl = 3600)
  ttl = 3600 if ttl.nil?
  name = name.to_s
  if value.nil?
    @data.delete(name)
  else
    @data[name] = {
      'value' => value,
      'expires_at' => ::Time.at(::Time.now + ttl - 1).to_s
    }
  end
end