Class: Mechanize::File
- Inherits:
-
Object
- Object
- Mechanize::File
- Defined in:
- lib/mechanize/file.rb
Overview
Synopsis
This is the default (and 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 a JPG, 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 'rubygems'
require 'mechanize'
agent = Mechanize.new
agent.get('http://example.com/foo.jpg').class #=> Mechanize::File
Instance Attribute Summary collapse
-
#body ⇒ Object
(also: #content)
Returns the value of attribute body.
-
#code ⇒ Object
Returns the value of attribute code.
-
#filename ⇒ Object
Returns the value of attribute filename.
-
#response ⇒ Object
(also: #header)
Returns the value of attribute response.
-
#uri ⇒ Object
Returns the value of attribute uri.
Instance Method Summary collapse
-
#initialize(uri = nil, response = nil, body = nil, code = nil, mech = nil) {|_self| ... } ⇒ File
constructor
A new instance of File.
-
#save_as(filename = nil) ⇒ Object
(also: #save)
Use this method to save the content of this object to filename.
Constructor Details
#initialize(uri = nil, response = nil, body = nil, code = nil, mech = nil) {|_self| ... } ⇒ File
Returns a new instance of File.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/mechanize/file.rb', line 24 def initialize(uri=nil, response=nil, body=nil, code=nil, mech=nil) @uri, @body, @code, @mech = uri, body, code, mech @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 @mech && @mech.use_content_disposition && (disposition = @response['content-disposition']) disposition.split(/;\s*/).each do |pair| k,v = pair.split(/=/, 2) @filename = v.sub(/\A"/){''}.sub(/"\Z/){''} 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 |
Instance Attribute Details
#body ⇒ Object Also known as: content
Returns the value of attribute body.
19 20 21 |
# File 'lib/mechanize/file.rb', line 19 def body @body end |
#code ⇒ Object
Returns the value of attribute code.
19 20 21 |
# File 'lib/mechanize/file.rb', line 19 def code @code end |
#filename ⇒ Object
Returns the value of attribute filename.
19 20 21 |
# File 'lib/mechanize/file.rb', line 19 def filename @filename end |
#response ⇒ Object Also known as: header
Returns the value of attribute response.
19 20 21 |
# File 'lib/mechanize/file.rb', line 19 def response @response end |
#uri ⇒ Object
Returns the value of attribute uri.
19 20 21 |
# File 'lib/mechanize/file.rb', line 19 def uri @uri end |
Instance Method Details
#save_as(filename = nil) ⇒ Object Also known as: save
Use this method to save the content of this object to filename
54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/mechanize/file.rb', line 54 def save_as(filename = nil) if filename.nil? filename = @filename number = 1 while(::File.exists?(filename)) filename = "#{@filename}.#{number}" number += 1 end end ::File::open(filename, "wb") { |f| f.write body } end |