Class: Henchman::Cache

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

Instance Method Summary collapse

Constructor Details

#initializeCache

Returns a new instance of Cache.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/cache.rb', line 8

def initialize
  begin
    @cache_file = File.expand_path("~/.henchman/cache")
    @cache = YAML.load_file(@cache_file)
    raise "Incorrectly formatted cache" if !(@cache.include? :ignore)

    @cache[:ignore].each_value { |val| val.default = 0 }
  rescue StandardError => err
    puts "#{DateTime.now.strftime('%m-%d-%Y %H:%M:%S')}|"\
         "Error opening cache file (#{err})"
    @cache = Henchman::Templates.cache
  end
  @cache[:history].default = DateTime.new
end

Instance Method Details

#clear(type, value) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/cache.rb', line 64

def clear type, value
  raise "Invalid type #{type}" if !@cache[:ignore].keys.include? type
  if @cache[:ignore][type].include? value
    @cache[:ignore][type].delete value
    puts "Deleting #{type} #{value} from cache"
  else
    puts "#{type} #{value} not found"
  end
  flush
end

#config(config) ⇒ Object



23
24
25
# File 'lib/cache.rb', line 23

def config config
  @config = config
end

#delete(track) ⇒ Object



46
47
48
# File 'lib/cache.rb', line 46

def delete track
  @cache[:history].delete track[:id].to_i
end

#flushObject



50
51
52
# File 'lib/cache.rb', line 50

def flush
  File.open(@cache_file, "w") { |f| f.write( @cache.to_yaml ) }
end

#get_time_last_downloaded(track) ⇒ Object



37
38
39
40
# File 'lib/cache.rb', line 37

def get_time_last_downloaded track
  id = track[:id].to_i
  (@cache[:history].include?(id)) ? @cache[:history][id] : DateTime.new
end

#ignore?(type, identifier) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
# File 'lib/cache.rb', line 32

def ignore? type, identifier
  return false if !(valid_ignore_type? type)
  @cache[:ignore][type][identifier] >= (Time.now.to_i - @config[:reprompt_timeout])
end

#tag(track) ⇒ Object



42
43
44
# File 'lib/cache.rb', line 42

def tag track
  @cache[:history][track[:id].to_i] = DateTime.now
end

#update_ignore(type, identifier) ⇒ Object



27
28
29
30
# File 'lib/cache.rb', line 27

def update_ignore type, identifier
  return false if !(valid_ignore_type? type)
  @cache[:ignore][type][identifier] = Time.now.to_i
end

#valid_ignore_type?(type) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
57
58
59
60
61
62
# File 'lib/cache.rb', line 54

def valid_ignore_type? type
  if !(Henchman::Templates.cache[:ignore].keys.include? type)
    puts "#{DateTime.now.strftime('%m-%d-%Y %H:%M:%S')}|"\
         "Invalid type '#{type}' for ignore cache check"
    false
  else
    true
  end
end