Module: Mechanize::Parser

### Home Classes Methods

### In Files

- lib/mechanize/parser.rb

### Methods

- #[]
- #[]=
- #each
- #extract_filename
- #fill_header
- #find_free_name
- #key?

### 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::Parser

The parser module provides standard methods for accessing the headers and content of a response that are shared across pluggable parsers.

### Constants

SPECIAL_FILENAMES

  Special filenames that must be escaped

### Attributes

code[RW]

The HTTP response code

header[RW]

The Mechanize::Headers for this file

response[RW]

The Mechanize::Headers for this file

uri[RW]

The URI this file was retrieved from

### Public Instance Methods

[](header) click to toggle source

Access HTTP `header` by name

    # File lib/mechanize/parser.rb, line 64
    def_delegator :header, :[], :[]

[]=(header, value) click to toggle source

Set HTTP `header` to `value`

    # File lib/mechanize/parser.rb, line 74
    def_delegator :header, :[]=, :[]=

each click to toggle source

Enumerate HTTP headers

    # File lib/mechanize/parser.rb, line 91
    def_delegator :header, :each, :each

extract_filename(full_path = @full_path) click to toggle source

Extracts the filename from a Content-Disposition header in the response or from the URI. If `full_path` is true the filename will include the host name and path to the resource, otherwise a filename in the current directory is given.

    # File lib/mechanize/parser.rb, line 106
    def extract_filename full_path = @full_path
      handled = false

      if @uri then
        uri = @uri
        uri += 'index.html' if uri.path.end_with? '/'

        path     = uri.path.split(/\//)
        filename = path.pop || 'index.html'
      else
        path     = []
        filename = 'index.html'
      end

      # Set the filename
      if disposition = @response['content-disposition'] then
        content_disposition =
          Mechanize::HTTP::ContentDispositionParser.parse disposition

        if content_disposition && content_disposition.filename && content_disposition.filename != '' then
          filename = content_disposition.filename
          filename = filename.split(/[\\\/]/).last
          handled = true
        end
      end

      if not handled and @uri then
        filename << '.html' unless filename =~ /\./
        filename << "?#{@uri.query}" if @uri.query
      end

      if SPECIAL_FILENAMES =~ filename then
        filename = "_#{filename}"
      end

      filename = filename.tr "\x00-\x20<>:\"/\\|?*", '_'

      @filename = if full_path then
                    File.join @uri.host, path, filename
                  else
                    filename
                  end
    end

fill_header(response) click to toggle source

Creates a Mechanize::Header from the Net::HTTPResponse `response`.

This allows the Net::HTTPResponse to be garbage collected sooner.

    # File lib/mechanize/parser.rb, line 155
    def fill_header response
      @response = Mechanize::Headers.new

      response.each { |k,v|
        @response[k] = v
      } if response

      @response
    end

find_free_name(filename) click to toggle source

Finds a free filename based on `filename`, but is not race-free

    # File lib/mechanize/parser.rb, line 168
    def find_free_name filename
      base_filename = filename ||= @filename

      number = 1

      while File.exist? filename do
        filename = "#{base_filename}.#{number}"
        number += 1
      end

      filename
    end

key?(header) click to toggle source

Is the named `header` present?

    # File lib/mechanize/parser.rb, line 84
    def_delegator :header, :key?, :key?

[Validate]

Generated with the Darkfish Rdoc Generator 2.