Class: Mechanize::Download
- Inherits:
-
Object
- Object
- Mechanize::Download
- Includes:
- Parser
- Defined in:
- lib/mechanize/download.rb
Overview
Download is a pluggable parser for downloading files without loading them into memory first. You may subclass this class to handle content types you do not wish to load into memory first.
See Mechanize::PluggableParser for instructions on using this class.
Direct Known Subclasses
Constant Summary
Constants included from Parser
Instance Attribute Summary collapse
-
#body_io ⇒ Object
(also: #content)
readonly
Accessor for the IO-like that contains the body.
-
#filename ⇒ Object
The filename for this file based on the content-disposition of the response or the basename of the URL.
Attributes included from Parser
Instance Method Summary collapse
-
#body ⇒ Object
The body of this response as a String.
-
#initialize(uri = nil, response = nil, body_io = nil, code = nil) {|_self| ... } ⇒ Download
constructor
Creates a new download retrieved from the given
uri
andresponse
object. -
#save(filename = nil) ⇒ Object
Saves a copy of the body_io to
filename
.
Methods included from Parser
#extract_filename, #fill_header, #find_free_name
Constructor Details
#initialize(uri = nil, response = nil, body_io = nil, code = nil) {|_self| ... } ⇒ Download
Creates a new download retrieved from the given uri
and response
object. The body_io
is an IO-like containing the HTTP response body and code
is the HTTP status.
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/mechanize/download.rb', line 30 def initialize uri = nil, response = nil, body_io = nil, code = nil @uri = uri @body_io = body_io @code = code @full_path = false unless defined? @full_path fill_header response extract_filename yield self if block_given? end |
Instance Attribute Details
#body_io ⇒ Object (readonly) Also known as: content
Accessor for the IO-like that contains the body
21 22 23 |
# File 'lib/mechanize/download.rb', line 21 def body_io @body_io end |
#filename ⇒ Object
The filename for this file based on the content-disposition of the response or the basename of the URL
16 17 18 |
# File 'lib/mechanize/download.rb', line 16 def filename @filename end |
Instance Method Details
#body ⇒ Object
The body of this response as a String.
Take care, this may use lots of memory if the response body is large.
48 49 50 |
# File 'lib/mechanize/download.rb', line 48 def body @body_io.read.tap { @body_io.rewind } end |
#save(filename = nil) ⇒ Object
Saves a copy of the body_io to filename
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/mechanize/download.rb', line 55 def save filename = nil filename = find_free_name filename dirname = File.dirname filename FileUtils.mkdir_p dirname open filename, 'wb' do |io| until @body_io.eof? do io.write @body_io.read 16384 end end end |