class DiskCacher

Class dedicated for caching data on local system as well as for cookie management

Constants

file name to hold cookies

FILE_DELIMITER

Public Class Methods

new(cache_dir = DTK::Client::OsUtil.get_temp_location) click to toggle source
# File lib/config/disk_cacher.rb, line 35
def initialize(cache_dir = DTK::Client::OsUtil.get_temp_location)
  @cache_dir = cache_dir
  @current_user = ::DTK::Client::Configurator.client_username
end

Public Instance Methods

fetch(file_name, max_age = 0, use_mock_up = true) click to toggle source
# File lib/config/disk_cacher.rb, line 40
def fetch(file_name, max_age = 0, use_mock_up = true)
  file = Digest::MD5.hexdigest(file_name)
  # current user is important so that there are no clashes in writting to temp file
  # between multiple users on same machine
  file_path = File.join(@cache_dir, "#{@current_user}#{FILE_DELIMITER}#{file}")

  # we check if the file -- a MD5 hexdigest of the URL -- exists
  #  in the dir. If it does and the data is fresh, we just read
  #  data from the file and return
  if File.exists? file_path
    return File.new(file_path).read if ((Time.now - File.mtime(file_path)) < max_age)
  end

  # if the file does not exist (or if the data is not fresh), we
  #  make an get request and save it to a file
  response_string = ""
  response = get rest_url("metadata/get_metadata/#{file_name}")

  if (response["status"] == "ok")
    file = File.open(file_path, "w") do |data|
      data << response_string = response["data"]
    end
  end

  return response_string
end