Class: Mechanize::File

### Home Classes Methods

### In Files

- lib/mechanize/file.rb

### Parent

Object

### Methods

- ::new
- #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::File

This is the base class for the Pluggable Parsers. If Mechanize cannot find an appropriate class to use for the content type, this class will be used. For example, if you download an image/jpeg, Mechanize will not know how to parse it, so this class will be instantiated.

This is a good class to use as the base class for building your own pluggable parsers.

## Example

    require 'mechanize'

    agent = Mechanize.new
    agent.get('http://example.com/foo.jpg').class  #=> Mechanize::File

### Attributes

body[RW]

The HTTP response body, the raw file contents

content[RW]

The HTTP response body, the raw file contents

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 = nil, code = nil) click to toggle source

Creates a new file retrieved from the given `uri` and `response` object. The `body` is the HTTP response body and `code` is the HTTP status.

    # File lib/mechanize/file.rb, line 38
    def initialize uri = nil, response = nil, body = nil, code = nil
      @uri  = uri
      @body = body
      @code = code

      @full_path = false unless defined? @full_path

      fill_header response
      extract_filename

      yield self if block_given?
    end

### Public Instance Methods

save(filename = nil) click to toggle source

Use this method to save the content of this object to `filename`. returns the filename

    file.save 'index.html'
    file.save 'index.html' # saves to index.html.1

    uri = URI 'http://localhost/test.html'
    file = Mechanize::File.new uri, nil, ''
    filename = file.save  # saves to test.html
    puts filename         # test.html

    # File lib/mechanize/file.rb, line 63
    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 this object to `filename`. This method will overwrite any existing filename that exists with the same name. returns the filename

    file.save 'index.html'
    file.save! 'index.html' # overwrite original file
    filename = file.save! 'index.html' # overwrite original file with filename 'index.html'

    # File lib/mechanize/file.rb, line 80
    def save! filename = nil
      filename ||= @filename
      dirname = File.dirname filename
      FileUtils.mkdir_p dirname

      open filename, 'wb' do |f|
        f.write body
      end

      filename
    end

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

[Validate]

Generated with the Darkfish Rdoc Generator 2.