Module: Mechanize::Parser

Extended by:
Forwardable
Included in:
Download, File
Defined in:
lib/mechanize/parser.rb

Overview

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

Constant Summary collapse

SPECIAL_FILENAMES =

Special filenames that must be escaped

/\A#{special_filenames}/i

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#codeObject

The HTTP response code



54
55
56
# File 'lib/mechanize/parser.rb', line 54

def code
  @code
end

#responseObject Also known as: header

The Mechanize::Headers for this file



47
48
49
# File 'lib/mechanize/parser.rb', line 47

def response
  @response
end

#uriObject

The URI this file was retrieved from



42
43
44
# File 'lib/mechanize/parser.rb', line 42

def uri
  @uri
end

Instance Method Details

#extract_filename(full_path = @full_path) ⇒ Object

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.



97
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
# File 'lib/mechanize/parser.rb', line 97

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 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) ⇒ Object

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

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



146
147
148
149
150
151
152
153
154
# File 'lib/mechanize/parser.rb', line 146

def fill_header response
  @response = Mechanize::Headers.new

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

  @response
end

#find_free_name(filename) ⇒ Object

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



159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/mechanize/parser.rb', line 159

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