Class: Henchman::Cache
- Inherits:
-
Object
- Object
- Henchman::Cache
- Defined in:
- lib/cache.rb
Instance Method Summary collapse
- #config(config) ⇒ Object
- #delete(track) ⇒ Object
- #flush ⇒ Object
- #get_time_last_downloaded(track) ⇒ Object
- #ignore?(type, identifier) ⇒ Boolean
-
#initialize ⇒ Cache
constructor
A new instance of Cache.
- #tag(track) ⇒ Object
- #update_ignore(type, identifier) ⇒ Object
- #valid_ignore_type?(type) ⇒ Boolean
Constructor Details
#initialize ⇒ Cache
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.("~/.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
#config(config) ⇒ Object
23 24 25 |
# File 'lib/cache.rb', line 23 def config config @config = config end |
#delete(track) ⇒ Object
45 46 47 |
# File 'lib/cache.rb', line 45 def delete track @cache[:history].delete track[:id].to_i end |
#flush ⇒ Object
49 50 51 |
# File 'lib/cache.rb', line 49 def flush File.open(@cache_file, "w") { |f| f.write( @cache.to_yaml ) } end |
#get_time_last_downloaded(track) ⇒ Object
37 38 39 |
# File 'lib/cache.rb', line 37 def get_time_last_downloaded track @cache[:history][track[:id].to_i] end |
#ignore?(type, identifier) ⇒ 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
41 42 43 |
# File 'lib/cache.rb', line 41 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
53 54 55 56 57 58 59 60 61 |
# File 'lib/cache.rb', line 53 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 |