Class: Mechanize::Download

### Home Classes Methods

### In Files

- lib/mechanize/download.rb

### Parent

Object

### Methods

- ::new
- #body
- #save
- #save!
- #save_as

### Included Modules

- Mechanize::Parser

### Files

- EXAMPLES.rdoc
- GUIDE.rdoc
- LICENSE.rdoc
- Manifest.txt
- README.rdoc

### Class/Module Index

Quicksearch

- Mechanize
- Mechanize::ChunkedTerminationError
- Mechanize::ContentTypeError
- Mechanize::Cookie
- Mechanize::CookieCMethods
- Mechanize::CookieDeprecated
- Mechanize::CookieIMethods
- Mechanize::CookieJar
- Mechanize::CookieJarIMethods
- Mechanize::DirectorySaver
- Mechanize::Download
- Mechanize::ElementMatcher
- Mechanize::ElementNotFoundError
- Mechanize::Error
- Mechanize::File
- Mechanize::FileConnection
- Mechanize::FileRequest
- Mechanize::FileResponse
- Mechanize::FileSaver
- Mechanize::Form
- Mechanize::Form::Button
- Mechanize::Form::CheckBox
- Mechanize::Form::Field
- Mechanize::Form::FileUpload
- Mechanize::Form::Hidden
- Mechanize::Form::ImageButton
- Mechanize::Form::Keygen
- Mechanize::Form::MultiSelectList
- Mechanize::Form::Option
- Mechanize::Form::RadioButton
- Mechanize::Form::Reset
- Mechanize::Form::SelectList
- Mechanize::Form::Submit
- Mechanize::Form::Text
- Mechanize::Form::Textarea
- Mechanize::HTTP
- Mechanize::HTTP::Agent
- Mechanize::HTTP::AuthChallenge
- Mechanize::HTTP::AuthRealm
- Mechanize::HTTP::AuthStore
- Mechanize::HTTP::ContentDispositionParser
- Mechanize::HTTP::WWWAuthenticateParser
- Mechanize::Headers
- Mechanize::History
- Mechanize::Image
- Mechanize::Page
- Mechanize::Page::Base
- Mechanize::Page::Frame
- Mechanize::Page::Image
- Mechanize::Page::Label
- Mechanize::Page::Link
- Mechanize::Page::MetaRefresh
- Mechanize::Parser
- Mechanize::PluggableParser
- Mechanize::Prependable
- Mechanize::RedirectLimitReachedError
- Mechanize::RedirectNotGetOrHeadError
- Mechanize::ResponseCodeError
- Mechanize::ResponseReadError
- Mechanize::RobotsDisallowedError
- Mechanize::TestCase
- Mechanize::UnauthorizedError
- Mechanize::UnsupportedSchemeError
- Mechanize::Util
- Mechanize::XmlFile
- HTTP
- HTTP::CookieJar

No matching classes.

# Mechanize::Download

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.

### Attributes

body_io[R]

Accessor for the IO-like that contains the body

content[R]

Accessor for the IO-like that contains the body

filename[RW]

The filename for this file based on the content-disposition of the response or the basename of the URL

### Public Class Methods

new(uri = nil, response = nil, body_io = nil, code = nil) click to toggle source

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.

    # 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

### Public Instance Methods

body() click to toggle source

The body of this response as a String.

Take care, this may use lots of memory if the response body is large.

    # File lib/mechanize/download.rb, line 48
    def body
      @body_io.read.tap { @body_io.rewind }
    end

save(filename = nil) click to toggle source

Saves a copy of the body_io to `filename` returns the filename

    # File lib/mechanize/download.rb, line 56
    def save filename = nil
      filename = find_free_name filename
      save! filename
    end

Also aliased as: save_as
save!(filename = nil) click to toggle source

Use this method to save the content of body_io to `filename`. This method will overwrite any existing filename that exists with the same name. returns the filename

    # File lib/mechanize/download.rb, line 69
    def save! filename = nil
      filename ||= @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

      filename
    end

save_as(filename = nil) click to toggle source
Alias for: save

[Validate]

Generated with the Darkfish Rdoc Generator 2.