Class: Mechanize::File
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
Constant Summary
Constants included from Parser
Instance Attribute Summary collapse
-
#body ⇒ Object
(also: #content)
The HTTP response body, the raw file contents.
-
#filename ⇒ Object
The filename for this file based on the content-disposition of the response or the basename of the URL.
Attributes included from Parser
Instance Method Summary collapse
-
#initialize(uri = nil, response = nil, body = nil, code = nil) {|_self| ... } ⇒ File
constructor
Creates a new file retrieved from the given
uri
andresponse
object. -
#save(filename = nil) ⇒ Object
(also: #save_as)
Use this method to save the content of this object to
filename
.
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.
38 39 40 41 42 43 44 45 46 47 48 49 |
# 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 |
Instance Attribute Details
#body ⇒ Object Also known as: content
The HTTP response body, the raw file contents
24 25 26 |
# File 'lib/mechanize/file.rb', line 24 def body @body end |
#filename ⇒ Object
The filename for this file based on the content-disposition of the response or the basename of the URL
30 31 32 |
# File 'lib/mechanize/file.rb', line 30 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
54 55 56 57 58 59 60 |
# File 'lib/mechanize/file.rb', line 54 def save filename = nil filename = find_free_name filename open filename, 'wb' do |f| f.write body end end |