class ZendeskAppsTools::Cache

Constants

CACHE_FILE_NAME

Attributes

options[R]

Public Class Methods

new(options) click to toggle source
# File lib/zendesk_apps_tools/cache.rb, line 10
def initialize(options)
  @options = options
end

Public Instance Methods

clear() click to toggle source
# File lib/zendesk_apps_tools/cache.rb, line 26
def clear
  File.delete local_cache_path if options[:clean] && File.exist?(local_cache_path)
end
fetch(key, subdomain = nil) click to toggle source
# File lib/zendesk_apps_tools/cache.rb, line 21
def fetch(key, subdomain = nil)
  # drop the default_proc and replace with Hash#dig if older Ruby versions are unsupported
  local_cache[key] || global_cache[subdomain][key] || global_cache['default'][key]
end
save(hash) click to toggle source
# File lib/zendesk_apps_tools/cache.rb, line 14
def save(hash)
  return if options[:zipfile]

  local_cache.update(hash)
  File.open(local_cache_path, 'w') { |f| f.write JSON.pretty_generate(local_cache) }
end

Private Instance Methods

global_cache() click to toggle source
# File lib/zendesk_apps_tools/cache.rb, line 36
def global_cache
  @global_cache ||= begin
    if File.exist?(global_cache_path)
      JSON.parse(File.read(global_cache_path)).tap do |cache|
        cache.default_proc = proc do |_hash, _key|
          {}
        end
      end
    else
      Hash.new({})
    end
  end
end
global_cache_path() click to toggle source
# File lib/zendesk_apps_tools/cache.rb, line 50
def global_cache_path
  @global_cache_path ||= File.join(Dir.home, CACHE_FILE_NAME)
end
local_cache() click to toggle source
# File lib/zendesk_apps_tools/cache.rb, line 32
def local_cache
  @local_cache ||= File.exist?(local_cache_path) ? JSON.parse(File.read(local_cache_path)) : {}
end
local_cache_path() click to toggle source
# File lib/zendesk_apps_tools/cache.rb, line 54
def local_cache_path
  @local_cache_path ||= File.join(options[:path], CACHE_FILE_NAME)
end