Class WWW::Mechanize::FileSaver
In: lib/mechanize/pluggable_parsers.rb
Parent: File
Mechanize\n[lib/mechanize.rb\nlib/mechanize/cookie.rb\nlib/mechanize/errors.rb\nlib/mechanize/form.rb\nlib/mechanize/form_elements.rb\nlib/mechanize/history.rb\nlib/mechanize/list.rb\nlib/mechanize/page.rb\nlib/mechanize/page_elements.rb\nlib/mechanize/pluggable_parsers.rb] lib/mechanize.rb WWW dot/m_19_0.png

Synopsis

This is a pluggable parser that automatically saves every file it encounters. It saves the files as a tree, reflecting the host and file path.

Example to save all PDF‘s

 require 'rubygems'
 require 'mechanize'

 agent = WWW::Mechanize.new
 agent.pluggable_parser.pdf = WWW::Mechanize::FileSaver
 agent.get('http://example.com/foo.pdf')

Methods

new  

Attributes

filename  [R] 

Public Class methods

[Source]

     # File lib/mechanize/pluggable_parsers.rb, line 89
 89:       def initialize(uri=nil, response=nil, body=nil, code=nil)
 90:         super(uri, response, body, code)
 91:         path = uri.path.empty? ? 'index.html' : uri.path.gsub(/^[\/]*/, '')
 92:         path += 'index.html' if path =~ /\/$/
 93: 
 94:         split_path = path.split(/\//)
 95:         filename = split_path.length > 0 ? split_path.pop : 'index.html'
 96:         joined_path = split_path.join(::File::SEPARATOR)
 97:         path = if joined_path.empty?
 98:           uri.host
 99:         else
100:           "#{uri.host}#{::File::SEPARATOR}#{joined_path}"
101:         end
102: 
103:         @filename = "#{path}#{::File::SEPARATOR}#{filename}"
104:         FileUtils.mkdir_p(path)
105:         save_as(@filename)
106:       end

[Validate]