class Asperalm::OauthCache

Constants

TOKEN_FILE_PREFIX

definition of token cache filename

TOKEN_FILE_SEPARATOR
TOKEN_FILE_SUFFIX
WINDOWS_PROTECTED_CHAR

Public Class Methods

ids_to_id(parts) click to toggle source
# File lib/asperalm/oauth_cache.rb, line 36
def self.ids_to_id(parts)
  Log.dump("parts",parts)
  result=parts.
  join(TOKEN_FILE_SEPARATOR).
  gsub(WINDOWS_PROTECTED_CHAR,TOKEN_FILE_SEPARATOR). # remove windows forbidden chars
  gsub('.',TOKEN_FILE_SEPARATOR)  # keep dot for extension only (nicer)
  Log.log.debug("id=#{result}")
  raise "at least one non empty id required" if result.empty?
  return result
end
new() click to toggle source
# File lib/asperalm/oauth_cache.rb, line 13
def initialize
  # change this with persistency_folder
  @token_cache_folder='.'
  # key = string unique identifier
  # value = ruby structure of data of returned value
  @token_cache={}
end

Public Instance Methods

discard(identifier) click to toggle source
# File lib/asperalm/oauth_cache.rb, line 77
def discard(identifier)
  Log.log.info("deleting cache file and memory for token")
  token_state_file=token_filepath(identifier)
  File.delete(token_state_file) if File.exist?(token_state_file)
  @token_cache.delete(identifier)
end
flush_tokens() click to toggle source

delete cached tokens

# File lib/asperalm/oauth_cache.rb, line 28
def flush_tokens
  tokenfiles=Dir[File.join(@token_cache_folder,TOKEN_FILE_PREFIX+'*'+TOKEN_FILE_SUFFIX)]
  tokenfiles.each do |filepath|
    File.delete(filepath)
  end
  return tokenfiles
end
get(identifier) click to toggle source
# File lib/asperalm/oauth_cache.rb, line 54
def get(identifier)
  # if first time, try to read from file
  if !@token_cache.has_key?(identifier)
    token_state_file=token_filepath(identifier)
    if File.exist?(token_state_file) then
      Log.log.info("reading token from file cache: #{token_state_file}")
      # returns decoded data
      @token_cache[identifier]=JSON.parse(File.read(token_state_file))
    end
  end
  return @token_cache[identifier]
end
persistency_folder() click to toggle source
# File lib/asperalm/oauth_cache.rb, line 23
def persistency_folder; @token_cache_folder;end
persistency_folder=(v) click to toggle source
# File lib/asperalm/oauth_cache.rb, line 25
def persistency_folder=(v); @token_cache_folder=v;end
save(identifier,token_data) click to toggle source

save token data in memory and disk cache

# File lib/asperalm/oauth_cache.rb, line 68
def save(identifier,token_data)
  Log.log.info("saving #{token_data}")
  @token_cache[identifier]=token_data
  token_state_file=token_filepath(identifier)
  File.write(token_state_file,token_data.to_json)
  Log.log.info("new saved token is #{@token_cache[identifier]['access_token']}")
  return nil
end
token_filepath(identifier) click to toggle source

get location of cache for token, using some unique filename

# File lib/asperalm/oauth_cache.rb, line 48
def token_filepath(identifier)
  filepath=File.join(@token_cache_folder,TOKEN_FILE_PREFIX+TOKEN_FILE_SEPARATOR+identifier+TOKEN_FILE_SUFFIX)
  Log.log.debug("token path=#{filepath}")
  return filepath
end