Class: Upton::Downloader

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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, options = {})
  @uri = uri
  @options = options
  @cache = options.fetch(:cache) { true }
  @cache_location = File.absolute_path(options[:cache_location] || "#{Dir.tmpdir}/upton")
  @verbose = options[:verbose] || false
  @readable_stash_filenames = options[:readable_filenames] || false
  initialize_cache!
end

Instance Attribute Details

#cache_locationObject (readonly)

Returns the value of attribute cache_location.



21
22
23
# File 'lib/upton/downloader.rb', line 21

def cache_location
  @cache_location
end

#uriObject (readonly)

Returns the value of attribute uri.



21
22
23
# File 'lib/upton/downloader.rb', line 21

def uri
  @uri
end

#verboseObject (readonly)

Returns the value of attribute verbose.



21
22
23
# File 'lib/upton/downloader.rb', line 21

def verbose
  @verbose
end

Instance Method Details

#getObject



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