-
Notifications
You must be signed in to change notification settings - Fork 473
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
66 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,80 @@ | ||
class Mechanize | ||
# = 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 | ||
# | ||
class File | ||
extend Forwardable | ||
## | ||
# = 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 | ||
|
||
attr_accessor :uri, :response, :body, :code, :filename | ||
alias :header :response | ||
def_delegator :header, :[], :[] | ||
def_delegator :header, :[]=, :[]= | ||
def_delegator :header, :key?, :key? | ||
def_delegator :header, :each, :each | ||
def_delegator :header, :canonical_each, :canonical_each | ||
class Mechanize::File | ||
extend Forwardable | ||
|
||
alias :content :body | ||
attr_accessor :uri, :response, :body, :code, :filename | ||
alias :header :response | ||
def_delegator :header, :[], :[] | ||
def_delegator :header, :[]=, :[]= | ||
def_delegator :header, :key?, :key? | ||
def_delegator :header, :each, :each | ||
def_delegator :header, :canonical_each, :canonical_each | ||
|
||
def initialize(uri=nil, response=nil, body=nil, code=nil) | ||
@uri = uri | ||
@body = body | ||
@code = code | ||
@response = Headers.new | ||
alias :content :body | ||
|
||
# Copy the headers in to a hash to prevent memory leaks | ||
if response | ||
response.each { |k,v| | ||
@response[k] = v | ||
} | ||
end | ||
def initialize(uri=nil, response=nil, body=nil, code=nil) | ||
@uri = uri | ||
@body = body | ||
@code = code | ||
@response = Mechanize::Headers.new | ||
|
||
@filename = 'index.html' | ||
# Copy the headers in to a hash to prevent memory leaks | ||
if response | ||
response.each { |k,v| | ||
@response[k] = v | ||
} | ||
end | ||
|
||
# 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 | ||
@filename = 'index.html' | ||
|
||
yield self if block_given? | ||
# 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 | ||
|
||
# Use this method to save the content of this object to filename | ||
def save_as(filename = nil) | ||
if filename.nil? | ||
filename = @filename | ||
number = 1 | ||
while(::File.exists?(filename)) | ||
filename = "#{@filename}.#{number}" | ||
number += 1 | ||
end | ||
end | ||
yield self if block_given? | ||
end | ||
|
||
::File::open(filename, "wb") { |f| | ||
f.write body | ||
} | ||
# Use this method to save the content of this object to filename | ||
def save_as(filename = nil) | ||
if filename.nil? | ||
filename = @filename | ||
number = 1 | ||
while(File.exists?(filename)) | ||
filename = "#{@filename}.#{number}" | ||
number += 1 | ||
end | ||
end | ||
|
||
alias :save :save_as | ||
open filename, "wb" do |f| | ||
f.write body | ||
end | ||
end | ||
|
||
alias :save :save_as | ||
end | ||
|