Class: Downspout::Downloader

Inherits:
Base
  • Object
show all
Defined in:
lib/downspout/downloader.rb

Overview

The object returned by a call to fetch_url()

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ Downloader

:nodoc:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/downspout/downloader.rb', line 20

def initialize( options=nil ) #:nodoc:
  @basename = nil
  @curb_enabled = Downspout::Config.use_curb?
  @response_headers = {}
  @started_at = nil
  @finished_at = nil
  @redirected_url = nil

  if options.respond_to?(:keys) then
    options.each do |key, value|
      self.send("#{key}=", value)
    end
  end

  @uri = URI.parse( @url ) unless @url.nil?
end

Instance Attribute Details

#pathObject

returns the path to the downloaded file



6
7
8
# File 'lib/downspout/downloader.rb', line 6

def path
  @path
end

#responseObject (readonly)

returns the remote response as the appropriate Net::HTTPResponse



9
10
11
# File 'lib/downspout/downloader.rb', line 9

def response
  @response
end

#response_headersObject (readonly)

returns the headers parsed from the remote response



12
13
14
# File 'lib/downspout/downloader.rb', line 12

def response_headers
  @response_headers
end

#uriObject (readonly)

returns the URI parsed from the URL



15
16
17
# File 'lib/downspout/downloader.rb', line 15

def uri
  @uri
end

#urlObject

returns the URL initially given



18
19
20
# File 'lib/downspout/downloader.rb', line 18

def url
  @url
end

Instance Method Details

#basenameObject

Extracts the file name from the URL or uses a default name based on the content-type header



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/downspout/downloader.rb', line 56

def basename
  return @basename unless @basename.nil?

  if !(@path.nil?) then
    @basename = File.basename( @path )
  else
    if !(@uri.path.nil? || @uri.path.empty? || @uri.path == '/')
      @basename = File.basename( @uri.path )
    else
      $logger.debug("downspout | downloader | basename | Bad URI path")
      @basename = 'file.downspout'
    end
  end

  $logger.debug("downspout | downloader | basename | #{@basename} ")

  return @basename
end

#disable_curb!Object

configure this download NOT to use the Curb library



94
95
96
# File 'lib/downspout/downloader.rb', line 94

def disable_curb!
  @curb_enabled = false
end

#download!Object

:nodoc:

Raises:



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/downspout/downloader.rb', line 98

def download! #:nodoc:
  $logger.debug("downspout | downloader | download! | URL : #{@url} ")
  @started_at = Time.now

  raise UnsupportedScheme if @uri.nil?
  raise UnsupportedScheme unless Downspout.supported_protocol?( @uri.scheme )

  if @path.nil? then
    tf = Downspout::Tmpfile.new( :name => self.basename )
    @path = tf.path
  end
  $logger.debug("downspout | downloader | download! | Path : #{@path} ")

  remove_file_at_target_path

  if Downspout::Config.network_enabled? then
    case self.scheme
    when /ftp/
      net_ftp_download
    when /http[s]?/
      if use_curb? then
        curb_http_download
      else
        net_http_download
      end
    else
      $logger.error("downspout | downloader | download! | Unknown URL Scheme : '#{self.scheme}'")
      raise UnsupportedScheme
    end
  else
    $logger.warn("downspout | downloader | download! | >>>>>   Networking Disabled   <<<<<")
  end

  downloaded = File.exist?( @path )

  $logger.debug("downspout | downloader | download! | #{self.basename} downloaded? : #{downloaded} ")
  @finished_at = Time.now

  new_name = generate_file_name

  if (tf && new_name && !(@basename == new_name)) then
    # rename file more appropriately
    $logger.debug("downspout | downloader | download! | Renaming #{@basename} to #{new_name} ...")
    new_path = File.join( File.dirname( tf.path ), new_name)
    begin
      FileUtils.mv( tf.path, new_path )
    rescue Exception => e
      $logger.error("downspout | downloader | download! | Exception while renaming #{@basename} : #{e}")
      raise e
    end
    @path = new_path
  end

  $logger.debug("downspout | downloader | download! | Started: #{@started_at.utc}, Finished: #{@finished_at.utc}, Duration: #{duration}")

  return downloaded
end

#durationObject

returns the time taken to download the file



48
49
50
51
52
53
# File 'lib/downspout/downloader.rb', line 48

def duration
  return nil unless @started_at
  return nil unless @finished_at

  return @finished_at - @started_at
end

#enable_curb!Object

configure this download to use the Curb library (will fail if Curb is unavailable.)



87
88
89
90
91
# File 'lib/downspout/downloader.rb', line 87

def enable_curb!
  @curb_enabled = true if Downspout::Config.curb_available?

  return @curb_enabled
end

#schemeObject

returns the protocol or ‘scheme’ of the URL



42
43
44
45
# File 'lib/downspout/downloader.rb', line 42

def scheme
  return @uri.scheme unless @uri.nil?
  return nil
end

#to_sObject

:nodoc:



37
38
39
# File 'lib/downspout/downloader.rb', line 37

def to_s #:nodoc:
  return @path
end

#use_curb?Boolean

will this download use the Curb library?

Returns:

  • (Boolean)


76
77
78
# File 'lib/downspout/downloader.rb', line 76

def use_curb?
  return @curb_enabled
end

#use_net_http?Boolean

will this download use the default Net/HTTP library?

Returns:

  • (Boolean)


81
82
83
84
# File 'lib/downspout/downloader.rb', line 81

def use_net_http?
  return false if use_curb?
  return true
end