class WWW::Mechanize::FileSaver


agent.get(‘example.com/foo.pdf’)
agent.pluggable_parser.pdf = WWW::Mechanize::FileSaver
agent = WWW::Mechanize.new
require ‘mechanize’
require ‘rubygems’
== Example to save all PDF’s
host and file path.
it encounters. It saves the files as a tree, reflecting the
This is a pluggable parser that automatically saves every file
= Synopsis

def initialize(uri=nil, response=nil, body=nil, code=nil)

def initialize(uri=nil, response=nil, body=nil, code=nil)
  super(uri, response, body, code)
  path = uri.path.empty? ? 'index.html' : uri.path.gsub(/^[\/]*/, '')
  path += 'index.html' if path =~ /\/$/
  split_path = path.split(/\//)
  filename = split_path.length > 0 ? split_path.pop : 'index.html'
  joined_path = split_path.join(::File::SEPARATOR)
  path = if joined_path.empty?
    uri.host
  else
    "#{uri.host}#{::File::SEPARATOR}#{joined_path}"
  end
  @filename = "#{path}#{::File::SEPARATOR}#{filename}"
  FileUtils.mkdir_p(path)
  save_as(@filename)
end