Class: Storyboard::Cache
Instance Attribute Summary collapse
-
#file ⇒ Object
Returns the value of attribute file.
-
#hash ⇒ Object
Returns the value of attribute hash.
Instance Method Summary collapse
- #download_file(url, &block) ⇒ Object
-
#initialize(file_hash) ⇒ Cache
constructor
A new instance of Cache.
- #last_used_subtitle ⇒ Object
- #last_used_subtitle=(val) ⇒ Object
- #old? ⇒ Boolean
- #save ⇒ Object
- #subtitles ⇒ Object
- #subtitles=(val) ⇒ Object
Constructor Details
#initialize(file_hash) ⇒ Cache
Returns a new instance of Cache.
6 7 8 9 10 11 12 13 14 |
# File 'lib/storyboard/cache.rb', line 6 def initialize(file_hash) @hash = file_hash @file = File.join(Dir.tmpdir, "#{file_hash}.storyboard") if File.exists?(@file) @data = JSON.parse(File.read(@file)) end @data = {'downloads' => {}} if @data.nil? || old? @data['lastran'] = Time.now.to_s end |
Instance Attribute Details
#file ⇒ Object
Returns the value of attribute file.
5 6 7 |
# File 'lib/storyboard/cache.rb', line 5 def file @file end |
#hash ⇒ Object
Returns the value of attribute hash.
5 6 7 |
# File 'lib/storyboard/cache.rb', line 5 def hash @hash end |
Instance Method Details
#download_file(url, &block) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/storyboard/cache.rb', line 25 def download_file(url, &block) if @data['downloads'][url] LOG.debug("Cached file #{@data['downloads'][url]}") return File.read(@data['downloads'][url]) else LOG.debug("Loading file from #{url}") results = yield subpath = File.join(Dir.tmpdir, "#{@hash}-#{Digest::SHA1.hexdigest(url)}.storyboard") File.open(subpath, 'w') { |f| f.write(results) } @data['downloads'][url] = subpath self.save return results end end |
#last_used_subtitle ⇒ Object
48 49 50 |
# File 'lib/storyboard/cache.rb', line 48 def last_used_subtitle @data['last_used_subtitle'] end |
#last_used_subtitle=(val) ⇒ Object
52 53 54 |
# File 'lib/storyboard/cache.rb', line 52 def last_used_subtitle=(val) @data['last_used_subtitle'] = val end |
#old? ⇒ Boolean
16 17 18 |
# File 'lib/storyboard/cache.rb', line 16 def old? DateTime.parse(@data['lastran']) < (DateTime.now - ((60 * 15)/86400.0)) end |
#save ⇒ Object
20 21 22 |
# File 'lib/storyboard/cache.rb', line 20 def save File.open(@file, 'w') { |f| f.write(@data.to_json)} end |
#subtitles ⇒ Object
40 41 42 |
# File 'lib/storyboard/cache.rb', line 40 def subtitles @data['subtitles'] end |
#subtitles=(val) ⇒ Object
44 45 46 |
# File 'lib/storyboard/cache.rb', line 44 def subtitles=(val) @data['subtitles'] = val end |