Class: Storyboard::Cache

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#fileObject

Returns the value of attribute file.



5
6
7
# File 'lib/storyboard/cache.rb', line 5

def file
  @file
end

#hashObject

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_subtitleObject



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

Returns:

  • (Boolean)


16
17
18
# File 'lib/storyboard/cache.rb', line 16

def old?
	DateTime.parse(@data['lastran']) < (DateTime.now - ((60 * 15)/86400.0))
end

#saveObject



20
21
22
# File 'lib/storyboard/cache.rb', line 20

def save
	File.open(@file, 'w') { |f| f.write(@data.to_json)}
end

#subtitlesObject



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