Class: Mechanize::File

Inherits:
Object
  • Object
show all
Includes:
Parser
Defined in:
lib/mechanize/file.rb

Overview

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

Direct Known Subclasses

Page, XmlFile

Constant Summary

Constants included from Parser

Parser::SPECIAL_FILENAMES

Instance Attribute Summary collapse

Attributes included from Parser

#code, #response, #uri

Instance Method Summary collapse

Methods included from Parser

#extract_filename, #fill_header, #find_free_name

Constructor Details

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

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.

Yields:

  • (_self)

Yield Parameters:



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/mechanize/file.rb', line 39

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

Instance Attribute Details

#bodyObject Also known as: content

The HTTP response body, the raw file contents



25
26
27
# File 'lib/mechanize/file.rb', line 25

def body
  @body
end

#filenameObject

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



31
32
33
# File 'lib/mechanize/file.rb', line 31

def filename
  @filename
end

Instance Method Details

#save(filename = nil) ⇒ Object Also known as: save_as

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


64
65
66
67
# File 'lib/mechanize/file.rb', line 64

def save filename = nil
  filename = find_free_name filename
  save! filename
end

#save!(filename = nil) ⇒ Object

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'


81
82
83
84
85
86
87
88
89
90
91
# File 'lib/mechanize/file.rb', line 81

def save! filename = nil
  filename ||= @filename
  dirname = File.dirname filename
  FileUtils.mkdir_p dirname

  ::File.open(filename, 'wb')do |f|
    f.write body
  end

  filename
end