| Class | WWW::Mechanize::FileSaver |
| In: |
lib/mechanize/pluggable_parsers.rb
|
| Parent: | File |
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.
require 'rubygems'
require 'mechanize'
agent = WWW::Mechanize.new
agent.pluggable_parser.pdf = WWW::Mechanize::FileSaver
agent.get('http://example.com/foo.pdf')
| filename | [R] |
# 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