Method: Mechanize::File#initialize

Defined in:
lib/mechanize/file.rb

#initialize(uri = nil, response = nil, body = nil, code = nil) {|_self| ... } ⇒ File

Returns a new instance of File.

Yields:

  • (_self)

Yield Parameters:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/mechanize/file.rb', line 31

def initialize(uri=nil, response=nil, body=nil, code=nil)
  @uri = uri
  @body = body
  @code = code
  @response = Headers.new

  # Copy the headers in to a hash to prevent memory leaks
  if response
    response.each { |k,v|
      @response[k] = v
    }
  end

  @filename = 'index.html'

  # Set the filename
  if disposition = @response['content-disposition']
    disposition.split(/;\s*/).each do |pair|
      k,v = pair.split(/=/, 2)
      @filename = v if k && k.downcase == 'filename'
    end
  else
    if @uri
      @filename = @uri.path.split(/\//).last || 'index.html'
      @filename << ".html" unless @filename =~ /\./
    end
  end

  yield self if block_given?
end