Class: GenericCachedDownloader
- Inherits:
-
Object
- Object
- GenericCachedDownloader
- Defined in:
- lib/cartocss_helper/util/generic_cached_downloader.rb
Instance Method Summary collapse
- #download(url, description) ⇒ Object
- #get_cache_timestamp(cache_filename) ⇒ Object
- #get_specified_resource(url, cache_filename, description: nil, invalidate_cache: false) ⇒ Object
-
#initialize(timeout: 20, error_message: nil, stop_on_timeout: true, wrapper: RestClientWrapper.new) ⇒ GenericCachedDownloader
constructor
A new instance of GenericCachedDownloader.
- #read_from_cache(cache_filename) ⇒ Object
- #write_cache(response, cache_filename) ⇒ Object
Constructor Details
#initialize(timeout: 20, error_message: nil, stop_on_timeout: true, wrapper: RestClientWrapper.new) ⇒ GenericCachedDownloader
Returns a new instance of GenericCachedDownloader.
6 7 8 9 10 11 |
# File 'lib/cartocss_helper/util/generic_cached_downloader.rb', line 6 def initialize(timeout: 20, error_message: nil, stop_on_timeout: true, wrapper: RestClientWrapper.new) @request_timeout = timeout @error_message = @stop_on_timeout = stop_on_timeout @wrapper = wrapper end |
Instance Method Details
#download(url, description) ⇒ Object
41 42 43 44 |
# File 'lib/cartocss_helper/util/generic_cached_downloader.rb', line 41 def download(url, description) downloader = GenericDownloader.new(timeout: @request_timeout, error_message: @error_message, stop_on_timeout: @stop_on_timeout, wrapper: @wrapper) return downloader.get_specified_resource(url, description: description) end |
#get_cache_timestamp(cache_filename) ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/cartocss_helper/util/generic_cached_downloader.rb', line 33 def (cache_filename) return nil unless File.exist?(cache_filename) f = File.new(cache_filename) = f.mtime.to_i f.close return end |
#get_specified_resource(url, cache_filename, description: nil, invalidate_cache: false) ⇒ Object
13 14 15 16 17 18 19 20 21 |
# File 'lib/cartocss_helper/util/generic_cached_downloader.rb', line 13 def get_specified_resource(url, cache_filename, description: nil, invalidate_cache: false) cache = read_from_cache(cache_filename) cache = nil if invalidate_cache return cache if cache != nil response = download(url, description) write_cache(response, cache_filename) return response end |
#read_from_cache(cache_filename) ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/cartocss_helper/util/generic_cached_downloader.rb', line 23 def read_from_cache(cache_filename) if File.exist?(cache_filename) file = File.new(cache_filename) cached = file.read file.close return cached end return nil end |
#write_cache(response, cache_filename) ⇒ Object
46 47 48 49 50 |
# File 'lib/cartocss_helper/util/generic_cached_downloader.rb', line 46 def write_cache(response, cache_filename) file = File.new(cache_filename, 'w') file.write response file.close end |