Class: Upton::Downloader
- Inherits:
-
Object
- Object
- Upton::Downloader
- Defined in:
- lib/upton/downloader.rb
Overview
This class is used internally to download and cache the webpages that are requested.
By default, the cache location is the output of ‘Dir.tmpdir`/upton. The Dir.tmpdir returns the temporary directory of the operating system. By default, the stashed files have a non-human-readable md5-based filename. If `readable_stash_filenames` is true, they will have human-readable names.
Constant Summary collapse
- MAX_FILENAME_LENGTH =
for unixes, win xp+
130
- EMPTY_STRING =
''
Instance Attribute Summary collapse
-
#cache_location ⇒ Object
readonly
Returns the value of attribute cache_location.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
-
#verbose ⇒ Object
readonly
Returns the value of attribute verbose.
Instance Method Summary collapse
- #get ⇒ Object
-
#initialize(uri, options = {}) ⇒ Downloader
constructor
A new instance of Downloader.
Constructor Details
#initialize(uri, options = {}) ⇒ Downloader
Returns a new instance of Downloader.
22 23 24 25 26 27 28 29 30 |
# File 'lib/upton/downloader.rb', line 22 def initialize(uri, = {}) @uri = uri @options = @cache = .fetch(:cache) { true } @cache_location = File.absolute_path([:cache_location] || "#{Dir.tmpdir}/upton") @verbose = [:verbose] || false @readable_stash_filenames = [:readable_filenames] || false initialize_cache! end |
Instance Attribute Details
#cache_location ⇒ Object (readonly)
Returns the value of attribute cache_location.
21 22 23 |
# File 'lib/upton/downloader.rb', line 21 def cache_location @cache_location end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
21 22 23 |
# File 'lib/upton/downloader.rb', line 21 def uri @uri end |
#verbose ⇒ Object (readonly)
Returns the value of attribute verbose.
21 22 23 |
# File 'lib/upton/downloader.rb', line 21 def verbose @verbose end |
Instance Method Details
#get ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/upton/downloader.rb', line 32 def get if cache_enabled? puts "Stashing enabled. Will try reading #{uri} data from cache." if @verbose download_from_cache! else puts "Stashing disabled. Will download from the internet." if @verbose from_resource = true resp = download_from_resource! {:resp => resp, :from_resource => from_resource } end end |